/// <summary> /// This function will check if any vertex of a triangle is touching this triangle /// </summary> /// <param name="other">The <see cref="PTriangle"/> to check collision with</param> /// <returns>A bool indicating whether or not the <see cref="PTriangle"/>s are touching</returns> /// https://stackoverflow.com/questions/2778240/detection-of-triangle-collision-in-2d-space public bool IsTouching(PTriangle other) { bool inPointOne = ContainsPoint(other.VertexOne); bool inPointTwo = ContainsPoint(other.VertexTwo); bool inPointThree = ContainsPoint(other.VertexThree); bool inPointFour = other.ContainsPoint(this.VertexOne); bool inPointFive = other.ContainsPoint(this.VertexTwo); bool inPointSix = other.ContainsPoint(this.VertexThree); if (inPointOne || inPointTwo || inPointThree) { return(true); } if (inPointFour || inPointFive || inPointSix) { return(true); } return(false); }
/// <summary> /// Checks whether or not a given vertex is an ear of the given polygon /// </summary> /// <param name="vertices">Vertices composing the edges of the polygon</param> /// <param name="vert">The vertex to check</param> /// <param name="clockwise">Indicates which direction the vertices list rotates around the polygon</param> /// <returns>A bool indicating if the vertex is an ear</returns> private bool VertexIsEar(List <Vector2> vertices, int vert, bool clockwise) { if (vert < 0 || vert >= vertices.Count) { throw new ArgumentOutOfRangeException("Argument vert out of range"); } // Get vertices for the relevant triangle int vert1 = vertices.GetPrevIndex(vert); int vert2 = vert; int vert3 = vertices.GetNextIndex(vert); // Determine if the angle at vert is reflex float crossProd = Vector2.CrossProduct(vertices[vert1], vertices[vert2], vertices[vert3]); if ((crossProd > 0 && clockwise) || (crossProd < 0 && !clockwise)) { return(false); } // Check if any point is inside the triangle formed from this vertex PTriangle tri = new PTriangle(vertices[vert1], vertices[vert2], vertices[vert3]); for (int i = 0; i < vertices.Count; i++) { // No point checking the vertices of the current triangle if (i != vert1 && i != vert2 && i != vert3) { if (tri.ContainsPoint(vertices[i])) { return(false); } } } return(true); }