//获取直线的最小包围盒(传入的点的坐标为屏幕坐标) public static Box2D GetLineMinBox(Vector2_DW screenPtSt, Vector2_DW screenPtEnd) { Box2D bxRet = new Box2D(); bxRet._min = Vector2_DW.Min(screenPtSt, screenPtEnd); bxRet._max = Vector2_DW.Max(screenPtSt, screenPtEnd); return(bxRet); }
//将屏幕Box转化为屏幕Rect public static Rectangle Box2Rect(Box2D bx) { Rectangle rectRet = new Rectangle(); rectRet.X = (int)bx._min.X; rectRet.Y = (int)bx._min.Y; rectRet.Width = (int)bx.Width; rectRet.Height = (int)bx.Height; return rectRet; }
//将屏幕Rect转化为屏幕Box public static Box2D Rect2Box(Rectangle rectScreen) { Box2D bxRet = new Box2D(); bxRet._min.X = rectScreen.X; bxRet._min.Y = rectScreen.Y; bxRet._max.X = rectScreen.Right; bxRet._max.Y = rectScreen.Bottom; return(bxRet); }
//将屏幕Box转化为屏幕Rect public static Rectangle Box2Rect(Box2D bx) { Rectangle rectRet = new Rectangle(); rectRet.X = (int)bx._min.X; rectRet.Y = (int)bx._min.Y; rectRet.Width = (int)bx.Width; rectRet.Height = (int)bx.Height; return(rectRet); }
static public bool BoxInRect(Rectangle rect, Box2D bx) { if (rect.Contains(new Point((int)bx._min.X, (int)bx._min.Y)) && rect.Contains(new Point((int)bx._max.X, (int)bx._max.Y))) { return(true); } else { return(false); } }
static public bool PtInBox(Box2D bx, Point posScreen)//判断某一点是否在包围盒内 { if (posScreen.X > bx._min.X && posScreen.X < bx._max.X && posScreen.Y > bx._min.Y && posScreen.Y < bx._max.Y) { return(true); } else { return(false); } }
//判断两个矩形是否相交(注:当一个矩形包围另一个矩形时,也认为是相交) public static bool BoxXBox(Box2D bx1, Box2D bx2) { Box2D intersectBx = new Box2D();//如果两个矩形相交,其相交区域必为矩形 intersectBx._min = Vector2_DW.Max(bx1._min, bx2._min); intersectBx._max = Vector2_DW.Min(bx1._max, bx2._max); if (intersectBx._min.X > intersectBx._max.X || intersectBx._min.Y > intersectBx._max.Y) { return(false); } else { return(true); } }
//获取points的最小包围盒和最左端的点 static public Box2D GetMinBox(Vector2_DW[] points, ref Vector2_DW leftMostPt) { Box2D bxRet = new Box2D(); int n = points.Length; double minX = 0; double minY = 0; double maxX = 0; double maxY = 0; int nLeftMostIndex = 0; for (int i = 0; i < n; i++) { if (0 == i) { minX = maxX = points[i].X; minY = maxY = points[i].Y; } else { if (points[i].X < minX) { minX = points[i].X; nLeftMostIndex = i; } if (points[i].Y < minY) { minY = points[i].Y; } if (points[i].X > maxX) { maxX = points[i].X; } if (points[i].Y > minY) { maxY = points[i].Y; } } } bxRet._min = new Vector2_DW(minX, minY); bxRet._max = new Vector2_DW(maxX, maxY); leftMostPt = new Vector2_DW(points[nLeftMostIndex].X, points[nLeftMostIndex].Y); return(bxRet); }
/// <summary> /// 判断直线段与矩形是否相交(注:当线段全部在矩形内,也认为相交) /// 1,线段端点是否在矩形内,在,true /// 2,线段包围框是否和矩形相交,否,false /// 3,矩形四个顶点是否在线段两侧,在true,不在,false /// </summary> /// <param name="lStScreen"></param> /// <param name="lEndScreen"></param> /// <param name="bx"></param> /// <returns></returns> public static bool LineSegmenXBox(Vector2_DW lStScreen, Vector2_DW lEndScreen, Box2D bx) { if (ToolsMath_DW.PtInBox(bx, lStScreen) && ToolsMath_DW.PtInBox(bx, lEndScreen)) { return(true); } Box2D bxLine = ToolsMath_DW.GetLineMinBox(lStScreen, lEndScreen); if (!ToolsMath_DW.BoxXBox(bx, bxLine)) { return(false); } Vector2_DW vecLn = lStScreen - lEndScreen; Vector2_DW vBxLeftUpper = bx._min - lEndScreen; Vector2_DW vBxRightTop = (new Vector2_DW(bx._max.X, bx._min.Y)) - lEndScreen; Vector2_DW vBxRightBtm = bx._max - lEndScreen; Vector2_DW vBxLeftBtm = (new Vector2_DW(bx._min.X, bx._max.Y)) - lEndScreen; double f1 = Vector2_DW.Cross(vecLn, vBxLeftUpper); double f2 = Vector2_DW.Cross(vecLn, vBxRightTop); double f3 = Vector2_DW.Cross(vecLn, vBxRightBtm); double f4 = Vector2_DW.Cross(vecLn, vBxLeftBtm); if ((f1 >= 0 && f2 >= 0 && f3 >= 0 && f4 >= 0) || (f1 <= 0 && f2 <= 0 && f3 <= 0 && f4 <= 0)) { return(false); } else { return(true); } }
/// <summary> /// 判断直线段与矩形是否相交(注:当线段全部在矩形内,也认为相交) /// 1,线段端点是否在矩形内,在,true /// 2,线段包围框是否和矩形相交,否,false /// 3,矩形四个顶点是否在线段两侧,在true,不在,false /// </summary> /// <param name="lStScreen"></param> /// <param name="lEndScreen"></param> /// <param name="bx"></param> /// <returns></returns> public static bool LineSegmenXBox(Vector2_DW lStScreen, Vector2_DW lEndScreen, Box2D bx) { if (ToolsMath_DW.PtInBox(bx, lStScreen) && ToolsMath_DW.PtInBox(bx, lEndScreen)) { return true; } Box2D bxLine = ToolsMath_DW.GetLineMinBox(lStScreen, lEndScreen); if (!ToolsMath_DW.BoxXBox(bx, bxLine)) { return false; } Vector2_DW vecLn = lStScreen - lEndScreen; Vector2_DW vBxLeftUpper = bx._min - lEndScreen; Vector2_DW vBxRightTop = (new Vector2_DW(bx._max.X, bx._min.Y)) - lEndScreen; Vector2_DW vBxRightBtm = bx._max - lEndScreen; Vector2_DW vBxLeftBtm = (new Vector2_DW(bx._min.X, bx._max.Y)) - lEndScreen; double f1 = Vector2_DW.Cross(vecLn, vBxLeftUpper); double f2 = Vector2_DW.Cross(vecLn, vBxRightTop); double f3 = Vector2_DW.Cross(vecLn, vBxRightBtm); double f4 = Vector2_DW.Cross(vecLn, vBxLeftBtm); if ((f1 >= 0 && f2 >= 0 && f3 >= 0 && f4 >= 0) || (f1 <= 0 && f2 <= 0 && f3 <= 0 && f4 <= 0)) { return false; } else { return true; } }
//将屏幕Rect转化为屏幕Box public static Box2D Rect2Box(Rectangle rectScreen) { Box2D bxRet = new Box2D(); bxRet._min.X = rectScreen.X; bxRet._min.Y = rectScreen.Y; bxRet._max.X = rectScreen.Right; bxRet._max.Y = rectScreen.Bottom; return bxRet; }
//获取points的最小包围盒和最左端的点 public static Box2D GetMinBox(Vector2_DW[] points, ref Vector2_DW leftMostPt) { Box2D bxRet = new Box2D(); int n = points.Length; double minX = 0; double minY = 0; double maxX = 0; double maxY = 0; int nLeftMostIndex = 0; for (int i = 0; i < n; i++) { if (0 == i) { minX = maxX = points[i].X; minY = maxY = points[i].Y; } else { if (points[i].X < minX) { minX = points[i].X; nLeftMostIndex = i; } if (points[i].Y < minY) { minY = points[i].Y; } if (points[i].X > maxX) { maxX = points[i].X; } if (points[i].Y > minY) { maxY = points[i].Y; } } } bxRet._min = new Vector2_DW(minX, minY); bxRet._max = new Vector2_DW(maxX, maxY); leftMostPt = new Vector2_DW(points[nLeftMostIndex].X, points[nLeftMostIndex].Y); return bxRet; }
//获取直线的最小包围盒(传入的点的坐标为屏幕坐标) public static Box2D GetLineMinBox(Vector2_DW screenPtSt, Vector2_DW screenPtEnd) { Box2D bxRet = new Box2D(); bxRet._min = Vector2_DW.Min(screenPtSt, screenPtEnd); bxRet._max = Vector2_DW.Max(screenPtSt, screenPtEnd); return bxRet; }
//判断两个矩形是否相交(注:当一个矩形包围另一个矩形时,也认为是相交) public static bool BoxXBox(Box2D bx1, Box2D bx2) { Box2D intersectBx = new Box2D();//如果两个矩形相交,其相交区域必为矩形 intersectBx._min = Vector2_DW.Max(bx1._min, bx2._min); intersectBx._max = Vector2_DW.Min(bx1._max, bx2._max); if (intersectBx._min.X > intersectBx._max.X || intersectBx._min.Y > intersectBx._max.Y) { return false; } else { return true; } }
public static bool BoxInRect(Rectangle rect, Box2D bx) { if (rect.Contains(new Point((int)bx._min.X, (int)bx._min.Y)) && rect.Contains(new Point((int)bx._max.X, (int)bx._max.Y))) { return true; } else { return false; } }
//判断某一点是否在包围盒内 public static bool PtInBox(Box2D bx, Point posScreen) { if (posScreen.X > bx._min.X && posScreen.X < bx._max.X && posScreen.Y > bx._min.Y && posScreen.Y < bx._max.Y) { return true; } else { return false; } }