public Pt p1, p2, vect; //vector p1 -> p2 #endregion Fields #region Constructors public Edge(Pt p1n, Pt p2n) { p1 = p1n; p2 = p2n; vect = p2 - p1; norm = vect.Norm(); }
public double Dist(Pt p) { //distance from p to the edge if (vect.Dot(p - p1) <= 0) return p.Dist(p1); //from p to p1 if (vect.Dot(p - p2) >= 0) return p.Dist(p2); //from p to p2 //distance to the line itself return Math.Abs(-vect.Y * p.X + vect.X * p.Y + p1.X * p2.Y - p1.Y * p2.X) / norm; }
/// <summary> /// Dot product of two vectors: O -> this and O -> other /// a dot b = |a| |b| cos(theta) = ax bx + ax by /// zero if two vectors run orthogonally /// </summary> public double Dot(Pt other) { return DoubleUtil.Add(this.X * other.X, this.Y * other.Y); }
static double get_vector_length(Pt v) { return Math.Pow((v.X * v.X) + (v.Y * v.Y), 0.5); }
/// <summary> /// Cross(det) product of two vectors: O -> this and O -> other /// a x b = |a| |b| sin(theta) = ax by - ay bx /// zero if two vectors run parallelly /// </summary> public double Cross(Pt other) { return DoubleUtil.Add(this.X * other.Y, -this.Y * other.X); }
public double Dist(Pt other) { return (this - other).Norm(); }
/// <summary> /// crosssing point of line p1-p2 and q1-q2 /// </summary> public static Pt Intersect(Pt p1, Pt p2, Pt q1, Pt q2) { return p1 + (p2 - p1) * ((q2 - q1).Cross(q1 - p1) / (q2 - q1).Cross(p2 - p1)); }
/// <summary> /// point q exists on line p1-p2? /// </summary> public static bool OnSeg(Pt p1, Pt p2, Pt q) { return (p1 - q).Cross(p2 - q) == 0 && (p1 - q).Dot(p2 - q) <= 0; }
public static bool HasIntersect(Pt p1, Pt p2, Pt q1, Pt q2) { if ((p1 - q1).Cross(p2 - q2) == 0) { return OnSeg(p1, q1, p2) || OnSeg(p1, q1, q2) || OnSeg(p2, q2, p1) || OnSeg(p2, q2, q1); } else { var r = Intersect(p1, q1, p2, q2); return OnSeg(p1, p2, r) && OnSeg(q1, q2, r); } }
//ger radian angle of vector A and B public static double GetAngleOf2Vectors(Pt A, Pt B) { double length_A = get_vector_length(A); double length_B = get_vector_length(B); double cos_sita = A.Dot(B) / (length_A * length_B); double sita = Math.Acos(cos_sita); //degree in 0^180 //sita = sita * 180.0 / PI; return sita; }
public static double Dist(Pt v1, Pt v2) { return v1.Dist(v2); }
public static int Ccw(Pt a, Pt b, Pt c) { b -= a; c -= a; if (b.Cross(c) > 0) return 1; //counter clockwise if (b.Cross(c) < 0) return -1; //clockwise if (b.Dot(c) < 0) return 2; //c--a--b on line if (b.Norm() < c.Norm()) return -2; //a--b--c on line return 0; }
public Circle(Pt p, double r) { P = p; R = r; }