Janilabo

tpafrompolygon

Sep 18th, 2013
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 2.60 KB | None | 0 0
  1. {==============================================================================]
  2.   Returns polygon by given main points OR outlines (shape)
  3. [==============================================================================}
  4. function TPAFromPolygon(shape: TPointArray): TPointArray;
  5. var
  6.   b: TBox;
  7.   x, y, h, i, l, r, z: Integer;
  8.   o: TPointArray;
  9.   f: Boolean;
  10.   t: array of TBoolArray;
  11.   e: Extended;
  12.   q, p, d: TPoint;
  13. begin
  14.   h := High(shape);
  15.   if (h > -1) then
  16.   begin
  17.     SetLength(o, 0);
  18.     b := IntToBox(shape[0].X, shape[0].Y, shape[0].X, shape[0].Y);
  19.     for i := 0 to h do
  20.     begin
  21.       q := shape[i];
  22.       if (i < h) then
  23.         p := shape[(i + 1)]
  24.       else
  25.         p := shape[0];
  26.       r := Length(o);
  27.       if ((q.X <> p.X) or (q.Y <> p.Y)) then
  28.       begin
  29.         l := Max(Round(Abs(q.X - p.X)), Round(Abs(q.Y - p.Y)));
  30.         SetLength(o, ((r + l) + 1));
  31.         for z := 0 to l do
  32.           o[(r + z)] := Point((q.X + Round((p.X - q.X) * (z / Extended(l)))), (q.Y + Round((p.Y - q.Y) * (z / Extended(l)))));
  33.       end else
  34.       begin
  35.         SetLength(o, (r + 1));
  36.         o[r] := q;
  37.       end;
  38.       if (shape[i].X < b.X1) then
  39.         b.X1 := shape[i].X
  40.       else
  41.         if (shape[i].X > b.X2) then
  42.           b.X2 := shape[i].X;
  43.       if (shape[i].Y < b.Y1) then
  44.         b.Y1 := shape[i].Y
  45.       else
  46.         if (shape[i].Y > b.Y2) then
  47.           b.Y2 := shape[i].Y;
  48.     end;
  49.     SetLength(t, ((b.X2 - b.X1) + 1), ((b.Y2 - b.Y1) + 1));
  50.     l := Length(o);
  51.     for i := 0 to (l - 1) do
  52.       if not t[(o[i].X - b.X1)][(o[i].Y - b.Y1)] then
  53.         t[(o[i].X - b.X1)][(o[i].Y - b.Y1)] := True;
  54.     for y := 0 to (b.Y2 - b.Y1) do
  55.       for x := 0 to (b.X2 - b.X1) do
  56.       begin
  57.         f := t[x][y];
  58.         if not f then
  59.         begin
  60.           d := Point((x + b.X1), (y + b.Y1));
  61.           q := shape[0];
  62.           for i := 0 to (h + 1) do
  63.           begin
  64.             p := shape[(i mod (h + 1))];
  65.             if (d.Y > Min(q.Y, p.Y)) then
  66.               if (d.Y <= Max(q.Y, p.Y)) then
  67.                 if (d.X <= Max(q.X, p.X)) then
  68.                 begin
  69.                   if (q.y <> p.y) then
  70.                     e := ((d.Y - q.Y) * (p.X - q.X) / Extended((p.Y - q.Y)) + q.X);
  71.                   if ((q.X = p.X) or (d.X < e)) then
  72.                     f := not f;
  73.                 end;
  74.             q := p;
  75.           end;
  76.         end;
  77.         if f then
  78.         begin
  79.           l := Length(Result);
  80.           SetLength(Result, (l + 1));
  81.           Result[l] := Point((x + b.X1), (y + b.Y1));
  82.         end;
  83.       end;
  84.   end else
  85.     SetLength(Result, 0);
  86. end;
Advertisement
Add Comment
Please, Sign In to add comment