Recent

Author Topic: [solved] unfilled polygons and uninitialized arrays :)  (Read 529 times)

speter

  • Sr. Member
  • ****
  • Posts: 338
[solved] unfilled polygons and uninitialized arrays :)
« on: April 29, 2022, 08:22:18 am »
G'Day Folks,

I have 2 minor problems in a program I an writing (project attached).

Firstly, I thought that canvas.polygon() would draw an unfilled polygon if canvas.brush.style = bsclear!?

In my paint procedure I have:

Code: Pascal  [Select][+][-]
  1.   paintbox1.canvas.brush.style := bssolid;
  2.   paintbox1.canvas.brush.color := clwhite;
  3.   for a := 1 to num_shapes do
  4.     drawStar(drawing[a]);
  5.  
  6. ...
  7.  
  8.   paintbox1.canvas.brush.style := bsclear;
  9.   paintbox1.canvas.brush.color := clgreen;
  10.   temp.p[pt_count] := pos;
  11.   if pt_count=1 then
  12.     drawRegPoly(temp)
  13.   else if pt_count=2 then
  14.     drawstar(temp);

drawRegPoly() and drawStar() do some calculations then call canvas.polygon(). But, as you can see I am setting the brush.style to bsclear in the later bit of code. I was expecting that the resulting polygon would look like a polyline (but closed); whereas it draws filled.

Am I doing something wrong, or is this operating as expected!?


Secondly, if I set the Project Optimization to 1 or 2 (Project > Project Options > Compiler Options > Compilation and Linking > Optimization levels), I get a Hint when compiling:
Code: [Select]
unit1.pas(141,22) Hint: Local variable "pts" of a managed type does not seem to be initialized
unit1.pas(177,22) Hint: Local variable "pts" of a managed type does not seem to be initialized
"pts" in each case is declared as follows:
Code: Pascal  [Select][+][-]
  1. var pts : array of tpoint;

The lines referenced above refer to the setlength() calls; which are followed in each case with a loop that populates the arrays. The program works fine and "Heaptrc" finds no memory leaks. If the optimization = 3, the hint is not displayed.

Listed below is the code from Linke 141:
Code: Pascal  [Select][+][-]
  1.         setlength(pts,sides);
  2.         a_inc := 2 * pi / sides;
  3.         for a := 0 to sides-1 do
  4.           begin
  5.             rads := angle + a*a_inc;
  6.             pts[a] := point(p[0].x + round(radius * sin(rads)),
  7.                             p[0].y - round(radius * cos(rads)));
  8.           end;

So, is there a "better" way to initialize a dynamic array!?

cheers
S.



« Last Edit: April 29, 2022, 11:37:35 am by speter »
I climbed mighty mountains, and saw that they were actually tiny foothills. :)

wp

  • Hero Member
  • *****
  • Posts: 11830
Re: unfilled polygons and uninitialized arrays :)
« Reply #1 on: April 29, 2022, 10:12:55 am »
Firstly, I thought that canvas.polygon() would draw an unfilled polygon if canvas.brush.style = bsclear!?
Your first thought is correct - see attached demo. The first star is drawn as filled by red, the second star is drawn without fill (bsClear), as expected.

But when you, like in your code, set the Brush.Color after the Brush.Style, then bsClear switches back to bsSolid - this is kind of logical, because when you set the color you want to see it. Delphi, BTW, has the same behaviour.

So, in order to fix the issue, either don't set clGreen at all, or move the setting of Brush.Color to before setting Brush.Style.

Your second question: Initialize the local dynamic array with nil in order to avoid the compiler hint. SetLength itself does not initialize the array elements because otherwise it would not be possible to change the length of a previously filled array without losing the data values.
« Last Edit: April 29, 2022, 10:18:19 am by wp »

speter

  • Sr. Member
  • ****
  • Posts: 338
Re: unfilled polygons and uninitialized arrays :)
« Reply #2 on: April 29, 2022, 11:36:11 am »
Thanks very much!

cheers
S.
I climbed mighty mountains, and saw that they were actually tiny foothills. :)

 

TinyPortal © 2005-2018