public LineSegment(Point s, Point e) { this.PtS = s; this.PtE = e; this.Distance = GeometryFunc.EuclideanDistanceInMeter(this.PtS, this.PtE); this.DirAngle = GeometryFunc.ComputeAngle(this.PtS, this.PtE, true); }
/// <summary> /// Find the shortest distance between a point P and the line segment PtS to PtE /// 1. Equation of line segment: PP = PtS + u(PtE-PtS); 0 leq u leq 1 /// 2. The shortest distance occured at the tangent to the line seqment which passes through P3: // (P - PP) dot (PtE - PtS) = 0 /// 3. Solve for u by subsititude 1. into 2. /// 4. Use u to find PP /// 5. Find distance: distance = ||PP-P|| in meter /// Note: meter2pixel ratios are irrelevant to find the value u. /// </summary> /// <param name="p"></param> /// <param name="u">show where the tangent that depict the shortest distance lies on the line; // closer to 0 means near PtS, vice versa</param> public double FindShortestDist2Point(Point p, Double u) { u = ((p.x - this.PtS.x) * (this.PtE.x - this.PtS.x) + (p.y - this.PtS.y) * (this.PtE.y - this.PtS.y)) / (Math.pow(this.PtE.x - this.PtS.x, 2) + Math.pow(this.PtE.y - this.PtS.y, 2)); return GeometryFunc.EuclideanDistanceInMeter(p, this.GetPointFromEqn(u)); }