LCL/TShape: Simplify usage of point arrays.

This commit is contained in:
wp_xyz 2023-09-11 19:54:38 +02:00
parent 3ec349e0eb
commit 6873e2646a

View File

@ -74,10 +74,8 @@ const
var var
PaintRect: TRect; PaintRect: TRect;
MinSize: Longint; MinSize: Longint;
P: array[0..3] of TPoint; P: array of TPoint;
PStar: array[0..10] of TPoint;
PenInc, PenDec: Integer; PenInc, PenDec: Integer;
PolygonPts: array of TPoint;
PolygonWinding: Boolean; PolygonWinding: Boolean;
RadiusBig, RadiusBig2, RadiusSm, i: Integer; RadiusBig, RadiusBig2, RadiusSm, i: Integer;
PCenter: TPoint; PCenter: TPoint;
@ -109,6 +107,7 @@ begin
ACanvas.Ellipse(PaintRect); ACanvas.Ellipse(PaintRect);
stSquaredDiamond, stDiamond: stSquaredDiamond, stDiamond:
begin begin
SetLength(P, 4);
P[0].x := PaintRect.Left; P[0].x := PaintRect.Left;
P[0].y := (PaintRect.Top + PaintRect.Bottom) div 2; P[0].y := (PaintRect.Top + PaintRect.Bottom) div 2;
P[1].x := (PaintRect.Left + PaintRect.Right) div 2; P[1].x := (PaintRect.Left + PaintRect.Right) div 2;
@ -121,50 +120,46 @@ begin
end; end;
stTriangle: stTriangle:
begin begin
SetLength(P, 3);
P[0].x := (Width - 1) div 2; P[0].x := (Width - 1) div 2;
P[0].y := PenInc; P[0].y := PenInc;
P[1].x := Width - PenInc - 1; P[1].x := Width - PenInc - 1;
P[1].y := Height - PenInc - 1; P[1].y := Height - PenInc - 1;
P[2].x := PenInc; P[2].x := PenInc;
P[2].y := Height - PenInc - 1; P[2].y := Height - PenInc - 1;
P[3].x := P[0].x;
P[3].y := P[0].y;
ACanvas.Polygon(P); ACanvas.Polygon(P);
end; end;
stTriangleDown: stTriangleDown:
begin begin
SetLength(P, 3);
P[0].x := (Width - 1) div 2; P[0].x := (Width - 1) div 2;
P[0].y := Height - PenInc - 1; P[0].y := Height - PenInc - 1;
P[1].x := Width - PenInc - 1; P[1].x := Width - PenInc - 1;
P[1].y := PenInc; P[1].y := PenInc;
P[2].x := PenInc; P[2].x := PenInc;
P[2].y := PenInc; P[2].y := PenInc;
P[3].x := P[0].x;
P[3].y := P[0].y;
ACanvas.Polygon(P); ACanvas.Polygon(P);
end; end;
stTriangleLeft: stTriangleLeft:
begin begin
SetLength(P, 3);
P[0].x := PenInc; P[0].x := PenInc;
P[0].y := Height div 2; P[0].y := Height div 2;
P[1].x := Width - PenInc - 1; P[1].x := Width - PenInc - 1;
P[1].y := PenInc; P[1].y := PenInc;
P[2].x := Width - PenInc - 1; P[2].x := Width - PenInc - 1;
P[2].y := Height - PenInc - 1; P[2].y := Height - PenInc - 1;
P[3].x := P[0].x;
P[3].y := P[0].y;
ACanvas.Polygon(P); ACanvas.Polygon(P);
end; end;
stTriangleRight: stTriangleRight:
begin begin
SetLength(P, 3);
P[0].x := Width - PenInc - 1; P[0].x := Width - PenInc - 1;
P[0].y := Height div 2; P[0].y := Height div 2;
P[1].x := PenInc; P[1].x := PenInc;
P[1].y := PenInc; P[1].y := PenInc;
P[2].x := PenInc; P[2].x := PenInc;
P[2].y := Height - PenInc - 1; P[2].y := Height - PenInc - 1;
P[3].x := P[0].x;
P[3].y := P[0].y;
ACanvas.Polygon(P); ACanvas.Polygon(P);
end; end;
stStar, stStarDown: stStar, stStarDown:
@ -188,33 +183,33 @@ begin
PCenter.X := Width div 2; PCenter.X := Width div 2;
RadiusSm := RadiusBig * 57 div 150; RadiusSm := RadiusBig * 57 div 150;
SetLength(P, 10);
for i := 0 to 4 do for i := 0 to 4 do
begin begin
PStar[i*2].x := PCenter.X + Round(RadiusBig*CosStarBig[i, FShape=stStarDown]); P[i*2].x := PCenter.X + Round(RadiusBig*CosStarBig[i, FShape=stStarDown]);
PStar[i*2].y := PCenter.Y - Round(RadiusBig*SinStarBig[i, FShape=stStarDown]); P[i*2].y := PCenter.Y - Round(RadiusBig*SinStarBig[i, FShape=stStarDown]);
PStar[i*2+1].x := PCenter.X + Round(RadiusSm*CosStarSmall[i, FShape=stStarDown]); P[i*2+1].x := PCenter.X + Round(RadiusSm*CosStarSmall[i, FShape=stStarDown]);
PStar[i*2+1].y := PCenter.Y - Round(RadiusSm*SinStarSmall[i, FShape=stStarDown]); P[i*2+1].y := PCenter.Y - Round(RadiusSm*SinStarSmall[i, FShape=stStarDown]);
end; end;
// Fix 1 pixel error of horizontal lines, adjust point on small radius to the point on big one // Fix 1 pixel error of horizontal lines, adjust point on small radius to the point on big one
for i := 0 to 4 do for i := 0 to 4 do
if Abs(PStar[i*2].y - PStar[i*2+1].y) <= cStarError then if Abs(P[i*2].y - P[i*2+1].y) <= cStarError then
PStar[i*2+1].y := PStar[i*2].y; P[i*2+1].y := P[i*2].y;
for i := 1 to 4 do for i := 1 to 4 do
if Abs(PStar[i*2].y - PStar[i*2-1].y) <= cStarError then if Abs(P[i*2].y - P[i*2-1].y) <= cStarError then
PStar[i*2-1].y := PStar[i*2].y; P[i*2-1].y := P[i*2].y;
PStar[10] := PStar[0]; ACanvas.Polygon(P);
ACanvas.Polygon(PStar);
end; end;
stPolygon: stPolygon:
if Assigned(FOnShapePoints) then if Assigned(FOnShapePoints) then
begin begin
PolygonPts := nil; SetLength(P, 0);
PolygonWinding := false; PolygonWinding := false;
FOnShapePoints(Self, PolygonPts, PolygonWinding); FOnShapePoints(Self, P, PolygonWinding);
if Length(PolygonPts) > 2 then if Length(P) > 2 then
ACanvas.Polygon(PolygonPts, PolygonWinding); ACanvas.Polygon(P, PolygonWinding);
end; end;
end; end;
end; end;