Advertisement
WarPie90

LineIntersection + InnerBox

May 31st, 2014
413
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 2.16 KB | None | 0 0
  1. {$I SimbaExt/SimbaExt.simba}
  2.  
  3. {$DEFINE NICEDEBUG}
  4. type TLine = record A,B:TPoint; end;
  5. var bmp:Integer;
  6.  
  7.  
  8. function FindIntersection(L1,L2:TLine): TPoint;
  9. var
  10.   D,dx,dy:Extended;
  11.   ax1,ax2,bx1,bx2,ay1,ay2,by1,by2:Int32;
  12. begin
  13.   ax1 := L1.a.x;  ax2 := L1.b.x;
  14.   bx1 := L2.a.x;  bx2 := L2.b.x;
  15.   ay1 := L1.a.y;  ay2 := L1.b.y;
  16.   by1 := L2.a.y;  by2 := L2.b.y;
  17.  
  18.   D := (ax1-ax2)*(by1-by2) - (ay1-ay2)*(bx1-bx2);
  19.   if D <> 0 then
  20.   begin
  21.     dx := ((bx1-bx2)*(ax1*ay2-ay1*ax2)-(ax1-ax2)*(bx1*by2-by1*bx2))/d;
  22.     dy := ((by1-by2)*(ax1*ay2-ay1*ax2)-(ay1-ay2)*(bx1*by2-by1*bx2))/d;
  23.     Result.x := Round(Dx);
  24.     Result.y := Round(Dy);
  25.   end else
  26.     RaiseException('No Intersection');
  27. end;
  28.  
  29.  
  30. function InnerBox(Rect:TPointArray): TBox;
  31. var
  32.   B:TBox; {$IFDEF NICEDEBUG} l:TPointArray; {$ENDIF}
  33.   upper,lower:TPoint;
  34. begin
  35.   B := Rect.Bounds();
  36.   {$IFDEF NICEDEBUG}
  37.   se.TPALine(l,B.ToCoords()[2],B.ToCoords()[0]);
  38.   se.TPALine(l,B.ToCoords()[3],B.ToCoords()[1]);
  39.   DrawTPABitmap(bmp, l, $008800);
  40.   DrawTPABitmap(bmp, EdgeFromBox(B), $555555);
  41.   {$ENDIF}
  42.  
  43.   if (Rect[1].distanceTo(Rect[2]) > Rect[2].distanceTo(Rect[3])) then
  44.   begin
  45.     upper := FindIntersection([Rect[1],Rect[2]], [B.ToCoords()[0],Rect.Mean()]);
  46.     lower := FindIntersection([Rect[3],Rect[0]], [B.ToCoords()[2],Rect.Mean()]);
  47.     Result := [Upper.x+1,Upper.y+1,Lower.x-1,Lower.y-1];
  48.   end else begin
  49.     upper := FindIntersection([Rect[0],Rect[1]], [B.ToCoords()[1],Rect.Mean()]);
  50.     lower := FindIntersection([Rect[2],Rect[3]], [B.ToCoords()[3],Rect.Mean()]);
  51.     Result := [Lower.x+1,Upper.y+1,Upper.x-1,Lower.y-1];
  52.   end;
  53.  
  54.   {$IFDEF NICEDEBUG}
  55.   DrawTPABitmap(bmp, se.TPACross(upper,4), $FFFF00);
  56.   DrawTPABitmap(bmp, se.TPACross(lower,4), $00FFFF);
  57.   {$ENDIF}
  58. end;
  59.  
  60.  
  61.  
  62. var i: Integer;
  63.     tpa: TPointArray;
  64.     B:TBox;
  65. begin
  66.   bmp := CreateBitmap(600, 600);
  67.   TPA := FindTPAEdges(TPAFromBox([50,50, Random(100,220),Random(100,220)]));
  68.   TPA := TPA.Rotate(Random(360));
  69.  
  70.   B := InnerBox(TPA.BoundingBox());
  71.  
  72.   DrawTPABitmap(bmp, tpa, 255);
  73.   DrawTPABitmap(bmp, EdgeFromBox(B), $FFFFFF);
  74.   DisplayDebugImgWindow(300, 300);
  75.   DrawBitmapDebugImg(bmp);
  76.   FreeBitmap(bmp);
  77. end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement