Janilabo

TPAFromPolygon() [Simba]

Jul 21st, 2013
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 7.28 KB | None | 0 0
  1. // Original algorithms by slacky, optimized by Janilabo
  2.  
  3. const
  4.   POLYGON_EXAMPLE = 1; // 0-9.
  5.   DISPLAY_MAINPOINTS = True; // True or False.
  6.   OFFSET = 5; // Used for debug image..
  7.  
  8. {==============================================================================]
  9.   Explanation: Returns polygon by given outlines (shape)
  10.   NOTE: Original algorithm by slacky (warpie)
  11. [==============================================================================}
  12. function TPAFromPolygon(shape: TPointArray): TPointArray;
  13. var
  14.   b: TBox;
  15.   x, y, h, i, l, r, z: Integer;
  16.   o: TPointArray;
  17.   f: Boolean;
  18.   t: array of TBoolArray;
  19.   e: Extended;
  20.   q, p, d: TPoint;
  21. begin
  22.   h := High(shape);
  23.   if (h > -1) then
  24.   begin
  25.     b := IntToBox(shape[0].X, shape[0].Y, shape[0].X, shape[0].Y);
  26.     for i := 0 to h do
  27.     begin
  28.       q := shape[i];
  29.       if (i < h) then
  30.         p := shape[(i + 1)]
  31.       else
  32.         p := shape[0];
  33.       r := Length(o);
  34.       if ((q.X <> p.X) or (q.Y <> p.Y)) then
  35.       begin
  36.         l := Max(Round(Abs(q.X - p.X)), Round(Abs(q.Y - p.Y)));
  37.         SetLength(o, ((r + l) + 1));
  38.         for z := 0 to l do
  39.           o[(r + z)] := Point((q.X + Round((p.X - q.X) * (z / Extended(l)))), (q.Y + Round((p.Y - q.Y) * (z / Extended(l)))));
  40.       end else
  41.       begin
  42.         SetLength(o, (r + 1));
  43.         o[r] := q;
  44.       end;
  45.       if (shape[i].X < b.X1) then
  46.         b.X1 := shape[i].X
  47.       else
  48.         if (shape[i].X > b.X2) then
  49.           b.X2 := shape[i].X;
  50.       if (shape[i].Y < b.Y1) then
  51.         b.Y1 := shape[i].Y
  52.       else
  53.         if (shape[i].Y > b.Y2) then
  54.           b.Y2 := shape[i].Y;
  55.     end;
  56.     SetLength(t, ((b.X2 - b.X1) + 1));
  57.     for i := 0 to (b.X2 - b.X1) do
  58.       SetLength(t[i], ((b.Y2 - b.Y1) + 1));
  59.     l := Length(o);
  60.     for i := 0 to (l - 1) do
  61.       if not t[(o[i].X - b.X1)][(o[i].Y - b.Y1)] then
  62.         t[(o[i].X - b.X1)][(o[i].Y - b.Y1)] := True;
  63.     SetLength(o, 0);
  64.     for y := 0 to (b.Y2 - b.Y1) do
  65.       for x := 0 to (b.X2 - b.X1) do
  66.       begin
  67.         f := t[x][y];
  68.         if not f then
  69.         begin
  70.           d := Point((x + b.X1), (y + b.Y1));
  71.           q := shape[0];
  72.           for i := 0 to (h + 1) do
  73.           begin
  74.             p := shape[(i mod (h + 1))];
  75.             if (d.Y > Min(q.Y, p.Y)) then
  76.               if (d.Y <= Max(q.Y, p.Y)) then
  77.                 if (d.X <= Max(q.X, p.X)) then
  78.                 begin
  79.                   if (q.y <> p.y) then
  80.                     e := ((d.Y - q.Y) * (p.X - q.X) / Extended((p.Y - q.Y)) + q.X);
  81.                   if ((q.X = p.X) or (d.X < e)) then
  82.                     f := not f;
  83.                 end;
  84.             q := p;
  85.           end;
  86.         end;
  87.         if f then
  88.         begin
  89.           l := Length(Result);
  90.           SetLength(Result, (l + 1));
  91.           Result[l] := Point((x + b.X1), (y + b.Y1));
  92.         end;
  93.       end;
  94.     SetLength(t, 0);
  95.   end else
  96.     SetLength(Result, 0);
  97. end;
  98.  
  99. {==============================================================================]
  100.   Explanation: Creates all the points needed to define a simple polygon
  101.   NOTE: Original algorithm by slacky (warpie)
  102. [==============================================================================}
  103. function TPAXagon(sides: Integer; C, S: TPoint): TPointArray;
  104. var
  105.   x, y, a, b, v: Integer;
  106.   n, m: Extended;
  107. begin
  108.   if (sides > 2) then
  109.   begin
  110.     SetLength(Result, sides);
  111.     x := S.x;
  112.     y := S.y;
  113.     n := Sin(Radians(360.0 / sides));
  114.     m := Cos(Radians(360.0 / sides));
  115.     for v := 0 to (sides - 1) do
  116.     begin
  117.       a := (x - C.x);
  118.       b := (y - C.y);
  119.       x := Round(((b * n) + (a * m) + C.x));
  120.       y := Round(((b * m) - (a * n) + C.y));
  121.       Result[v] := Point(x, y);
  122.     end;
  123.   end else
  124.     SetLength(Result, 0);
  125. end;
  126.  
  127. //******* DIRTY EXAMPLE BELLOW: *******//
  128. procedure SetPixels(bmp: Integer; TPA: TPointArray; color: Integer);
  129. var
  130.   a, z, w, h: Integer;
  131. begin
  132.   z := High(TPA);
  133.   if (z > -1) then
  134.   begin
  135.     GetBitmapSize(bmp, w, h);
  136.     for a := 0 to z do
  137.       if ((TPA[a].X >= 0) and (TPA[a].Y >= 0) and (TPA[a].X < w) and (TPA[a].Y < h)) then
  138.         FastSetPixel(bmp, TPA[a].X, TPA[a].Y, color);
  139.   end;
  140. end;
  141.  
  142. procedure SetPixelsMP(bmp: Integer; TPA: TPointArray; color: Integer);
  143. var
  144.   a, z, w, h: Integer;
  145. begin
  146.   z := High(TPA);
  147.   if (z > -1) then
  148.   begin
  149.     GetBitmapSize(bmp, w, h);
  150.     for a := 0 to z do
  151.       if ((TPA[a].X > 0) and (TPA[a].Y > 0) and (TPA[a].X < (w - 1)) and (TPA[a].Y < (h - 1))) then
  152.       begin
  153.         FastSetPixel(bmp, (TPA[a].X - 1), TPA[a].Y, color);
  154.         FastSetPixel(bmp, (TPA[a].X + 1), TPA[a].Y, color);
  155.         FastSetPixel(bmp, TPA[a].X, (TPA[a].Y + 1), color);
  156.         FastSetPixel(bmp, TPA[a].X, (TPA[a].Y - 1), color);
  157.       end;
  158.   end;
  159. end;
  160.  
  161. procedure Setup;
  162. var
  163.   shape, TPA: TPointArray;
  164.   bmp, t, o: Integer;
  165.   v: Boolean;
  166.   a: TBox;
  167. begin
  168.   //Dynamic shapes from Example 6 and down:
  169.   v := True;
  170.   case POLYGON_EXAMPLE of
  171.     // Microshape ------//
  172.     //           TOP           LEFT           BOTTOM          RIGHT
  173.     0: shape := [Point(20,20), Point(20,120), Point(120,120), Point(120,20)];
  174.     // Arrow like shape //
  175.     1: shape := [Point(70,90),Point(185,90),Point(185,116), Point(70,116), //Square +
  176.                  Point(70,140),Point(35,105),Point(70,70)];                //Triangle
  177.     // Triangle --------//
  178.     2: shape := [Point(50,50),Point(0,100),Point(100,150)];
  179.     // Raped pentagon --//
  180.     3: shape := [Point(10,25),Point(35,5),Point(82,22),Point(50,90),Point(4,60)];
  181.     // Octagon ---------//
  182.     4: shape := [Point(100,30), Point(155,50),  //Top-Right
  183.                  Point(180,110),Point(155,160), //Right-Bottom
  184.                  Point(100,180),Point(45,160),  //Bottom-Left
  185.                  Point(20,100), Point(45,50)];  //Left-Top
  186.     // S-shape ---------//
  187.     5: shape := [Point(135,50),Point(123,70),Point(103,58),Point(85,70),Point(85,85),
  188.                  Point(122,105),Point(132,125),Point(120,155),Point(78,164),Point(55,147),
  189.                  Point(64,128),Point(80,140),Point(100,140),Point(96,116),Point(62,98),
  190.                  Point(60,55),Point(111,35)];
  191.     // Xagon: 15 corners //
  192.     6: shape := TPAXagon(15, Point(100,100), Point(60,100));
  193.     // Xagon: 9 corners //
  194.     7: shape := TPAXagon(9, Point(100,100), Point(60,50));
  195.     // Xagon: 5 corners AKA Pentagon //
  196.     8: shape := TPAXagon(5, Point(100,100), Point(74,50));
  197.     // Xagon: 3 corners AKA Triangle //
  198.     9: shape := TPAXagon(3, Point(100,100), Point(48,48));
  199.   else
  200.     v := False;
  201.   end;
  202.   if v then
  203.   begin
  204.     if ((a.X1 > -1) and (a.Y1 > -1)) then
  205.     begin
  206.       if (OFFSET > 0) then
  207.         o := OFFSET;
  208.       t := GetSystemTime;
  209.       if (o > 0) then
  210.         OffsetTPA(shape, Point(o, o));
  211.       TPA := TPAFromPolygon(shape);
  212.       a := GetTPABounds(TPA);
  213.       bmp := CreateBitmap((a.X2 + o), (a.Y2 + o));
  214.       WriteLn(IntToStr(GetSystemTime - t) + ' ms.');
  215.       SetPixels(bmp, TPA, 357435);
  216.       if DISPLAY_MAINPOINTS then
  217.         SetPixelsMP(bmp, shape, 16777215);
  218.       DisplayDebugImgWindow((a.X2 + a.X1), (a.Y2 + a.Y1));
  219.       DrawBitmapDebugImg(bmp);
  220.       FreeBitmap(bmp);
  221.       SetLength(TPA, 0);
  222.     end else
  223.       WriteLn('Invalid TPA! Negative points found...');
  224.   end else
  225.     WriteLn('Invalid polygon ID!');
  226. end;
  227.  
  228. begin
  229.   ClearDebug;
  230.   Setup;
  231. end.
Advertisement
Add Comment
Please, Sign In to add comment