Beispiel #1
0
        public double DistToPoint(Point2D p, out Point2D footPos)
        {
            double  minDist  = this.points.Min(x => x.DistTo(p));
            Point2D minPoint = this.points.First(x => x.DistTo(p) == minDist);

            if (minPoint == this.points.First())
            {
                LineSeg seg1 = new LineSeg(this.points[0], this.points[1]);
                LineSegPointRelation relation1 = new LineSegPointRelation(seg1, p);
                if (relation1.Inner)
                {
                    footPos = relation1.Foot;
                    return(relation1.Dist);
                }
                else
                {
                    footPos = minPoint;
                    return(minDist);
                }
            }
            else if (minPoint == this.points.Last())
            {
                int     n    = this.points.Count;
                LineSeg seg1 = new LineSeg(this.points[n - 2], this.points[n - 1]);
                LineSegPointRelation relation1 = new LineSegPointRelation(seg1, p);
                if (relation1.Inner)
                {
                    footPos = relation1.Foot;
                    return(relation1.Dist);
                }
                else
                {
                    footPos = minPoint;
                    return(minDist);
                }
            }
            else
            {
                int     index = this.points.IndexOf(minPoint);
                LineSeg seg1  = new LineSeg(this.points[index - 1], this.points[index]);
                LineSeg seg2  = new LineSeg(this.points[index + 1], this.points[index]);
                LineSegPointRelation relation1 = new LineSegPointRelation(seg1, p);
                LineSegPointRelation relation2 = new LineSegPointRelation(seg2, p);
                if (relation1.Inner)
                {
                    footPos = relation1.Foot;
                    return(relation1.Dist);
                }
                else if (relation2.Inner)
                {
                    footPos = relation2.Foot;
                    return(relation2.Dist);
                }
                else
                {
                    footPos = minPoint;
                    return(minDist);
                }
            }
        }
Beispiel #2
0
        public static double DistToPoint(this PointString poly, Vector p, out Vector footPos)
        {
            var    points   = poly.Points;
            double minDist  = points.Min(x => x.Dist(p));
            var    minPoint = points.First(x => x.Dist(p) == minDist);

            if (minPoint.Equals(points.First()))
            {
                LineSeg seg1 = new LineSeg(points[0], points[1]);
                LineSegPointRelation relation1 = new LineSegPointRelation(seg1, p);
                if (relation1.Inner)
                {
                    footPos = relation1.Foot;
                    return(relation1.Dist);
                }
                else
                {
                    footPos = minPoint;
                    return(minDist);
                }
            }
            else if (minPoint.Equals(points.Last()))
            {
                int     n    = points.Count;
                LineSeg seg1 = new LineSeg(points[n - 2], points[n - 1]);
                LineSegPointRelation relation1 = new LineSegPointRelation(seg1, p);
                if (relation1.Inner)
                {
                    footPos = relation1.Foot;
                    return(relation1.Dist);
                }
                else
                {
                    footPos = minPoint;
                    return(minDist);
                }
            }
            else
            {
                int     index = points.IndexOf(minPoint);
                LineSeg seg1  = new LineSeg(points[index - 1], points[index]);
                LineSeg seg2  = new LineSeg(points[index + 1], points[index]);
                LineSegPointRelation relation1 = new LineSegPointRelation(seg1, p);
                LineSegPointRelation relation2 = new LineSegPointRelation(seg2, p);
                if (relation1.Inner)
                {
                    footPos = relation1.Foot;
                    return(relation1.Dist);
                }
                else if (relation2.Inner)
                {
                    footPos = relation1.Foot;
                    return(relation2.Dist);
                }
                else
                {
                    footPos = minPoint;
                    return(minDist);
                }
            }
        }