Help! How to better write this? lazarus / 21-General ---------------------------------------------------------------------- OC DelGuy · 2021-02-11 05:33 I made a grid of 484 (22x22) squares.  As I click in each square, I want to draw a rectangle in that square (and eventually more).  So far I got this much, but there must be a better and more efficient way to write all these 484 conditions.  Can anyone help me? Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i     If ((d.X>x+1) and (d.Xy+1) and (d.Yx+1+SqSize) and (d.Xy+1) and (d.Yx+1+SqSize* 2) and (d.Xy+1) and (d.Yx+1+SqSize* 3) and (d.Xy+1) and (d.Yx+1+SqSize* 4) and (d.Xy+1) and (d.Yx+1+SqSize* 5) and (d.Xy+1) and (d.Yx+1+SqSize* 6) and (d.Xy+1) and (d.Yx+1+SqSize* 7) and (d.Xy+1) and (d.Yx+1+SqSize* 8) and (d.Xy+1) and (d.Yx+1+SqSize* 9) and (d.Xy+1) and (d.Yy+1) and (d.Yy+1) and (d.Y x + 1 + SqSize * I then begin      Found := True;      Canvas.Rectangle(x + 1 + SqSize * I, y + 1, x + SqSize * (I + 1), y + SqSize);      Break;    end;  end;end;if not Found then  Label3.Caption:='Please click inside the small squares'; ---------------------------------------------------------------------- egsuh · 2021-02-11 13:57 I re-checked the formula. Following codes would do all (still may need minor changes). Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i      // check out of range first.     // if (d.X <= x + 1) or (d.X > x + SqSize * {max count}) then Exit;     // if (d.Y <= y + 1) or (d.Y > y + SqSize * {max count}) then Exit;      xpos := (d.X-x-1) div SqSize;    x1 := x + 1 + SqSize * xpos;      x2 := x + SqSize * (xpos+1);      ypos := (d.Y-y-1) div SqSize;       y1 := y + 1 + SqSize * ypos;     y2 := y + SqSize * (ypos+1);        Canvas.Rectangle(x1, y1 x2, y2);     But if the size of each rectagle is not contant, then Handoko's method is right.