Forum > LCL

[solved] unfilled polygons and uninitialized arrays :)

(1/1)

speter:
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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---  paintbox1.canvas.brush.style := bssolid;  paintbox1.canvas.brush.color := clwhite;  for a := 1 to num_shapes do    drawStar(drawing[a]); ...   paintbox1.canvas.brush.style := bsclear;  paintbox1.canvas.brush.color := clgreen;  temp.p[pt_count] := pos;  if pt_count=1 then    drawRegPoly(temp)  else if pt_count=2 then    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: ---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
--- End code ---
"pts" in each case is declared as follows:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---        setlength(pts,sides);        a_inc := 2 * pi / sides;        for a := 0 to sides-1 do          begin            rads := angle + a*a_inc;            pts[a] := point(p[0].x + round(radius * sin(rads)),                            p[0].y - round(radius * cos(rads)));          end;
So, is there a "better" way to initialize a dynamic array!?

cheers
S.



wp:

--- Quote from: speter on April 29, 2022, 08:22:18 am ---Firstly, I thought that canvas.polygon() would draw an unfilled polygon if canvas.brush.style = bsclear!?

--- End quote ---
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.

speter:
Thanks very much!

cheers
S.

Navigation

[0] Message Index

Go to full version