Example #1
0
        private bool SearchForOutstandingVertex(Vertices hullArea, out Vector2 outstanding)
        {
            Vector2 outstandingResult = Vector2.zero;
            bool    found             = false;

            if (hullArea.Count > 2)
            {
                int hullAreaLastPoint = hullArea.Count - 1;

                Vector2 tempVector1;
                Vector2 tempVector2 = hullArea[0];
                Vector2 tempVector3 = hullArea[hullAreaLastPoint];

                // Search between the first and last hull point.
                for (int i = 1; i < hullAreaLastPoint; i++)
                {
                    tempVector1 = hullArea[i];

                    // Check if the distance is over the one that's tolerable.
                    if (LineTools.DistanceBetweenPointAndLineSegment(ref tempVector1, ref tempVector2, ref tempVector3) >= _hullTolerance)
                    {
                        outstandingResult = hullArea[i];
                        found             = true;
                        break;
                    }
                }
            }

            outstanding = outstandingResult;
            return(found);
        }
Example #2
0
        private bool DistanceToHullAcceptable(Vertices polygon, Vector2 point, bool higherDetail)
        {
            if (polygon == null)
            {
                throw new ArgumentNullException("polygon", "'polygon' can't be null.");
            }

            if (polygon.Count < 3)
            {
                throw new ArgumentException("'polygon.Count' can't be less then 3.");
            }


            Vector2 edgeVertex2 = polygon[polygon.Count - 1];
            Vector2 edgeVertex1;

            if (higherDetail)
            {
                for (int i = 0; i < polygon.Count; i++)
                {
                    edgeVertex1 = polygon[i];

                    if (LineTools.DistanceBetweenPointAndLineSegment(ref point, ref edgeVertex1, ref edgeVertex2) <= _hullTolerance ||
                        LineTools.DistanceBetweenPointAndPoint(ref point, ref edgeVertex1) <= _hullTolerance)
                    {
                        return(false);
                    }

                    edgeVertex2 = polygon[i];
                }

                return(true);
            }
            else
            {
                for (int i = 0; i < polygon.Count; i++)
                {
                    edgeVertex1 = polygon[i];

                    if (LineTools.DistanceBetweenPointAndLineSegment(ref point, ref edgeVertex1, ref edgeVertex2) <= _hullTolerance)
                    {
                        return(false);
                    }

                    edgeVertex2 = polygon[i];
                }

                return(true);
            }
        }
        private static bool DistanceToHullAcceptable(PolygonCreationAssistance pca, Vertices polygon, Vector2 point,
                                                     bool higherDetail)
        {
            if (polygon != null && polygon.Count > 2)
            {
                Vector2 edgeVertex2 = polygon[polygon.Count - 1];

                Vector2 edgeVertex1;
                if (higherDetail)
                {
                    for (int i = 0; i < polygon.Count; i++)
                    {
                        edgeVertex1 = polygon[i];

                        if (LineTools.DistanceBetweenPointAndLineSegment(ref point, ref edgeVertex1, ref edgeVertex2) <=
                            pca.HullTolerance ||
                            LineTools.DistanceBetweenPointAndPoint(ref point, ref edgeVertex1) <= pca.HullTolerance)
                        {
                            return(false);
                        }

                        edgeVertex2 = polygon[i];
                    }

                    return(true);
                }
                else
                {
                    for (int i = 0; i < polygon.Count; i++)
                    {
                        edgeVertex1 = polygon[i];

                        if (LineTools.DistanceBetweenPointAndLineSegment(ref point, ref edgeVertex1, ref edgeVertex2) <=
                            pca.HullTolerance)
                        {
                            return(false);
                        }

                        edgeVertex2 = polygon[i];
                    }

                    return(true);
                }
            }

            return(false);
        }
