void CalcMinMax() { pmin = new Point2D(Math.Min(p1.x, p2.x) - Path2D.Epsilon, Math.Min(p1.y, p2.y) - Path2D.Epsilon); pmax = new Point2D(Math.Max(p1.x, p2.x) + Path2D.Epsilon, Math.Max(p1.y, p2.y) + Path2D.Epsilon); isValid = false; }
// generate a sigment from single line poly, using the normal to determine direction public Segment2D(Point2D p1, Point2D p2, double normx, double normy) { // the current segment is not longer matching // match curve direction based on normal such that the right side is inside the object. double curveDir = Math.Atan2(p2.y - p1.y, p2.x - p1.x); double normalDir = Math.Atan2(normy, normx); double dirDiff = curveDir - normalDir; if (dirDiff < 0) dirDiff += 2 * Math.PI; // handle negative result if ((dirDiff > 0) && (dirDiff < Math.PI)) { // reverse direction to match normal this.p1 = p2; this.p2 = p1; } else { this.p1 = p1; this.p2 = p2; } CalcMinMax(); }
// split a sigment at a given point, preserving direction public List<Segment2D> SplitAt(Point2D pt) { List<Segment2D> segments = new List<Segment2D>(); segments.Add(new Segment2D(p1, pt)); segments.Add(new Segment2D(pt, p2)); return segments; }
public Segment2D(Point2D p1, Point2D p2) { this.p1 = p1; this.p2 = p2; CalcMinMax(); }
public Segment2D(double x1, double y1, double x2, double y2) { p1 = new Point2D(x1, y1); p2 = new Point2D(x2, y2); CalcMinMax(); }
public bool PointInPoly(Point2D pt) { int i, j, nvert = nPoints; bool c = false; for (i = 0, j = nvert - 1; i < nvert; j = i++) { if (((points[i].y >= pt.y) != (points[j].y >= pt.y)) && (pt.x <= (points[j].x - points[i].x) * (pt.y - points[i].y) / (points[j].y - points[i].y) + points[i].x) ) c = !c; } return c; }
public void Add(Point2D p) { points.Add(p); }
public Point2D GetCreatePoint(double x, double y) { double key = x * x + y * y; double keyfrom = key * 0.99 - 2 * Epsilon; double keyto = key * 1.01 + 2 * Epsilon; int from = 0; int to = points.Count; int pos = 0; while ((to - from) > 1) { pos = (to + from) / 2; if (points[pos].key < keyfrom) from = pos; else to = pos; } while ((from < points.Count) && (points[from].key < keyto)) { if (points[from].Matches(x, y)) return points[from]; from++; } while ((pos < points.Count) && (points[pos].key < key)) pos++; /*for (int i = 0; i < points.Count; i++ ) if (points[i].Matches(x, y)) return points[i];*/ Point2D npt = new Point2D(x, y); npt.key = key; if (pos < points.Count) points.Insert(pos, npt); else points.Add(npt); if (minx > x) minx = x; if (maxx < x) maxx = x; if (miny > y) miny = y; if (maxy < y) maxy = y; return npt; }