Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Original algorithms by slacky, optimized by Janilabo
- const
- POLYGON_EXAMPLE = 1; // 0-9.
- DISPLAY_MAINPOINTS = True; // True or False.
- OFFSET = 5; // Used for debug image..
- {==============================================================================]
- Explanation: Returns polygon by given outlines (shape)
- NOTE: Original algorithm by slacky (warpie)
- [==============================================================================}
- 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
- 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;
- SetLength(o, 0);
- 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;
- SetLength(t, 0);
- end else
- SetLength(Result, 0);
- end;
- {==============================================================================]
- Explanation: Creates all the points needed to define a simple polygon
- NOTE: Original algorithm by slacky (warpie)
- [==============================================================================}
- function TPAXagon(sides: Integer; C, S: TPoint): TPointArray;
- var
- x, y, a, b, v: Integer;
- n, m: Extended;
- begin
- if (sides > 2) then
- begin
- SetLength(Result, sides);
- x := S.x;
- y := S.y;
- n := Sin(Radians(360.0 / sides));
- m := Cos(Radians(360.0 / sides));
- for v := 0 to (sides - 1) do
- begin
- a := (x - C.x);
- b := (y - C.y);
- x := Round(((b * n) + (a * m) + C.x));
- y := Round(((b * m) - (a * n) + C.y));
- Result[v] := Point(x, y);
- end;
- end else
- SetLength(Result, 0);
- end;
- //******* DIRTY EXAMPLE BELLOW: *******//
- procedure SetPixels(bmp: Integer; TPA: TPointArray; color: Integer);
- var
- a, z, w, h: Integer;
- begin
- z := High(TPA);
- if (z > -1) then
- begin
- GetBitmapSize(bmp, w, h);
- for a := 0 to z do
- if ((TPA[a].X >= 0) and (TPA[a].Y >= 0) and (TPA[a].X < w) and (TPA[a].Y < h)) then
- FastSetPixel(bmp, TPA[a].X, TPA[a].Y, color);
- end;
- end;
- procedure SetPixelsMP(bmp: Integer; TPA: TPointArray; color: Integer);
- var
- a, z, w, h: Integer;
- begin
- z := High(TPA);
- if (z > -1) then
- begin
- GetBitmapSize(bmp, w, h);
- for a := 0 to z do
- if ((TPA[a].X > 0) and (TPA[a].Y > 0) and (TPA[a].X < (w - 1)) and (TPA[a].Y < (h - 1))) then
- begin
- FastSetPixel(bmp, (TPA[a].X - 1), TPA[a].Y, color);
- FastSetPixel(bmp, (TPA[a].X + 1), TPA[a].Y, color);
- FastSetPixel(bmp, TPA[a].X, (TPA[a].Y + 1), color);
- FastSetPixel(bmp, TPA[a].X, (TPA[a].Y - 1), color);
- end;
- end;
- end;
- procedure Setup;
- var
- shape, TPA: TPointArray;
- bmp, t, o: Integer;
- v: Boolean;
- a: TBox;
- begin
- //Dynamic shapes from Example 6 and down:
- v := True;
- case POLYGON_EXAMPLE of
- // Microshape ------//
- // TOP LEFT BOTTOM RIGHT
- 0: shape := [Point(20,20), Point(20,120), Point(120,120), Point(120,20)];
- // Arrow like shape //
- 1: shape := [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: shape := [Point(50,50),Point(0,100),Point(100,150)];
- // Raped pentagon --//
- 3: shape := [Point(10,25),Point(35,5),Point(82,22),Point(50,90),Point(4,60)];
- // Octagon ---------//
- 4: shape := [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: shape := [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)];
- // Xagon: 15 corners //
- 6: shape := TPAXagon(15, Point(100,100), Point(60,100));
- // Xagon: 9 corners //
- 7: shape := TPAXagon(9, Point(100,100), Point(60,50));
- // Xagon: 5 corners AKA Pentagon //
- 8: shape := TPAXagon(5, Point(100,100), Point(74,50));
- // Xagon: 3 corners AKA Triangle //
- 9: shape := TPAXagon(3, Point(100,100), Point(48,48));
- else
- v := False;
- end;
- if v then
- begin
- if ((a.X1 > -1) and (a.Y1 > -1)) then
- begin
- if (OFFSET > 0) then
- o := OFFSET;
- t := GetSystemTime;
- if (o > 0) then
- OffsetTPA(shape, Point(o, o));
- TPA := TPAFromPolygon(shape);
- a := GetTPABounds(TPA);
- bmp := CreateBitmap((a.X2 + o), (a.Y2 + o));
- WriteLn(IntToStr(GetSystemTime - t) + ' ms.');
- SetPixels(bmp, TPA, 357435);
- if DISPLAY_MAINPOINTS then
- SetPixelsMP(bmp, shape, 16777215);
- DisplayDebugImgWindow((a.X2 + a.X1), (a.Y2 + a.Y1));
- DrawBitmapDebugImg(bmp);
- FreeBitmap(bmp);
- SetLength(TPA, 0);
- end else
- WriteLn('Invalid TPA! Negative points found...');
- end else
- WriteLn('Invalid polygon ID!');
- end;
- begin
- ClearDebug;
- Setup;
- end.
Advertisement
Add Comment
Please, Sign In to add comment