Exemple #1
0
 /// <summary>
 /// Checks objects equality.
 /// Uses CompareDouble from RegularPolygonUtilityUtility class when comparing X and Y values.
 /// <see cref="RegularPolygonUtility.CompareDouble(double, double)"/>
 /// </summary>
 /// <param name="other">Vertex for comparison</param>
 /// <returns>True, if objects meet equality requirements. Otherwise, false.</returns>
 public bool Equals(Vertex other)
 {
     if (Utility.CompareDouble(X, other.X) && Utility.CompareDouble(Y, other.Y))
     {
         return(true);
     }
     else
     {
         return(false);
     }
 }
Exemple #2
0
        private bool ValidateVertices(Vertex[] vertices)
        {
            if (vertices == null)
            {
                throw new RegularPolygonException("Cannot create polygon without vertices.");
            }
            if (vertices.Length <= 2)
            {
                throw new RegularPolygonException($"Cannot create polygon with {vertices.Length} vertices, at least 3 are needed.");
            }

            // check if vertices are unique;
            for (int i = 0; i < vertices.Length; i++)
            {
                if (vertices.Count(v => v.Equals(vertices[i])) > 1) // if there are two equals vertices
                {
                    throw new RegularPolygonException("Cannot create polygon with duplicated vertices.");
                }
            }

            // check if sides lenghts are equal;
            // distance between first and last will be used as reference side length value for other vertices distances.
            double firstSideLength = vertices[0].GetDistanceFrom(vertices.Last());

            for (int i = 0; i < vertices.Length - 1; i++)
            {
                double currentSideLength = vertices[i].GetDistanceFrom(vertices[i + 1]);
                if (!Utility.CompareDouble(firstSideLength, currentSideLength))
                {
                    throw new RegularPolygonException("Cannot create regular polygon with unequal side lengths.");
                }
            }

            // check if vertices determine polygon that can be inscribed in a circle.;
            double avgX         = vertices.Average(v => v.X);
            double avgY         = vertices.Average(v => v.Y);
            Vertex circleCenter = new Vertex(avgX, avgY);
            double circleRadius = circleCenter.GetDistanceFrom(vertices[0]); // distance from first vertex as reference distance value

            for (int i = 1; i < vertices.Length; i++)
            {
                if (!Utility.CompareDouble(circleCenter.GetDistanceFrom(vertices[i]), circleRadius))
                {
                    throw new RegularPolygonException("Cannot create regular polygon that cannot be inscribed in circle.");
                }
            }

            return(true);
        }