Recent

Author Topic: Trying to draw rectangles in random positions  (Read 1431 times)

Prado

  • Newbie
  • Posts: 2
Trying to draw rectangles in random positions
« on: September 08, 2019, 05:14:13 pm »
I'm trying to generate random points and then drawing rectangles in those positions, after pressing a button.

The problem is that the rectangles are drawn only inside the button, instead of the entire window.

Code of Unit:

Code: Pascal  [Select][+][-]
  1. const
  2.   MAX_POINTS = 300;
  3.  
  4. var
  5.   Form1: TForm1;
  6.   Points : array [0 .. MAX_POINTS - 1] of TPoint;
  7.   {isDrawing : boolean;}
  8.  
  9. implementation
  10.  
  11. {$R *.lfm}
  12.  
  13. { TForm1 }
  14.  
  15. procedure TForm1.FormCreate(Sender: TObject);
  16. var
  17.   i:integer;
  18. begin
  19.   Randomize;
  20.   {isDrawing := False;}
  21.   i := 0;
  22.   while i < MAX_POINTS do
  23.   begin
  24.     Points[i] := Point(0, 0);
  25.     i := i + 1;
  26.   end;
  27. end;
  28.  
  29. procedure TForm1.FormPaint(Sender: TObject);
  30. var
  31.   i:integer;
  32. begin
  33.     i := 0;
  34.     while i < MAX_POINTS do
  35.       begin
  36.         Canvas.Pen.Color := clBlack;
  37.         Canvas.Rectangle(Points[i].x, Points[i].y, Points[i].x + 6, Points[i].y + 6);
  38.         i := i + 1;
  39.       end;
  40. end;
  41.  
  42. procedure TForm1.BRandomClick(Sender: TObject);
  43. var
  44.   px:integer;
  45.   py:integer;
  46.   i:integer;
  47. begin
  48.   i := 0;
  49.   while i < MAX_POINTS do
  50.   begin
  51.     px := random(Canvas.Width);
  52.     py := random(Canvas.Height);
  53.     Points[i].x := px;
  54.     Points[i].y := py;
  55.     i := i + 1;
  56.   end;
  57. end;
« Last Edit: September 08, 2019, 05:21:02 pm by Prado »

bigeno

  • Sr. Member
  • ****
  • Posts: 266
Re: Trying to draw rectangles in random positions
« Reply #1 on: September 08, 2019, 05:29:16 pm »
Then don't use BRandom.Canvas

wp

  • Hero Member
  • *****
  • Posts: 11916
Re: Trying to draw rectangles in random positions
« Reply #2 on: September 08, 2019, 05:39:39 pm »
Maybe I did not set up my test program correctly, but for me there is only a single point at the top-left corner of the form (It would be better if you'd post a compilable project instead of a code snippet, this avoids any misunderstandings).

I do get a form full of points when I
- use the form's width and height instead of the Canvas width/height. In fact, I was not even aware that the canvas has a width and a height, I always thought that it is "infinite" - maybe it is the size of the clip rect?
- force repainting of the form by calling Invalidate at the end of the button-click procedure.

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   px:integer;
  4.   py:integer;
  5.   i:integer;
  6. begin
  7.   i := 0;
  8.   while i < MAX_POINTS do
  9.   begin
  10.     px := random(Width); //random(Canvas.Width);  <-- replaced
  11.     py := random(Height); //random(Canvas.Height);
  12.     Points[i].x := px;
  13.     Points[i].y := py;
  14.     i := i + 1;
  15.   end;
  16.   Invalidate;  // <--- added
  17. end;

Prado

  • Newbie
  • Posts: 2
Re: Trying to draw rectangles in random positions
« Reply #3 on: September 08, 2019, 05:44:42 pm »
Maybe I did not set up my test program correctly, but for me there is only a single point at the top-left corner of the form (It would be better if you'd post a compilable project instead of a code snippet, this avoids any misunderstandings).

I do get a form full of points when I
- use the form's width and height instead of the Canvas width/height. In fact, I was not even aware that the canvas has a width and a height, I always thought that it is "infinite" - maybe it is the size of the clip rect?
- force repainting of the form by calling Invalidate at the end of the button-click procedure.

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   px:integer;
  4.   py:integer;
  5.   i:integer;
  6. begin
  7.   i := 0;
  8.   while i < MAX_POINTS do
  9.   begin
  10.     px := random(Width); //random(Canvas.Width);  <-- replaced
  11.     py := random(Height); //random(Canvas.Height);
  12.     Points[i].x := px;
  13.     Points[i].y := py;
  14.     i := i + 1;
  15.   end;
  16.   Invalidate;  // <--- added
  17. end;

Thanks, it works now. I forgot to mention the rectangle at the corner.

 

TinyPortal © 2005-2018