Example #4
0
        private bool SplitPolygonEdge(Vertices polygon, Vector2 coordInsideThePolygon,
                                      out int vertex1Index, out int vertex2Index)
        {
            Vector2 slope;
            int     nearestEdgeVertex1Index = 0;
            int     nearestEdgeVertex2Index = 0;
            bool    edgeFound = false;

            float shortestDistance = float.MaxValue;

            bool    edgeCoordFound = false;
            Vector2 foundEdgeCoord = Vector2.zero;

            List <float> xCoords = SearchCrossingEdges(polygon, (int)coordInsideThePolygon.y);

            vertex1Index = 0;
            vertex2Index = 0;

            foundEdgeCoord.y = coordInsideThePolygon.y;

            if (xCoords != null && xCoords.Count > 1 && xCoords.Count % 2 == 0)
            {
                float distance;
                for (int i = 0; i < xCoords.Count; i++)
                {
                    if (xCoords[i] < coordInsideThePolygon.x)
                    {
                        distance = coordInsideThePolygon.x - xCoords[i];

                        if (distance < shortestDistance)
                        {
                            shortestDistance = distance;
                            foundEdgeCoord.x = xCoords[i];

                            edgeCoordFound = true;
                        }
                    }
                }

                if (edgeCoordFound)
                {
                    shortestDistance = float.MaxValue;

                    int edgeVertex2Index = polygon.Count - 1;

                    int edgeVertex1Index;
                    for (edgeVertex1Index = 0; edgeVertex1Index < polygon.Count; edgeVertex1Index++)
                    {
                        Vector2 tempVector1 = polygon[edgeVertex1Index];
                        Vector2 tempVector2 = polygon[edgeVertex2Index];
                        distance = LineTools.DistanceBetweenPointAndLineSegment(ref foundEdgeCoord,
                                                                                ref tempVector1, ref tempVector2);
                        if (distance < shortestDistance)
                        {
                            shortestDistance = distance;

                            nearestEdgeVertex1Index = edgeVertex1Index;
                            nearestEdgeVertex2Index = edgeVertex2Index;

                            edgeFound = true;
                        }

                        edgeVertex2Index = edgeVertex1Index;
                    }

                    if (edgeFound)
                    {
                        slope = polygon[nearestEdgeVertex2Index] - polygon[nearestEdgeVertex1Index];
                        slope.Normalize();

                        Vector2 tempVector = polygon[nearestEdgeVertex1Index];
                        distance = LineTools.DistanceBetweenPointAndPoint(ref tempVector, ref foundEdgeCoord);

                        vertex1Index = nearestEdgeVertex1Index;
                        vertex2Index = nearestEdgeVertex1Index + 1;

                        polygon.Insert(nearestEdgeVertex1Index, distance * slope + polygon[vertex1Index]);
                        polygon.Insert(nearestEdgeVertex1Index, distance * slope + polygon[vertex2Index]);

                        return(true);
                    }
                }
            }

            return(false);
        }
        private static bool SplitPolygonEdge(Vertices polygon, EdgeAlignment edgeAlign, Vector2 coordInsideThePolygon,
                                             out int vertex1Index, out int vertex2Index)
        {
            List <CrossingEdgeInfo> edges;

            Vector2 slope;
            int     nearestEdgeVertex1Index = 0;
            int     nearestEdgeVertex2Index = 0;
            bool    edgeFound = false;

            float shortestDistance = float.MaxValue;

            bool    edgeCoordFound = false;
            Vector2 foundEdgeCoord = Vector2.Zero;

            vertex1Index = 0;
            vertex2Index = 0;

            switch (edgeAlign)
            {
            case EdgeAlignment.Vertical:
                edges = GetCrossingEdges(polygon, EdgeAlignment.Vertical, (int)coordInsideThePolygon.Y);

                foundEdgeCoord.Y = coordInsideThePolygon.Y;

                if (edges != null && edges.Count > 1 && edges.Count % 2 == 0)
                {
                    float distance;
                    for (int i = 0; i < edges.Count; i++)
                    {
                        if (edges[i].CrossingPoint.X < coordInsideThePolygon.X)
                        {
                            distance = coordInsideThePolygon.X - edges[i].CrossingPoint.X;

                            if (distance < shortestDistance)
                            {
                                shortestDistance = distance;
                                foundEdgeCoord.X = edges[i].CrossingPoint.X;

                                edgeCoordFound = true;
                            }
                        }
                    }

                    if (edgeCoordFound)
                    {
                        shortestDistance = float.MaxValue;

                        int edgeVertex2Index = polygon.Count - 1;

                        int edgeVertex1Index;
                        for (edgeVertex1Index = 0; edgeVertex1Index < polygon.Count; edgeVertex1Index++)
                        {
                            Vector2 tempVector1 = polygon[edgeVertex1Index];
                            Vector2 tempVector2 = polygon[edgeVertex2Index];
                            distance = LineTools.DistanceBetweenPointAndLineSegment(ref foundEdgeCoord,
                                                                                    ref tempVector1, ref tempVector2);
                            if (distance < shortestDistance)
                            {
                                shortestDistance = distance;

                                nearestEdgeVertex1Index = edgeVertex1Index;
                                nearestEdgeVertex2Index = edgeVertex2Index;

                                edgeFound = true;
                            }

                            edgeVertex2Index = edgeVertex1Index;
                        }

                        if (edgeFound)
                        {
                            slope = polygon[nearestEdgeVertex2Index] - polygon[nearestEdgeVertex1Index];
                            slope.Normalize();

                            Vector2 tempVector = polygon[nearestEdgeVertex1Index];
                            distance = LineTools.DistanceBetweenPointAndPoint(ref tempVector, ref foundEdgeCoord);

                            vertex1Index = nearestEdgeVertex1Index;
                            vertex2Index = nearestEdgeVertex1Index + 1;

                            polygon.Insert(nearestEdgeVertex1Index, distance * slope + polygon[vertex1Index]);
                            polygon.Insert(nearestEdgeVertex1Index, distance * slope + polygon[vertex2Index]);

                            return(true);
                        }
                    }
                }
                break;

            case EdgeAlignment.Horizontal:
                throw new Exception("EdgeAlignment.Horizontal isn't implemented yet. Sorry.");
            }

            return(false);
        }