/// <summary> /// Checks if vertices meet the logical requirements, if not throws exception. /// Logical requirements: /// 1. The Vertices are unique. (no duplicates) /// 2. Distances between any two pair of consecutive vertices are equal. /// 3. Center of the vertices (center of the circumsribed circle) is equidistant to all vertices /// </summary> /// <param name="vertices">Array of consecutive vertices</param> public void ValidateVertices(Vertex[] vertices) { if (vertices == null) { throw new RegularPolygonLogicalException("Regular polygon can not be created without providing vertices."); } if (vertices.Length < 3) { throw new RegularPolygonLogicalException($"Regular polygon has at least 3 vertices ({vertices.Length} < 3)."); } //checks vertices for uniqueness (no duplicates) foreach (Vertex vertex in vertices) { if (vertices.Count(v => v.X.Equals(vertex.X) && v.Y.Equals(vertex.X)) > 1) { throw new RegularPolygonLogicalException("Regular polygon can not consists of duplicate vertices."); } } //check vertices for side length equality double sideLength = vertices[0].CalculateLength(vertices[vertices.Length - 1]); for (int i = 0; i < vertices.Length - 1; i++) { if (!Utility.CompareDouble(vertices[i].CalculateLength(vertices[i + 1]), sideLength)) { throw new RegularPolygonLogicalException("Regular polygon must have all sides the same length."); } } //checks if the center of vertices (center of circumsribed circle) is equidistant to all vertices double avgX = vertices.Average(v => v.X); double avgY = vertices.Average(v => v.Y); Vertex circleCenter = new Vertex(avgX, avgY); double circleRadius = vertices[0].CalculateLength(circleCenter); for (int i = 1; i < vertices.Length; i++) { if (!Utility.CompareDouble(vertices[i].CalculateLength(circleCenter), circleRadius)) { throw new RegularPolygonLogicalException( "The circle must be circumscribed on the regular polygon."); } } }
/// <summary> /// Indicates whether the current object is equal to another object of the same type. Uses PolygonLibraryUtility. /// </summary> /// <param name="other">An object to compare with this object.</param> /// <see cref="PolygonLibrary.PolygonLibraryUtility.CompareDouble"/> /// <returns> /// <see langword="true" /> if the current object is equal to the <paramref name="other" /> parameter; otherwise, <see langword="false" />. /// </returns> public bool Equals(Vertex other) { return(Utility.CompareDouble(X, other.X) && Utility.CompareDouble(Y, other.Y)); }