Janilabo

DistanceToBox

Aug 25th, 2013
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 0.44 KB | None | 0 0
  1. function DistanceToBox(pt: TPoint; bx: TBox): Extended;
  2. var
  3.   p: TPoint;
  4. begin
  5.   if not ((pt.X >= bx.X1) and (pt.X <= bx.X2) and (pt.Y >= bx.Y1) and (pt.Y <= bx.Y2)) then
  6.   begin
  7.     p := Point(Max((bx.X1 - pt.X), (pt.X - bx.X2)), Max((bx.Y1 - pt.Y), (pt.Y - bx.Y2)));
  8.     if (p.X < 0) then
  9.       p.X := 0;
  10.     if (p.Y < 0) then
  11.       p.Y := 0;
  12.     Result := Sqrt(Sqr(pt.X - p.X) + Sqr(pt.Y - p.Y));
  13.   end else
  14.     Result := 0.0;
  15. end;
Advertisement
Add Comment
Please, Sign In to add comment