public void CalcBounds() { if (Points == null || Points.Length == 0) { Bounds = RectangleD.Empty; } else { double l = Points[0].X, r = l; double t = Points[0].Y, b = t; for (int i = 1; i < Points.Length; i++) { if (Points[i].X < l) { l = Points[i].X; } else if (Points[i].X > r) { r = Points[i].X; } if (Points[i].Y < t) { t = Points[i].Y; } else if (Points[i].Y > b) { b = Points[i].Y; } } Bounds = RectangleD.FromLTRB(l, t, r, b); } }
public static RectangleD Intersect(RectangleD r1, RectangleD r2) { if (r1.Left > r2.Right || r1.Right < r2.Left || r1.Top > r2.Bottom || r1.Bottom < r2.Top) { return(RectangleD.Empty); } return(RectangleD.FromLTRB(Math.Max(r1.Left, r2.Left), Math.Max(r1.Top, r2.Top), Math.Min(r1.Right, r2.Right), Math.Min(r1.Bottom, r2.Bottom))); }
public static bool PtInRgn(double x, double y, PointD[] region) { if (region.Length == 0) { return(false); } double l = region[0].X; double r = l; double t = region[0].Y; double b = t; for (int i = 1; i < region.Length; i++) { if (region[i].X > r) { r = region[i].X; } else { if (region[i].X < l) { l = region[i].X; } } if (region[i].Y > b) { b = region[i].Y; } else { if (region[i].Y < t) { t = region[i].Y; } } } //RectangleD rd = new RectangleD(l, t, r - l, b - t); RectangleD rd = RectangleD.FromLTRB(l, t, r, b); if (!rd.Contains(x, y)) { return(false); } int count = 0; PointD start, end; for (int i = 0; i < region.Length; i++) { start = region[i]; end = (i == region.Length - 1) ? region[0] : region[i + 1]; if (start.X == x && start.Y == y) //是顶点 { return(true); } else { if (start.Y == y) //在X轴上 { if (start.X > x) //在正半轴 { if (end.Y == y) //边终点也在X轴上 { if (end.X < x) //点在边上 { return(true); } //否则该边可以当作一个顶点,忽略 } else { if (end.Y < y) { count++; } //左算又不算 } } else//在负半轴上 { if (end.Y == y && end.X > x) //点在边上 { return(true); } } } else { if (end.Y == y) //边终点在X轴上 { if (end.X > x && start.Y < y) { count++; } } else { if (start.Y < y && end.Y < y) { continue; } if (start.Y > y && end.Y > y) { continue; } if (start.X == end.X) { if (start.X > x) { count++; } } else { double x1 = (end.X * (start.Y - y) + start.X * (y - end.Y)) / (start.Y - end.Y); if (x1 == x) { return(true); } else if (x1 > x) { count++; } } } } } } return((count & 1) != 0); }