public static double DistanceFrom(this Point p, LineSegment line)
 {
     var s = p.LerpProjectOnto(line);
     if (s < 0) return p.DistanceFrom(line.Start);
     if (s > 1) return p.DistanceFrom(line.End);
     return p.DistanceFrom(line.LerpAcross(s));
 }
Esempio n. 2
0
        /// <summary>
        /// Returns the point where a linear cut path intersects a line segment whose ends points are following linear paths.
        /// Returns null if there is no such point.
        /// Also returns the normalized 'direction' of the cut.
        /// </summary>
        public static Tuple <Point, Vector> TryGetCut(this LineSegment cutPath, LineSegment endPath1, LineSegment endPath2)
        {
            var e = new[] { endPath1, endPath2 }
            .Select(line => new LineSegment(line.Start, line.Delta - cutPath.Delta))
            .ToArray();
            var c = GeometryUtilities.LineDefinedByMovingEndPointsCrossesOrigin(e[0], e[1], origin: cutPath.Start);

            if (!c.HasValue)
            {
                return(null);
            }
            var p = cutPath.LerpAcross(c.Value.T);
            var v = cutPath.Delta - endPath1.Delta.LerpTo(endPath2.Delta, c.Value.S);

            return(Tuple.Create(p, v.Normal()));
        }
        public static IntersectionParameters? LineDefinedByMovingEndPointsCrossesOrigin(LineSegment endPath1,
                                                                            LineSegment endPath2,
                                                                            Point origin = default(Point)) {
            var b = endPath1.Start - origin;
            var d = endPath2.Start - endPath1.Start;
            var db = endPath1.Delta;
            var dd = endPath2.Delta - endPath1.Delta;

            return (from t in QuadraticRoots(dd.Cross(db),
                                             d.Cross(db) + dd.Cross(b),
                                             d.Cross(b))
                    where t >= 0 && t <= 1
                    let p0 = endPath1.LerpAcross(t)
                    let p1 = endPath2.LerpAcross(t)
                    let s = origin.LerpProjectOnto(p0.To(p1))
                    where s >= 0 && s <= 1
                    select (IntersectionParameters?)new IntersectionParameters {T = t, S = s}
                   ).FirstOrDefault();
        }
        public static IntersectionParameters?LineDefinedByMovingEndPointsCrossesOrigin(LineSegment endPath1,
                                                                                       LineSegment endPath2,
                                                                                       Point origin = default(Point))
        {
            var b  = endPath1.Start - origin;
            var d  = endPath2.Start - endPath1.Start;
            var db = endPath1.Delta;
            var dd = endPath2.Delta - endPath1.Delta;

            return((from t in QuadraticRoots(dd.Cross(db),
                                             d.Cross(db) + dd.Cross(b),
                                             d.Cross(b))
                    where t >= 0 && t <= 1
                    let p0 = endPath1.LerpAcross(t)
                             let p1 = endPath2.LerpAcross(t)
                                      let s = origin.LerpProjectOnto(p0.To(p1))
                                              where s >= 0 && s <= 1
                                              select(IntersectionParameters?) new IntersectionParameters {
                T = t, S = s
            }
                    ).FirstOrDefault());
        }