Exemplo n.º 1
0
        //returns 0 if false, +1 if true, -1 if pt ON polygon boundary
        public static int PointIsInside(this Polygon polygon, IntPoint testPoint, INearestNeighbours <int> nearestNeighbours = null)
        {
            if (polygon.FindPoint(testPoint, nearestNeighbours) != -1)
            {
                return(-1);
            }

            return(Clipper.PointInPolygon(testPoint, polygon));
        }
        private int FindClosestPoint(Polygon polygon,
                                     INearestNeighbours <int> accelerator,
                                     IntPoint currentPosition,
                                     bool doSeamHiding,
                                     SEAM_PLACEMENT seamPlacement,
                                     bool canTravelForwardOrBackward,
                                     int layerIndex,
                                     long lineWidth_um,
                                     out double bestDistSquared,
                                     out IntPoint endPosition)
        {
            int bestPoint;

            if (canTravelForwardOrBackward || polygon.Count == 2)
            {
                int endIndex = polygon.Count - 1;

                bestDistSquared = (polygon[0] - currentPosition).LengthSquared();
                bestPoint       = 0;
                endPosition     = polygon[endIndex];

                // check if the end is better
                double distSquared = (polygon[endIndex] - currentPosition).LengthSquared();
                if (distSquared < bestDistSquared)
                {
                    bestDistSquared = distSquared;
                    bestPoint       = endIndex;
                    endPosition     = polygon[0];
                }
            }
            else
            {
                if (doSeamHiding)
                {
                    bestPoint = polygon.FindGreatestTurnIndex(layerIndex, lineWidth_um, seamPlacement, currentPosition);
                }
                else
                {
                    bestPoint = polygon.FindClosestPositionIndex(currentPosition, accelerator);
                }

                bestDistSquared = (polygon[bestPoint] - currentPosition).LengthSquared();

                endPosition = polygon[bestPoint];
            }

            return(bestPoint);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Return the point index or -1 if not a vertex of the polygon
        /// </summary>
        /// <param name="polygon"></param>
        /// <param name="position"></param>
        /// <returns></returns>
        public static int FindPoint(this Polygon polygon, IntPoint position, INearestNeighbours <int> nearestNeighbours = null)
        {
            if (nearestNeighbours != null)
            {
                var index = nearestNeighbours.GetNearestNeighbour(position);
                if (position == polygon[index])
                {
                    return(index);
                }
            }
            else
            {
                for (int i = 0; i < polygon.Count; i++)
                {
                    if (position == polygon[i])
                    {
                        return(i);
                    }
                }
            }

            return(-1);
        }
Exemplo n.º 4
0
        public static int FindClosestPositionIndex(this Polygon polygon, IntPoint position, INearestNeighbours <int> nearestNeighbours = null)
        {
            if (nearestNeighbours != null)
            {
                return(nearestNeighbours.GetNearestNeighbour(position));
            }
            else
            {
                int    bestPointIndex = -1;
                double closestDist    = double.MaxValue;
                for (int pointIndex = 0; pointIndex < polygon.Count; pointIndex++)
                {
                    double dist = (polygon[pointIndex] - position).LengthSquared();
                    if (dist < closestDist)
                    {
                        bestPointIndex = pointIndex;
                        closestDist    = dist;
                    }
                }

                return(bestPointIndex);
            }
        }