Example #1
0
        public int[] getClosestIntersectingPointsToX1Y1OfLineSegment(int x1, int y1, int x2, int y2)
        {
            double minDistatnce = 999999999;

            int[]        closestIntersect      = new int[] { -1, -1 };
            List <int[]> allIntersectingPoints = getAllIntersectionPointsOfLineSegment(x1, y1, x2, y2);

            foreach (int[] xy in allIntersectingPoints)
            {
                double distance = LineSegment.getLength(x1, y1, xy[0], xy[1]);
                if (distance < minDistatnce)
                {
                    minDistatnce        = distance;
                    closestIntersect[0] = xy[0];
                    closestIntersect[1] = xy[1];
                }
            }
            return(closestIntersect);
        }
Example #2
0
        public int[] getClosestPointsOnPolygonToAPoint(int x, int y, out LineSegment closestEdge, out int closestEdgeStartingPointIndex)
        {
            double closestDistance = double.MaxValue;

            int[] closestPointOnPolygonBoundary = new int[] { -1, -1 };
            closestEdge = new LineSegment(-1, -1, -1, -1);
            closestEdgeStartingPointIndex = -1;

            for (int i = 0; i < vertices.Count; i++)
            {
                int xx1 = vertices[i][0];
                int yy1 = vertices[i][1];

                int         j     = (i + 1) % vertices.Count;
                int         xx2   = vertices[j][0];
                int         yy2   = vertices[j][1];
                LineSegment line  = new LineSegment(xx1, yy1, xx2, yy2);
                double      slope = line.slope();
                double      perpendicularSlope = 0;
                if (!double.IsInfinity(slope))
                {
                    perpendicularSlope = -1 / slope;
                }

                // find the perpendicular line that cross (x,y) in image.
                // assume the line is y=ax+b, a= slope, b = y-ax, b=px0[1]
                //double xstart = 0;
                //double ystart = (y - perpendicularSlope * (double)x);
                //double xend = -(ystart / perpendicularSlope);
                //double yend = 0;

                double xstart = 0;
                double ystart = 0;
                double xend   = 0;
                double yend   = 0;

                double   b   = (y - perpendicularSlope * (double)x);
                double[] px0 = new double[] { 0, b };
                double[] py0 = new double[] { -(b / perpendicularSlope), 0 };
                // assume width height less than 10000
                int      max   = 100000;
                double[] pxmax = new double[] { max, perpendicularSlope *max + b };
                double[] pymax = new double[] { -1, -1 };
                if (perpendicularSlope == 0)
                {
                    pymax[0] = max - 1;
                    pymax[1] = b;
                }
                else
                {
                    pymax[0] = (max - b) / perpendicularSlope;
                    pymax[1] = max;
                }

                // select the 2 far ends
                List <double[]> plist = new List <double[]>()
                {
                    px0, py0, pxmax, pymax
                };
                double minX = double.MaxValue;
                double maxX = double.MinValue;
                foreach (double[] p in plist)
                {
                    if (p[0] < minX)
                    {
                        minX   = p[0];
                        xstart = minX;
                        ystart = p[1];
                    }
                    if (p[0] > maxX)
                    {
                        maxX = p[0];
                        xend = maxX;
                        yend = p[1];
                    }
                }


                // find out whether it intersect, if intersect, since perpendicular, the intersects is the closest distance to that edge
                double[]        intersect       = new double[] { -1, -1 };
                List <double[]> candidatePoints = new List <double[]>();
                if (LineSegment.Intersect(xstart, ystart, xend, yend, xx1, yy1, xx2, yy2, out intersect))
                {
                    candidatePoints.Add(intersect);
                }
                else
                {
                    //"Not Intersecting", then the closest point must be one of the 2 ends.
                    candidatePoints.Add(new double[] { xx1, yy1 });
                    candidatePoints.Add(new double[] { xx2, yy2 });
                }
                foreach (double[] p in candidatePoints)
                {
                    double distance = LineSegment.getLength(x, y, (int)p[0], (int)p[1]);
                    if (distance < closestDistance)
                    {
                        closestDistance = distance;
                        closestPointOnPolygonBoundary[0] = (int)p[0];
                        closestPointOnPolygonBoundary[1] = (int)p[1];
                        closestEdge = line;
                        closestEdgeStartingPointIndex = i;
                    }
                }
            }
            return(closestPointOnPolygonBoundary);
        }