Advertisement
WarPie90

Expand Polygon

May 4th, 2016
348
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 1.46 KB | None | 0 0
  1. (*
  2.  A method to increase the size of a Polygon, without changing it's shape.
  3.  Can also be used to resize a rectangle without changing the aspect ratio.
  4.  
  5.  Note: Not designed to reduce the size of polygons, should work with rectangles tho.
  6. *)
  7. function ExpandPoly(Poly:TPointArray; inc:Int32): TPointArray;
  8. type
  9.   TPointF = record x,y:Double; end;
  10. var
  11.   i,j,k:Int32;
  12.   theta, det: Double;
  13.   c1,c2:array [0..2] of Double;
  14.   p1,q1,p2,q2: TPointF;
  15.   tmp:array of TPointF;
  16. begin
  17.   SetLength(Result, Length(Poly));
  18.   SetLength(tmp, Length(Poly)*2);
  19.   for i:=0 to High(Poly) do
  20.   begin
  21.     k := (i+1) mod Length(poly);
  22.     theta := ArcTan2(poly[i].Y - poly[k].Y, poly[i].X - poly[k].X) + PI/2;
  23.     tmp[i*2]  := TPointF([inc*Cos(theta)+poly[i].x, inc*Sin(theta)+poly[i].y]);
  24.     tmp[i*2+1]:= TPointF([inc*Cos(theta)+poly[k].x, inc*Sin(theta)+poly[k].y]);
  25.   end;
  26.  
  27.   for i:=0 to High(tmp) with 2 do
  28.   begin
  29.     p1 := tmp[i];
  30.     p2 := tmp[(i+1) mod Length(tmp)];
  31.     q1 := tmp[(i+2) mod Length(tmp)];
  32.     q2 := tmp[(i+3) mod Length(tmp)];
  33.  
  34.     c1 := [(p1.y-p2.y), (p2.x-p1.x), -(p1.x*p2.y-p2.x*p1.y)];
  35.     c2 := [(q1.y-q2.y), (q2.x-q1.x), -(q1.x*q2.y-q2.x*q1.y)];
  36.     det  := c1[0] * c2[1] - c1[1] * c2[0];
  37.     if (det <> 0) then
  38.     begin
  39.       Result[i div 2].x := Round((c1[2] * c2[1] - c1[1] * c2[2]) / det);
  40.       Result[i div 2].y := Round((c1[0] * c2[2] - c1[2] * c2[0]) / det);
  41.     end else
  42.       Result[i div 2] := Point(Round(p2.x), Round(p2.y));
  43.   end;
  44. end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement