public static Between ( Vector2F v, Vector2F w ) : float | ||
v | Vector2F | First vector. |
w | Vector2F | Second vector. |
리턴 | float |
/// <summary> /// Orthogonally projects <paramref name="v" /> onto a straight line /// parallel to <paramref name="w" />. /// </summary> /// <remarks> /// See https://en.wikipedia.org/wiki/Vector_projection for details. /// </remarks> /// <param name="v">Vector to project.</param> /// <param name="w">Vector to project onto.</param> /// <returns>Projected vector.</returns> public static Vector2F Project(Vector2F v, Vector2F w) { return(v.Length * Angle.Cos(Angle.Between(v, w)) * w.Normalize()); }
/// <summary> /// Decomposes this polygon into triangles. /// </summary> /// <remarks> /// See http://www.geometrictools.com/Documentation/TriangulationByEarClipping.pdf for details. /// </remarks> /// <returns>Triangles composing this polygon.</returns> public List <Polygon2F> Triangulate() { var triangles = new List <Polygon2F>(); var vertices = new List <Vector2F>(this.points); if (this.IsClockwise()) { vertices.Reverse(); } // Remove ears one by one. var index = 0; while (vertices.Count > 3) { var remainingPolygon = new Polygon2F(vertices); // Get three consecutive vertices. var u = vertices[(index - 1 + vertices.Count) % vertices.Count]; var v = vertices[index % vertices.Count]; var w = vertices[(index + 1) % vertices.Count]; // Check if middle vertex is convex. var vu = u - v; var vw = w - v; var angle = Angle.Between(vw, vu); var convex = angle < Math.PI; if (!convex) { ++index; continue; } // Check if line segment lies completely inside the polygon. var uw = new LineSegment2F(u, w); if (!remainingPolygon.Contains(uw)) { ++index; continue; } // Check if no other vertices of the polygon are contained in the triangle. var triangle = new Polygon2F(new[] { new Vector2F(u.X, u.Y), new Vector2F(v.X, v.Y), new Vector2F(w.X, w.Y) }); var containsOtherVertex = vertices.Where(other => other != u && other != v && other != w) .Any(other => triangle.Contains(other)); if (containsOtherVertex) { ++index; continue; } // Remove ear. vertices.Remove(v); triangles.Add(triangle); } // Add last triangle. triangles.Add(new Polygon2F(vertices)); return(triangles); }