Beispiel #1
0
        public static Point ClosestPointFromPointToPolygon(CustomPolygon polygon, Point point)
        {
            // we keep track of point with minimum distance
            Point? minPoint = null;
            double minDist  = double.MaxValue;

            List <CustomLine> polygonLines = polygon.GetPolygonLines();

            // for each line in polygon, find the perpendicular line from point
            for (int i = 0; i < polygonLines.Count(); i++)
            {
                CustomLine lineSegment       = polygonLines[i];
                double     slope             = lineSegment.getSlope();
                double     perpSlope         = CustomLine.getPerpendicularSlope(slope);
                CustomLine perpLine          = new CustomLine(point, perpSlope);
                Point?     intersectionPoint = CustomLine.getIntersectionCoordinates(lineSegment, perpLine);

                if (intersectionPoint.HasValue)
                {
                    double dist = Utilities.getDistance(intersectionPoint.Value, point);
                    // does this line intersect the polygon line segment?
                    // is the intersection point and the point a min distance
                    if (CustomLine.withinLineSegment(intersectionPoint.Value, lineSegment) &&
                        dist < minDist)
                    {
                        minDist  = dist;
                        minPoint = intersectionPoint;
                    }
                }
                else
                {
                    throw new Exception("Intersection Point is null even though it should have a coordinate");
                }
            }

            // If there is a minpoint we are good to go
            if (minPoint.HasValue)
            {
                return(minPoint.Value);
            }

            // or else we calculate the closest distance from the point to the polygon corners
            else
            {
                minDist = double.MaxValue;
                PointCollection points = polygon.GetPoints();
                for (int i = 0; i < points.Count; i++)
                {
                    double dist = Utilities.getDistance(points[i], point);
                    if (dist < minDist)
                    {
                        minDist  = dist;
                        minPoint = points[i];
                    }
                }
            }

            // If there is a minpoint we are good to go
            if (minPoint.HasValue)
            {
                return(minPoint.Value);
            }
            else
            {
                throw new Exception("Point with minimum distance is null even though it should have a coordinate");
            }
        }
Beispiel #2
0
        private List <Double> getExtendedDistanceList(PointCollection points, Point clickPoint)
        {
            List <Double> extendedDistances = new List <Double>();

            for (int i = 0; i < points.Count; i++)
            {
                Point objectivePoint = points[i];

                // get the line from objective point to the clickpoint
                CustomLine clickLine = new CustomLine(objectivePoint, clickPoint);

                Point?intersectionPoint = null;

                // we cycle through the polygon points 2 at a time, finding the polygon line
                // that intersects with the extended clickLine
                for (int j = 0; j < points.Count; j++)
                {
                    Point currentPoint = points[j];
                    Point nextPoint;

                    if ((j + 1) < points.Count)
                    {
                        nextPoint = points[j + 1];
                    }
                    else
                    {
                        // if the point is the last point, we cycle back to get the point in the 0th index
                        nextPoint = points[0];
                    }

                    CustomLine polygonLine = new CustomLine(currentPoint, nextPoint);

                    intersectionPoint = CustomLine.getIntersectionCoordinates(clickLine, polygonLine);

                    // Make sure the intersection point calculated is not the objectivePoint
                    if (intersectionPoint.HasValue &&

                        // this part is trick af because it is floating point calculation. Need some offset
                        (!Utilities.floatsEqual(objectivePoint.X, intersectionPoint.Value.X, Constants.offset) ||
                         !Utilities.floatsEqual(objectivePoint.Y, intersectionPoint.Value.Y, Constants.offset))

                        &&

                        CustomLine.withinLineSegment(intersectionPoint.Value, polygonLine))
                    {
                        // if we have discovered the point we break out of the for loop
                        Console.WriteLine("the valid intersection point is ({0}, {1})", intersectionPoint.Value.X, intersectionPoint.Value.Y);
                        break;
                    }
                }

                // Get distance between the polygon corner and the intersecting coordinate
                if (intersectionPoint.HasValue)
                {
                    double extendedDistance = Utilities.getDistance(intersectionPoint.Value, objectivePoint);
                    extendedDistances.Add(extendedDistance);
                }
                else
                {
                    // I should be getting some kind of intersection here if not
                    throw new Exception("Intersection Point is null even though it should have a coordinate");
                }
            }

            return(extendedDistances);
        }