Janilabo

tpafrompolygon

Sep 18th, 2013
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 6.06 KB | None | 0 0
  1. // WHITE = TPA NON-Convex Hull points
  2. // RED = TPA Convex Hull points
  3. // GREEN = Shape created by Convex Hull points (red)
  4.  
  5. const
  6.   GENERATE = 15; // Amount of random points.
  7.  
  8. procedure ConvexHull(var TPA: TPointArray);
  9. var
  10.   p, Lower: TPointArray;
  11.   LH, H, I, UH, c, j, idx, x, y: Integer;
  12.   area: TBox;
  13.   m: array of TBoolArray;
  14.   b: Boolean;
  15. begin
  16.   h := High(TPA);
  17.   if (h > 0) then
  18.   begin
  19.     area := GetTPABounds(TPA);
  20.     SetLength(m, ((area.X2 - area.X1) + 1));
  21.     for i := 0 to (area.X2 - area.X1) do
  22.       SetLength(m[i], ((area.Y2 - area.Y1) + 1));
  23.     C := 0;
  24.     for i := 0 to h do
  25.     begin
  26.       x := (TPA[i].X - area.X1);
  27.       y := (TPA[i].Y - area.Y1);
  28.       if m[x][y] then
  29.         Continue;
  30.       m[x][y] := True;
  31.       Inc(C);
  32.     end;
  33.     SetLength(p, C);
  34.     idx := 0;
  35.     b := False;
  36.     for y := area.Y1 to area.Y2 do
  37.     begin
  38.       for x := area.X1 to area.X2 do
  39.         if m[(x - area.X1)][(y - area.Y1)] then
  40.         begin
  41.           p[idx] := Point(x, y);
  42.           Inc(idx);
  43.           b := (idx >= C);
  44.           if b then
  45.             Break;
  46.         end;
  47.       if b then
  48.         Break;
  49.     end;
  50.     h := High(p);
  51.     if (h > 0) then
  52.     begin
  53.       UH := 2;
  54.       SetLength(TPA, (h + 1));
  55.       TPA[0] := p[0];
  56.       TPA[1] := p[1];
  57.       for i := 2 to h do
  58.       begin
  59.         TPA[UH] := p[i];
  60.         Inc(UH);
  61.         while ((UH > 2) and not (((TPA[(UH - 2)].x * TPA[(UH - 1)].y + TPA[(UH - 3)].x * TPA[(UH - 2)].y + TPA[(UH - 1)].x * TPA[(UH - 3)].y) - (TPA[(UH - 2)].x * TPA[(UH - 3)].y + TPA[(UH - 1)].x * TPA[(UH - 2)].y + TPA[(UH - 3)].x * TPA[(UH - 1)].y)) < 0)) do
  62.         begin
  63.           Dec(UH);
  64.           TPA[(UH - 1)] := TPA[UH];
  65.         end;
  66.       end;
  67.       LH := 2;
  68.       SetLength(Lower, (h + 1));
  69.       Lower[0] := p[h];
  70.       Lower[1] := p[(h - 1)];
  71.       for i := 2 to h do
  72.       begin
  73.         Lower[LH] := p[(h - i)];
  74.         Inc(LH);
  75.         while ((LH > 2) and not (((Lower[(LH - 2)].x * Lower[(LH - 1)].y + Lower[(LH - 3)].x * Lower[(LH - 2)].y + Lower[(LH - 1)].x * Lower[(LH - 3)].y) - (Lower[(LH - 2)].x * Lower[(LH - 3)].y + Lower[(LH - 1)].x * Lower[(LH - 2)].y + Lower[(LH - 3)].x * Lower[(LH - 1)].y)) < 0)) do
  76.         begin
  77.           Dec(LH);
  78.           Lower[(LH - 1)] := Lower[LH];
  79.         end;
  80.       end;
  81.       Dec(LH);
  82.       SetLength(TPA, (UH + LH));
  83.       for i := UH to ((UH + LH) - 1) do
  84.         TPA[i] := Lower[(i - UH)];
  85.     end;
  86.   end;
  87. end;
  88.  
  89. {==============================================================================]
  90.   Returns polygon by given main points OR outlines (shape)
  91. [==============================================================================}
  92. function TPAFromPolygon(shape: TPointArray): TPointArray;
  93. var
  94.   b: TBox;
  95.   x, y, h, i, l, r, z: Integer;
  96.   o: TPointArray;
  97.   f: Boolean;
  98.   t: array of TBoolArray;
  99.   e: Extended;
  100.   q, p, d: TPoint;
  101. begin
  102.   h := High(shape);
  103.   if (h > -1) then
  104.   begin
  105.     SetLength(o, 0);
  106.     b := IntToBox(shape[0].X, shape[0].Y, shape[0].X, shape[0].Y);
  107.     for i := 0 to h do
  108.     begin
  109.       q := shape[i];
  110.       if (i < h) then
  111.         p := shape[(i + 1)]
  112.       else
  113.         p := shape[0];
  114.       r := Length(o);
  115.       if ((q.X <> p.X) or (q.Y <> p.Y)) then
  116.       begin
  117.         l := Max(Round(Abs(q.X - p.X)), Round(Abs(q.Y - p.Y)));
  118.         SetLength(o, ((r + l) + 1));
  119.         for z := 0 to l do
  120.           o[(r + z)] := Point((q.X + Round((p.X - q.X) * (z / Extended(l)))), (q.Y + Round((p.Y - q.Y) * (z / Extended(l)))));
  121.       end else
  122.       begin
  123.         SetLength(o, (r + 1));
  124.         o[r] := q;
  125.       end;
  126.       if (shape[i].X < b.X1) then
  127.         b.X1 := shape[i].X
  128.       else
  129.         if (shape[i].X > b.X2) then
  130.           b.X2 := shape[i].X;
  131.       if (shape[i].Y < b.Y1) then
  132.         b.Y1 := shape[i].Y
  133.       else
  134.         if (shape[i].Y > b.Y2) then
  135.           b.Y2 := shape[i].Y;
  136.     end;
  137.     SetLength(t, ((b.X2 - b.X1) + 1));
  138.     for i := 0 to (b.X2 - b.X1) do
  139.       SetLength(t[i], ((b.Y2 - b.Y1) + 1));
  140.     l := Length(o);
  141.     for i := 0 to (l - 1) do
  142.       if not t[(o[i].X - b.X1)][(o[i].Y - b.Y1)] then
  143.         t[(o[i].X - b.X1)][(o[i].Y - b.Y1)] := True;
  144.     for y := 0 to (b.Y2 - b.Y1) do
  145.       for x := 0 to (b.X2 - b.X1) do
  146.       begin
  147.         f := t[x][y];
  148.         if not f then
  149.         begin
  150.           d := Point((x + b.X1), (y + b.Y1));
  151.           q := shape[0];
  152.           for i := 0 to (h + 1) do
  153.           begin
  154.             p := shape[(i mod (h + 1))];
  155.             if (d.Y > Min(q.Y, p.Y)) then
  156.               if (d.Y <= Max(q.Y, p.Y)) then
  157.                 if (d.X <= Max(q.X, p.X)) then
  158.                 begin
  159.                   if (q.y <> p.y) then
  160.                     e := ((d.Y - q.Y) * (p.X - q.X) / Extended((p.Y - q.Y)) + q.X);
  161.                   if ((q.X = p.X) or (d.X < e)) then
  162.                     f := not f;
  163.                 end;
  164.             q := p;
  165.           end;
  166.         end;
  167.         if f then
  168.         begin
  169.           l := Length(Result);
  170.           SetLength(Result, (l + 1));
  171.           Result[l] := Point((x + b.X1), (y + b.Y1));
  172.         end;
  173.       end;
  174.   end else
  175.     SetLength(Result, 0);
  176. end;
  177.  
  178. function RandomTPA(Amount:Integer; MinX,MinY,MaxX,MaxY:Integer): TPointArray;
  179. var
  180.   i: Integer;
  181. begin
  182.   SetLength(Result, (Amount + 1));
  183.   for i:=0 to Amount do
  184.     Result[i] := Point(RandomRange(MinX, MaxX), RandomRange(MinY, MaxY));
  185. end;
  186.  
  187. //******* Plot a few shapes using this algorithm *******//
  188. var
  189.   TPA,CTPA,OTPA: TPointArray;
  190.   bmp, t: Integer;
  191. begin
  192.   bmp := CreateBitmap(700, 700);
  193.  
  194.   TPA := RandomTPA(GENERATE, 100,100,595,595);
  195.   CTPA := CopyTPA(TPA);
  196.   OTPA := CopyTPA(TPA);
  197.   SetLength(TPA, 0);
  198.   t := GetSystemTime;
  199.   ConvexHull(CTPA);
  200.   WriteLn('ConvexHull took ' + IntToStr(GetSystemTime - t) + ' ms.');
  201.   t := GetSystemTime;
  202.   TPA := TPAFromPolygon(CTPA);
  203.   WriteLn('TPAFromPolygon took ' + IntToStr(GetSystemTime - t) + ' ms.');
  204.   DrawTPABitmap(bmp, TPA, 357435);
  205.   DrawTPABitmap(bmp, OTPA, 16777215);
  206.   DrawTPABitmap(bmp, CTPA, 255);
  207.  
  208.   DisplayDebugImgWindow(700,700);
  209.   DrawBitmapDebugImg(bmp);
  210.   FreeBitmap(bmp);
  211. end.
Advertisement
Add Comment
Please, Sign In to add comment