Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Original algorithms by slacky, optimized by Janilabo
- const
- POLYGON_EXAMPLE = 2; // 0-9.
- function BuildShape: TPointArray;
- begin
- case POLYGON_EXAMPLE of
- // SQUARE: TOP LEFT BOTTOM RIGHT
- 0: Result := [Point(20,20),Point(20,120),Point(120,120),Point(120,20)];
- // "Arrow" shape----//
- 1: Result := [Point(70,90),Point(185,90),Point(185,116), Point(70,116), //Square +
- Point(70,140),Point(35,105),Point(70,70)]; //Triangle
- // Triangle --------//
- 2: Result := [Point(50,150),Point(100,100),Point(150,150)];
- // Raped pentagon --//
- 3: Result := [Point(10,25),Point(35,5),Point(82,22),Point(50,90),Point(4,60)];
- // Octagon ---------//
- 4: Result := [Point(100,30), Point(155,50), //Top-Right
- Point(180,110),Point(155,160), //Right-Bottom
- Point(100,180),Point(45,160), //Bottom-Left
- Point(20,100), Point(45,50)]; //Left-Top
- // S-shape ---------//
- 5: Result := [Point(135,50),Point(123,70),Point(103,58),Point(85,70),Point(85,85),
- Point(122,105),Point(132,125),Point(120,155),Point(78,164),Point(55,147),
- Point(64,128),Point(80,140),Point(100,140),Point(96,116),Point(62,98),
- Point(60,55),Point(111,35)];
- else
- TerminateScript;
- end;
- end;
- function TPAFromPolygon(shape: TPointArray): TPointArray;
- var
- b: TBox;
- x, y, h, i, l, r, z: Integer;
- o: TPointArray;
- f: Boolean;
- t: array of TBoolArray;
- e: Extended;
- q, p, d: TPoint;
- begin
- h := High(shape);
- if (h > -1) then
- begin
- SetLength(o, 0);
- b := IntToBox(shape[0].X, shape[0].Y, shape[0].X, shape[0].Y);
- for i := 0 to h do
- begin
- q := shape[i];
- if (i < h) then
- p := shape[(i + 1)]
- else
- p := shape[0];
- r := Length(o);
- if ((q.X <> p.X) or (q.Y <> p.Y)) then
- begin
- l := Max(Round(Abs(q.X - p.X)), Round(Abs(q.Y - p.Y)));
- SetLength(o, ((r + l) + 1));
- for z := 0 to l do
- o[(r + z)] := Point((q.X + Round((p.X - q.X) * (z / Extended(l)))), (q.Y + Round((p.Y - q.Y) * (z / Extended(l)))));
- end else
- begin
- SetLength(o, (r + 1));
- o[r] := q;
- end;
- if (shape[i].X < b.X1) then
- b.X1 := shape[i].X
- else
- if (shape[i].X > b.X2) then
- b.X2 := shape[i].X;
- if (shape[i].Y < b.Y1) then
- b.Y1 := shape[i].Y
- else
- if (shape[i].Y > b.Y2) then
- b.Y2 := shape[i].Y;
- end;
- SetLength(t, ((b.X2 - b.X1) + 1));
- for i := 0 to (b.X2 - b.X1) do
- SetLength(t[i], ((b.Y2 - b.Y1) + 1));
- l := Length(o);
- for i := 0 to (l - 1) do
- if not t[(o[i].X - b.X1)][(o[i].Y - b.Y1)] then
- t[(o[i].X - b.X1)][(o[i].Y - b.Y1)] := True;
- for y := 0 to (b.Y2 - b.Y1) do
- for x := 0 to (b.X2 - b.X1) do
- begin
- f := t[x][y];
- if not f then
- begin
- d := Point((x + b.X1), (y + b.Y1));
- q := shape[0];
- for i := 0 to (h + 1) do
- begin
- p := shape[(i mod (h + 1))];
- if (d.Y > Min(q.Y, p.Y)) then
- if (d.Y <= Max(q.Y, p.Y)) then
- if (d.X <= Max(q.X, p.X)) then
- begin
- if (q.y <> p.y) then
- e := ((d.Y - q.Y) * (p.X - q.X) / Extended((p.Y - q.Y)) + q.X);
- if ((q.X = p.X) or (d.X < e)) then
- f := not f;
- end;
- q := p;
- end;
- end;
- if f then
- begin
- l := Length(Result);
- SetLength(Result, (l + 1));
- Result[l] := Point((x + b.X1), (y + b.Y1));
- end;
- end;
- end else
- SetLength(Result, 0);
- end;
- procedure Setup;
- var
- TPA: TPointArray;
- bmp: Integer;
- a: TBox;
- begin
- TPA := TPAFromPolygon(BuildShape);
- a := GetTPABounds(TPA);
- WriteLn(ToStr(A));
- bmp := CreateBitmap(((a.X2 + a.X1) + 1), ((a.Y2 + a.Y1) + 1));
- DrawTPABitmap(bmp, TPA, 357435);
- DisplayDebugImgWindow((a.X2 + a.X1), (a.Y2 + a.Y1));
- DrawBitmapDebugImg(bmp);
- FreeBitmap(bmp);
- end;
- begin
- ClearDebug;
- Setup;
- end.
Advertisement
Add Comment
Please, Sign In to add comment