Beispiel #1
0
        public static Point?getIntersectionCoordinates(CustomLine l1, CustomLine l2)
        {
            Point intersectionPoint = new Point();

            // if parallel
            if (l1.getSlope() == l2.getSlope())
            {
                return(null);
            }

            // if either one is vertical (but not both because we would have returned null already)

            else if (l1.getIsVertical())
            {
                intersectionPoint.X = l1.getPoint1().X;
                Point l2Point = l2.getPoint1();
                intersectionPoint.Y = (intersectionPoint.X * l2.getSlope()) + l2.getConstant();
            }

            else if (l2.getIsVertical())
            {
                intersectionPoint.X = l2.getPoint1().X;
                Point l1Point = l1.getPoint1();
                intersectionPoint.Y = (intersectionPoint.X * l1.getSlope()) + l1.getConstant();
            }

            else
            {
                Point  l1Point = l1.getPoint1();
                Point  l2Point = l2.getPoint1();
                double a1      = l1Point.Y - l1.getSlope() * l1Point.X;
                double a2      = l2Point.Y - l2.getSlope() * l2Point.X;
                intersectionPoint.X = (a1 - a2) / (l2.getSlope() - l1.getSlope());
                intersectionPoint.Y = a2 + l2.getSlope() * intersectionPoint.X;
            }

            return(intersectionPoint);
        }
Beispiel #2
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");
            }
        }