Exemplo n.º 1
0
        // Project a point to polyline.
        // point: (p), polyline: (pts)
        // output distance relative to the start point of the polyline: (distance)
        // output projection point: (outPnt)
        // return value:
        //      true: the point is projected on the polyline without extending the polyline
        //      false: the point is projected on the polyline through extending the polyline
        //
        public static bool ProjectPointToPolyline(IMapPoint p,
                                                  IPointCollection pts, ref double distance, ref IMapPoint outPnt)
        {
            distance = 0.0;

            double    outx = 0.0, outy = 0.0;
            IMapPoint p0, p1;

            for (int i = 0; i < pts.Count - 1; ++i)
            {
                p0 = pts[i];
                p1 = pts[i + 1];

                bool canProject = GeometryAlgorithms.ProjectPointToLine(p.X, p.Y,
                                                                        p0.X, p0.Y, p1.X, p1.Y, ref outx, ref outy);

                if (canProject == true)
                {
                    distance += GeometryAlgorithms.PointDistanceToPoint(outx, outy, p0.X, p0.Y);
                    outPnt    = Runtime.geometryEngine.newMapPoint(outx, outy);
                    return(true);
                }
                distance += GeometryAlgorithms.PointDistanceToPoint(p0.X, p0.Y, p1.X, p1.Y);
            }

            // Project the point by extending the polyline
            p0 = pts[0];
            p1 = pts[pts.Count - 1];
            double d0p = GeometryAlgorithms.PointDistanceToPoint(p.X, p.Y, p0.X, p0.Y);
            double d1p = GeometryAlgorithms.PointDistanceToPoint(p.X, p.Y, p1.X, p1.Y);

            if (d0p < d1p)
            {
                // the map point is closer to the beginning of the polyline,
                // then extend the beginning segment.
                p1 = pts[1];
                GeometryAlgorithms.ProjectPointToLine(p.X, p.Y,
                                                      p0.X, p0.Y, p1.X, p1.Y, ref outx, ref outy);
                distance  = GeometryAlgorithms.PointDistanceToPoint(outx, outy, p0.X, p0.Y);
                distance *= -1.0;
                outPnt    = Runtime.geometryEngine.newMapPoint(outx, outy);
            }
            else
            {
                // the map point is closer to the endding of the polyline,
                // since the loop is ended on the last segment, just use the result is OK.
                distance += GeometryAlgorithms.PointDistanceToPoint(outx, outy, p1.X, p1.Y);
                outPnt    = Runtime.geometryEngine.newMapPoint(outx, outy);
            }
            return(false);
        }
Exemplo n.º 2
0
 public static double Distance(IMapPoint p1, IMapPoint p2)
 {
     return(GeometryAlgorithms.PointDistanceToPoint(p1.X, p1.Y, p2.X, p2.Y));
 }