Home > Delphi > Scale a polygon?

## Scale a polygon?

Following up on fun “bare-bones” retro coding, here is how you scale a polygon. While I can’t think of a single use for this in a real life application, I can paradoxically also think of a million uses for it. For instance, if you are making a paint program which uses polygon based brushes – you could use this to scale the same shape up and down. Well, I’ll leave the creativity up to you — it’s retro time!

Note: The example below is just one way of doing things. As a friend of mine pointed out, it would be considerably faster to use a matrix / linear approach.

Once again, this is written in Smart Mobile Studio‘s version of object pascal, but it should be easy to get it running on vanilla Delphi 🙂

```type
TPointArray = array of TPoint;

Function ScalePolygon(factor:Float;InPoints:TPointArray;
Const centerX,centerY:Float):TPointArray;
var
i:   integer;
r,p: float;
begin
result.SetLength(inPoints.length);
if inPoints.Length>0 then
begin
for i := 0 to inPoints.high do
begin
r:= factor * sqrt(sqr(InPoints[i].X - centerX) + sqr(InPoints[i].Y - centerY));
p:= arcTan2(InPoints[i].Y - centerY, InPoints[i].X - centerX);
Result[i].X := Round(centerX + r * cos(p));
Result[i].Y := Round(centerY + r * sin(p));
end;
end;
end;
```

Incidentaly, here is Spaceballs – the Amiga polygon demo par excellence:

1. January 20, 2017 at 2:39 pm

Hi,

Remove lines 8, 15 and 16, and change lines 17 and 18 to:

Result[i].X := Round(centerX + factor * (InPoints[i].X – centerX));
Result[i].Y := Round(centerY + factor * (InPoints[i].Y – centerY));

It’s faster.