示例#1
0
        internal static bool DoesIntersect(Line line, DSPoint point)
        {
            Edge   edge   = Edge.ByStartVertexEndVertex(Points.ToVertex(line.StartPoint), Points.ToVertex(line.EndPoint));
            Vertex vertex = Points.ToVertex(point);

            return(vertex.OnEdge(edge));
        }
示例#2
0
        internal static void AddColouredEdge(IRenderPackage package, Edge edge, DSCore.Color color)
        {
            package.AddLineStripVertex(edge.StartVertex.X, edge.StartVertex.Y, edge.StartVertex.Z);
            package.AddLineStripVertex(edge.EndVertex.X, edge.EndVertex.Y, edge.EndVertex.Z);

            package.AddLineStripVertexColor(color.Red, color.Green, color.Blue, color.Alpha);
            package.AddLineStripVertexColor(color.Red, color.Green, color.Blue, color.Alpha);

            package.AddLineStripVertexCount(2);
        }
示例#3
0
        /// <summary>
        /// Creates a new Graph by a set of lines.
        /// </summary>
        /// <param name="lines">Lines</param>
        /// <returns name="baseGraph">Base Graph</returns>
        public static BaseGraph ByLines(List <Line> lines)
        {
            if (lines == null)
            {
                throw new NullReferenceException("lines");
            }
            BaseGraph g = new BaseGraph()
            {
                graph = new Graphical.Graphs.Graph()
            };

            foreach (Line line in lines)
            {
                Vertex start = Geometry.Points.ToVertex(line.StartPoint);
                Vertex end   = Geometry.Points.ToVertex(line.EndPoint);
                g.graph.AddEdge(Edge.ByStartVertexEndVertex(start, end));
            }
            return(g);
        }
示例#4
0
        internal static bool DoesIntersect(Line line1, Line line2)
        {
            Edge edge1 = Edge.ByStartVertexEndVertex(Points.ToVertex(line1.StartPoint), Points.ToVertex(line1.EndPoint));
            Edge edge2 = Edge.ByStartVertexEndVertex(Points.ToVertex(line2.StartPoint), Points.ToVertex(line2.EndPoint));

            if (edge1.Intersects(edge2))
            {
                if (edge2.StartVertex.OnEdge(edge1))
                {
                    return(false);
                }
                if (edge2.EndVertex.OnEdge(edge1))
                {
                    return(false);
                }
                return(true);
            }
            else
            {
                return(false);
            }
        }
示例#5
0
        public List <Geometry> Intersection(Edge edge)
        {
            // No fast algorithm yet to calculate intersection on concave polygons
            if (!this.IsConvex())
            {
                return(this.IntersectionNaive(edge));
            }

            //https://stackoverflow.com/questions/4497841/asymptotically-optimal-algorithm-to-compute-if-a-line-intersects-a-convex-polygo

            List <Geometry> intersections = new List <Geometry>();

            if (!this.BoundingBox.Intersects(edge.BoundingBox))
            {
                return(intersections);
            }

            var  vertexCount      = this.Vertices.Count;
            int  midIndex         = (int)(vertexCount / 2);
            Edge diagonal         = this.DiagonalByVertexIndex(0, midIndex);
            int  polygonDirection = this.Vertices[1].IsLeftFrom(diagonal);

            Geometry intersection = diagonal.Intersection(edge);

            while (intersection == null)
            {
                // If midIndex is any neighbour from the start vertex
                // means the whole line is to one side or the other and doesn't intersect.
                if (midIndex == 1 || midIndex == vertexCount - 1)
                {
                    return(intersections);
                }

                int startSide = edge.StartVertex.IsLeftFrom(diagonal);
                int endSide   = edge.EndVertex.IsLeftFrom(diagonal);

                // If same side, don't intersect
                if (startSide == polygonDirection && endSide == polygonDirection)
                {
                    return(intersections);
                }
                // Is on other side from the polygonDirection (Vertices[1])
                else if (startSide != polygonDirection)
                {
                    midIndex += (int)((vertexCount - midIndex) / 2);
                }
                else
                {
                    midIndex = (int)(midIndex / 2);
                }

                diagonal     = this.DiagonalByVertexIndex(0, midIndex);
                intersection = edge.Intersection(diagonal);
            }

            // If intersection is an Edge
            if (intersection is Edge edgeIntersection)
            {
                // If diagonal other edge is any neighbour, intersection is on one of the sides
                if (midIndex == 1 || midIndex == vertexCount - 1)
                {
                    intersections.Add(edgeIntersection);
                    return(intersections);
                }

                // If not, the intersection can pass through both diagonal extremes, so its external.
                // Through just on, so only one intersection, or non and endge is inside polygon.

                if (this.Vertices.First().OnEdge(edgeIntersection))
                {
                    intersections.Add(this.Vertices.First());
                }
                if (this.Vertices[midIndex].OnEdge(edgeIntersection))
                {
                    intersections.Add(this.Vertices[midIndex]);
                }

                return(intersections);
            }

            // If intersection is a Vertex
            if (intersection is Vertex vertexIntersection)
            {
                if (vertexIntersection.Equals(this.Vertices.First()))
                {
                    throw new NotImplementedException();
                }

                if (vertexIntersection.Equals(this.Vertices[midIndex]))
                {
                    throw new NotImplementedException();
                }

                // Else the intersection is between the diagonal's extremes
                // find intersection at each side of the mid vertex

                // Going from midVertex to 0
                for (int i = midIndex; i > 0; i--)
                {
                    var    side             = DiagonalByVertexIndex(i, i - 1);
                    Vertex sideIntersection = edge.Intersection(side) as Vertex;
                    if (sideIntersection != null)
                    {
                        intersections.Add(sideIntersection);
                        break;
                    }
                }

                for (int j = midIndex; j < vertexCount; j++)
                {
                    int    next             = (j + 1) % vertexCount;
                    var    side             = DiagonalByVertexIndex(j, next);
                    Vertex sideIntersection = edge.Intersection(side) as Vertex;
                    if (sideIntersection != null)
                    {
                        intersections.Add(sideIntersection);
                        break;
                    }
                }

                return(intersections);
            }

            return(intersections);
        }
示例#6
0
 internal static Edge ToEdge(this Line line)
 {
     return(Edge.ByStartVertexEndVertex(line.StartPoint.ToVertex(), line.EndPoint.ToVertex()));
 }