internal bool CheckXYInBoxBounds(int boxnum, Point p) { // Since this method is called by many other methods that take params // from e.g. script opcodes, but do not validate the boxnum, we // make a check here to filter out invalid boxes. // See also bug #1599113. if (boxnum < 0 || boxnum == InvalidBox) return false; var box = GetBoxCoordinates(boxnum); // Quick check: If the x (resp. y) coordinate of the point is // strictly smaller (bigger) than the x (y) coordinates of all // corners of the quadrangle, then it certainly is *not* contained // inside the quadrangle. if (p.X < box.UpperLeft.X && p.X < box.UpperRight.X && p.X < box.LowerRight.X && p.X < box.LowerLeft.X) return false; if (p.X > box.UpperLeft.X && p.X > box.UpperRight.X && p.X > box.LowerRight.X && p.X > box.LowerLeft.X) return false; if (p.Y < box.UpperLeft.Y && p.Y < box.UpperRight.Y && p.Y < box.LowerRight.Y && p.Y < box.LowerLeft.Y) return false; if (p.Y > box.UpperLeft.Y && p.Y > box.UpperRight.Y && p.Y > box.LowerRight.Y && p.Y > box.LowerLeft.Y) return false; // Corner case: If the box is a simple line segment, we consider the // point to be contained "in" (or rather, lying on) the line if it // is very close to its projection to the line segment. if ((box.UpperLeft == box.UpperRight && box.LowerRight == box.LowerLeft) || (box.UpperLeft == box.LowerLeft && box.UpperRight == box.LowerRight)) { Point tmp; tmp = ScummMath.ClosestPtOnLine(box.UpperLeft, box.LowerRight, p); if (p.SquareDistance(tmp) <= 4) return true; } // Finally, fall back to the classic algorithm to compute containment // in a convex polygon: For each (oriented) side of the polygon // (quadrangle in this case), compute whether p is "left" or "right" // from it. if (!ScummMath.CompareSlope(box.UpperLeft, box.UpperRight, p)) return false; if (!ScummMath.CompareSlope(box.UpperRight, box.LowerRight, p)) return false; if (!ScummMath.CompareSlope(box.LowerRight, box.LowerLeft, p)) return false; if (!ScummMath.CompareSlope(box.LowerLeft, box.UpperLeft, p)) return false; return true; }
public static uint GetClosestPtOnBox(BoxCoords box, Point pIn, out Point pOut) { Point tmp; uint dist; uint bestdist = 0xFFFFFF; pOut = new Point(); tmp = ScummMath.ClosestPtOnLine(box.UpperLeft, box.UpperRight, pIn); dist = pIn.SquareDistance(tmp); if (dist < bestdist) { bestdist = dist; pOut = tmp; } tmp = ScummMath.ClosestPtOnLine(box.UpperRight, box.LowerRight, pIn); dist = pIn.SquareDistance(tmp); if (dist < bestdist) { bestdist = dist; pOut = tmp; } tmp = ScummMath.ClosestPtOnLine(box.LowerRight, box.LowerLeft, pIn); dist = pIn.SquareDistance(tmp); if (dist < bestdist) { bestdist = dist; pOut = tmp; } tmp = ScummMath.ClosestPtOnLine(box.LowerLeft, box.UpperLeft, pIn); dist = pIn.SquareDistance(tmp); if (dist < bestdist) { bestdist = dist; pOut = tmp; } return bestdist; }