Esempio n. 1
0
        private ClosePolygonResult FindPolygonPointClosestTo(IntPoint input)
        {
            ClosePolygonResult ret = new ClosePolygonResult();

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

                    //Q = A + Normal( B - A ) * ((( B - A ) dot ( P - A )) / VSize( A - B ));
                    IntPoint pDiff      = p1 - p0;
                    long     lineLength = (pDiff).Length();
                    if (lineLength > 1)
                    {
                        long distOnLine = (pDiff).Dot(input - p0) / lineLength;
                        if (distOnLine >= 0 && distOnLine <= lineLength)
                        {
                            IntPoint q = p0 + pDiff * distOnLine / lineLength;
                            if ((q - input).ShorterThen(100))
                            {
                                ret.intersectionPoint = q;
                                ret.polygonIdx        = n;
                                ret.pointIdx          = i;
                                return(ret);
                            }
                        }
                    }
                    p0 = p1;
                }
            }
            ret.polygonIdx = -1;
            return(ret);
        }
Esempio n. 2
0
        private GapCloserResult FindPolygonGapCloser(IntPoint ip0, IntPoint ip1)
        {
            GapCloserResult    ret = new GapCloserResult();
            ClosePolygonResult c1  = FindPolygonPointClosestTo(ip0);
            ClosePolygonResult c2  = FindPolygonPointClosestTo(ip1);

            if (c1.polygonIdx < 0 || c1.polygonIdx != c2.polygonIdx)
            {
                ret.len = -1;
                return(ret);
            }
            ret.polygonIndex = c1.polygonIdx;
            ret.pointIndexA  = c1.pointIdx;
            ret.pointIndexB  = c2.pointIdx;
            ret.AtoB         = true;

            if (ret.pointIndexA == ret.pointIndexB)
            {
                //Connection points are on the same line segment.
                ret.len = (ip0 - ip1).Length();
            }
            else
            {
                //Find out if we have should go from A to B or the other way around.
                IntPoint p0   = PolygonList[ret.polygonIndex][ret.pointIndexA];
                long     lenA = (p0 - ip0).Length();
                for (int i = ret.pointIndexA; i != ret.pointIndexB; i = (i + 1) % PolygonList[ret.polygonIndex].Count)
                {
                    IntPoint p1 = PolygonList[ret.polygonIndex][i];
                    lenA += (p0 - p1).Length();
                    p0    = p1;
                }
                lenA += (p0 - ip1).Length();

                p0 = PolygonList[ret.polygonIndex][ret.pointIndexB];
                long lenB = (p0 - ip1).Length();
                for (int i = ret.pointIndexB; i != ret.pointIndexA; i = (i + 1) % PolygonList[ret.polygonIndex].Count)
                {
                    IntPoint p1 = PolygonList[ret.polygonIndex][i];
                    lenB += (p0 - p1).Length();
                    p0    = p1;
                }
                lenB += (p0 - ip0).Length();

                if (lenA < lenB)
                {
                    ret.AtoB = true;
                    ret.len  = lenA;
                }
                else
                {
                    ret.AtoB = false;
                    ret.len  = lenB;
                }
            }
            return(ret);
        }
Esempio n. 3
0
		private ClosePolygonResult FindPolygonPointClosestTo(IntPoint input)
		{
			ClosePolygonResult ret = new ClosePolygonResult();
			for (int n = 0; n < PolygonList.Count; n++)
			{
				IntPoint p0 = PolygonList[n][PolygonList[n].Count - 1];
				for (int i = 0; i < PolygonList[n].Count; i++)
				{
					IntPoint p1 = PolygonList[n][i];

					//Q = A + Normal( B - A ) * ((( B - A ) dot ( P - A )) / VSize( A - B ));
					IntPoint pDiff = p1 - p0;
					long lineLength = (pDiff).Length();
					if (lineLength > 1)
					{
						long distOnLine = (pDiff).Dot(input - p0) / lineLength;
						if (distOnLine >= 0 && distOnLine <= lineLength)
						{
							IntPoint q = p0 + pDiff * distOnLine / lineLength;
							if ((q - input).ShorterThen(100))
							{
								ret.intersectionPoint = q;
								ret.polygonIdx = n;
								ret.pointIdx = i;
								return ret;
							}
						}
					}
					p0 = p1;
				}
			}
			ret.polygonIdx = -1;
			return ret;
		}