public static BoundingArea GetBoundingArea(float x1, float y1, float x2, float y2) { #if !SIMPLE_BOUNDING_AREA if (x1 == x2 || y1 == y2) { return new BoundingArea(VisualizationUtils.CreateRectangle(x1, y1, x2, y2)); } float delta = Math.Abs((x2 - x1) / (y2 - y1)); float stepMax = (float)Math.Sqrt(mMaxBoxArea / delta + delta * mMaxBoxArea); Vector2DF line = new Vector2DF(x1, y1, x2, y2); float lineLen = line.GetLength(); if (stepMax >= lineLen) { return new BoundingArea(VisualizationUtils.CreateRectangle(x1, y1, x2, y2)); } BoundingArea boundingArea = new BoundingArea(); int steps = (int)Math.Ceiling(lineLen / stepMax); Vector2DF stepVec = line; stepVec.SetLength(lineLen / (float)steps); Vector2DF pt1 = new Vector2DF(x1, y1); Vector2DF pt2; for (int i = 0; i < steps - 1; i++) { pt2 = pt1 + stepVec; boundingArea.AddRectangles(VisualizationUtils.CreateRectangle(pt1.X, pt1.Y, pt2.X, pt2.Y)); pt1 = pt2; } pt2 = new Vector2DF(x2, y2); boundingArea.AddRectangles(VisualizationUtils.CreateRectangle(pt1.X, pt1.Y, pt2.X, pt2.Y)); return boundingArea; #else BoundingArea boundingArea = new BoundingArea(); boundingArea.AddRectangles(VisualizationUtils.CreateRectangle(x1, y1, x2, y2)); return boundingArea; #endif }
// *** Misc utils *** public static bool TestLineHit(Vector2DF testPt, Vector2DF lineTail, Vector2DF lineHead, float maxDist, ref float dist) { dist = float.MaxValue; if (lineTail != lineHead) { Vector2DF edge = lineHead - lineTail; Vector2DF edgeNormal = edge.Normal(); float intrsctX = 0, intrsctY = 0; float posA = 0, posB = 0; Vector2DF.Intersect(testPt, edgeNormal, lineTail, edge, ref intrsctX, ref intrsctY, ref posA, ref posB); if (posB >= 0f && posB <= 1f) { Vector2DF distVec = new Vector2DF(intrsctX, intrsctY) - testPt; dist = distVec.GetLength(); } dist = Math.Min((lineTail - testPt).GetLength(), dist); } dist = Math.Min((lineHead - testPt).GetLength(), dist); return dist <= maxDist; }