Beispiel #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));
        }
Beispiel #2
0
        internal Polygon AddVertex(Vertex vertex, Edge intersectinEdge)
        {
            //Assumes that vertex v intersects one of _polygonsDict Edges.
            Polygon newPolygon = (Polygon)this.Clone();

            // Assign the polygon Id to the new vertex.
            vertex.Parent = this;

            // Getting the index of the intersecting edge's start vertex and
            // inserting the new vertex at the following index.
            int index = newPolygon.vertices.IndexOf(intersectinEdge.StartVertex);

            newPolygon.vertices.Insert(index + 1, vertex);

            // Rebuilding Edges.
            newPolygon.edges.Clear();
            int verticesCount = newPolygon.vertices.Count;

            for (var i = 0; i < verticesCount; i++)
            {
                int nextIndex = (i + 1) % verticesCount;
                newPolygon.edges.Add(Edge.ByStartVertexEndVertex(newPolygon.vertices[i], newPolygon.vertices[nextIndex]));
            }

            return(newPolygon);
        }
Beispiel #3
0
        public Geometry Intersection(Edge edge)
        {
            // If no coplanar, cannot intersect
            if (!edge.IsCoplanarTo(this.Origin, this.Direction))
            {
                return(null);
            }

            // Intersection can be an Edge or null
            if (this.Direction.IsParallelTo(edge.Direction))
            {
                bool containsStart = this.Contains(edge.StartVertex);
                bool containsEnd   = this.Contains(edge.EndVertex);

                if (!containsStart && !containsEnd)
                {
                    return(null);
                }
                else if (containsStart)
                {
                    return(Edge.ByStartVertexEndVertex(edge.StartVertex, this.Origin));
                }
                else
                {
                    return(Edge.ByStartVertexEndVertex(this.Origin, edge.EndVertex));
                }
            }

            // No coincident nor same extremes
            var b   = this.Direction;
            var a   = edge.Direction;
            var c   = Vector.ByTwoVertices(edge.StartVertex, this.Origin);
            var cxb = c.Cross(b);
            var axb = a.Cross(b);
            var dot = cxb.Dot(axb);

            double t = (dot) / Math.Pow(axb.Length, 2);

            if (t < 0 && !t.AlmostEqualTo(0))
            {
                return(null);
            }
            if (t > 1 && !t.AlmostEqualTo(1))
            {
                return(null);
            }

            var intersection = edge.StartVertex.Translate(edge.Direction.Scale(t));

            return(this.Contains(intersection) ? intersection : null);
        }
Beispiel #4
0
        private Edge DiagonalByVertexIndex(int start, int end)
        {
            var vertexCount = this.Vertices.Count;

            if (start < 0 || start > vertexCount - 1)
            {
                throw new ArgumentOutOfRangeException("start", start, "Out of range");
            }
            if (end < 0 || end > vertexCount - 1)
            {
                throw new ArgumentOutOfRangeException("end", end, "Out of range");
            }

            return(Edge.ByStartVertexEndVertex(this.Vertices[start], this.Vertices[end]));
        }
Beispiel #5
0
        internal Polygon(List <Vertex> vertices, bool _isExternal = false) : base()
        {
            isBoundary = _isExternal;
            int vertexCount = vertices.Count;

            for (var j = 0; j < vertexCount; j++)
            {
                int    next_index  = (j + 1) % vertexCount;
                Vertex vertex      = vertices[j];
                Vertex next_vertex = vertices[next_index];
                var    edge        = Edge.ByStartVertexEndVertex(vertex, next_vertex);

                vertex.Parent = this;
                edge.Parent   = this;

                this.Vertices.Add(vertex);
                this.Edges.Add(edge);
            }
        }
Beispiel #6
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);
        }
Beispiel #7
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);
            }
        }
Beispiel #8
0
 internal static Edge ToEdge(this Line line)
 {
     return(Edge.ByStartVertexEndVertex(line.StartPoint.ToVertex(), line.EndPoint.ToVertex()));
 }
Beispiel #9
0
        public Geometry Intersection(Edge other)
        {
            // http://mathworld.wolfram.com/Line-LineIntersection.html
            if (!this.BoundingBox.Intersects(other.BoundingBox))
            {
                return(null);
            }
            if (!this.IsCoplanarTo(other))
            {
                return(null);
            }
            if (this.Equals(other))
            {
                return(this);
            }                                        // Issues if same polygon id???

            var a = this.Direction;
            var b = other.Direction;

            if (a.IsParallelTo(b))
            {
                // Fully contains the test edge
                if (other.StartVertex.OnEdge(this) && other.EndVertex.OnEdge(this))
                {
                    return(other);
                }
                // Is fully contained by test edge
                else if (this.StartVertex.OnEdge(other) && this.EndVertex.OnEdge(other))
                {
                    return(this);
                }
                // Not fully inclusive but overlapping
                else if (this.StartVertex.OnEdge(other) || this.EndVertex.OnEdge(other))
                {
                    Vertex[] vertices = new Vertex[4]
                    {
                        this.StartVertex,
                        this.EndVertex,
                        other.StartVertex,
                        other.EndVertex
                    };
                    var sorted = vertices.OrderBy(v => v.Y).ThenBy(v => v.X).ThenBy(v => v.Z).ToList();
                    return(Edge.ByStartVertexEndVertex(sorted[1], sorted[2]));
                }
                // Not intersecting
                else
                {
                    return(null);
                }
            }

            // No parallels but intersecting on one of the extreme Vertices
            if (other.Contains(this.StartVertex))
            {
                return(this.StartVertex);
            }
            else if (other.Contains(this.EndVertex))
            {
                return(this.EndVertex);
            }


            // No coincident nor same extremes
            var c   = Vector.ByTwoVertices(this.StartVertex, other.StartVertex);
            var cxb = c.Cross(b);
            var axb = a.Cross(b);
            var dot = cxb.Dot(axb);

            // If dot == 0 it means that other edge contains at least a vertex from this edge
            // and they are parallel or perpendicular. Cannot be parallel as that was tested before.
            // It might also mean they don't intersect but the would if extending the projections
            double s = (dot) / Math.Pow(axb.Length, 2);

            if (s.AlmostEqualTo(0))
            {
                if (this.StartVertex.OnEdge(other))
                {
                    return(this.StartVertex);
                }
                else if (this.EndVertex.OnEdge(other))
                {
                    return(this.EndVertex);
                }
                else if (other.StartVertex.OnEdge(this))
                {
                    return(other.StartVertex);
                }
                else if (other.EndVertex.OnEdge(this))
                {
                    return(other.EndVertex);
                }
                else
                {
                    return(null);
                }
            }



            // s > 1, means that "intersection" vertex is not on either edge
            // s == NaN means they are parallels so never intersect
            if (s < 0 || s > 1 || Double.IsNaN(s))
            {
                return(null);
            }

            Vertex intersection = this.StartVertex.Translate(a.Scale(s));

            if (intersection.Equals(other.StartVertex))
            {
                return(other.StartVertex);
            }
            if (intersection.Equals(other.EndVertex))
            {
                return(other.EndVertex);
            }
            if (!intersection.OnEdge(other))
            {
                return(null);
            }

            return(intersection);
        }