Exemple #1
0
 public static double AngleVector0(
     double x1, double y1,
     double x2, double y2,
     double x3, double y3)
 {
     return(Atan2(CrossProduct3Vector2DTests.CrossProductVector2D(x1, y1, x2, y2, x3, y3), DotProduct3Vector2DTests.DotProductVector2D(x1, y1, x2, y2, x3, y3)));
 }
Exemple #2
0
        /// <summary>
        /// For each set of three adjacent points A, B, C,
        /// find the dot product AB · BC. If the sign of
        /// all the dot products is the same, the angles
        /// are all positive or negative (depending on the
        /// order in which we visit them) so the polygon
        /// is convex.
        /// </summary>
        /// <returns>
        /// Return true if the polygon is convex.
        /// </returns>
        /// <acknowledgment>
        /// http://csharphelper.com/blog/2014/07/determine-whether-a-polygon-is-convex-in-c/
        /// </acknowledgment>
        public static bool IsConvex(PolygonContour2D polygon)
        {
            var got_negative = false;
            var got_positive = false;
            var num_points = polygon.Points.Count;
            int B, C;

            for (var A = 0; A < num_points; A++)
            {
                B = (A + 1) % num_points;
                C = (B + 1) % num_points;

                var cross_product = CrossProduct3Vector2DTests.CrossProductVector2D(
                    polygon.Points[A].X, polygon.Points[A].Y,
                    polygon.Points[B].X, polygon.Points[B].Y,
                    polygon.Points[C].X, polygon.Points[C].Y);
                if (cross_product < 0)
                {
                    got_negative = true;
                }
                else
                {
                    got_positive |= cross_product > 0;
                }

                if (got_negative && got_positive)
                {
                    return(false);
                }
            }

            // If we got this far, the polygon is convex.
            return(true);
        }
Exemple #3
0
        public static double AngleVector1(
            double aX, double aY,
            double bX, double bY,
            double cX, double cY)
        {
            // Get the dot product.
            var dotProduct = DotProduct3Vector2DTests.DotProductVector2D(aX, aY, bX, bY, cX, cY);

            // Get the cross product.
            var crossProduct = CrossProduct3Vector2DTests.CrossProductVector2D(aX, aY, bX, bY, cX, cY);

            // Calculate the angle.
            return(Atan2(crossProduct, dotProduct));
        }