public static float PlacePoint(Vector start, Vector end, Vector point) { float a = start.y - end.y; float b = -(start.x - end.x); float c = -(a * start.x + b * start.y); return a * point.x + b * point.y + c; }
public float PlaceOfPoint(Vector point) { float a = begin.Y - end.Y; float b = -(begin.X - end.X); float c = -(a * begin.X + b * begin.Y); return a * point.X + b * point.Y + c; }
public static Vector CrossingPoint(Vector a1, Vector a2, Vector b1, Vector b2) { float x = -((a1.x * a2.y - a2.x * a1.y) * (b2.x - b1.x) - (b1.x * b2.y - b2.x * b1.y) * (a2.x - a1.x)) / ((a1.y - a2.y) * (b2.x - b1.x) - (b1.y - b2.y) * (a2.x - a1.x)); if (x == float.NaN) return null; float y = ((b1.y - b2.y) * (-x) - (b1.x * b2.y - b2.x * b1.y)) / (b2.x - b1.x); return new Vector(x, y); }
public bool ContainPoint(Vector point) { float end = (new Vector(points.Last(), points.First())) * (new Vector(points.Last(), point)); for (int i = 0; i < points.Count-1; i++ ) { float step = (new Vector(points[i], points[i+1])) * (new Vector(points[i], point)); if (end * step < 0) return false; } return true; }
public static Polygon Intersection(Polygon a, Vector b1, Vector b2) { List<Vector> result = new List<Vector>(); a.points.Add(a.points.Last()); for (int i = 0; i < a.points.Count - 1; i++) { Vector start = a.points[i]; Vector end = a.points[i + 1]; bool s = Vector.PlacePoint(b1, b2, start) >= 0; bool e = Vector.PlacePoint(b1, b2, end) >= 0; if (s) result.Add(start); if (s ^ e) result.Add(Vector.CrossingPoint(start, end, b1, b2)); } return new Polygon(result); }
public Segment(Vector begin, Vector end) { this.begin = begin; this.end = end; }
public Vector(Vector a, Vector b) { this.x = b.x - a.x; this.y = b.y - a.y; }