zoukankan      html  css  js  c++  java
  • 判断多边形是顺时针还是逆时针(C#实现)

    判断多边形是顺时针还是逆时针的方法

    1、关于如何判定多边形是顺时针还是逆时针对于凸多边形而言,只需对某一个点计算cross product = ((xi - xi-1),(yi - yi-1)) x ((xi+1 - xi),(yi+1 - yi)) 
    = (xi - xi-1) * (yi+1 - yi) - (yi - yi-1) * (xi+1 - xi)

    如果上式的值为正,逆时针;为负则是顺时针

    而对于一般的简单多边形,则需对于多边形的每一个点计算上述值,如果正值比较多,是逆时针;负值较多则为顺时针。


    2、还有一种说明是取多边形的极点值,多边形的方向和这个顶点与其相邻两边构成的方向相同。

    需要注意的是在屏幕坐标中,Y是向下的,所以在屏幕坐标系中看到的顺时针既是在Y轴向上的直角坐标系中看到的逆时针方向。

    原文参见http://debian.fmi.uni-sofia.bg/~sergei/cgsr/docs/clockwise.htm


    public class Polygon
        {
            #region CalculateClockDirection
            /// <summary>
            /// 判断多边形是顺时针还是逆时针.
            /// </summary>
            /// <param name="points">所有的点</param>
            /// <param name="isYAxixToDown">true:Y轴向下为正(屏幕坐标系),false:Y轴向上为正(一般的坐标系)</param>
            /// <returns></returns>
            public static ClockDirection CalculateClockDirection(List<PointF> points, bool isYAxixToDown)
            {
                int i, j, k;
                int count = 0;
                double z;
                int yTrans = isYAxixToDown ? (-1) : (1);
                if (points == null || points.Count < 3)
                {
                    return (0);
                }
                int n = points.Count;
                for (i = 0; i < n; i++)
                {
                    j = (i + 1) % n;
                    k = (i + 2) % n;
                    z = (points[j].X - points[i].X) * (points[k].Y * yTrans - points[j].Y * yTrans);
                    z -= (points[j].Y * yTrans - points[i].Y * yTrans) * (points[k].X - points[j].X);
                    if (z < 0)
                    {
                        count--;
                    }
                    else if (z > 0)
                    {
                        count++;
                    }
                }
                if (count > 0)
                {
                    return (ClockDirection.Counterclockwise);
                }
                else if (count < 0)
                {
                    return (ClockDirection.Clockwise);
                }
                else
                {
                    return (ClockDirection.None);
                }
            }
            #endregion
    
            #region CalculatePolygonType
            /// <summary>      
            ///判断多边形是凸多边形还是凹多边形.
            ///假定该多边形是简单的多边形,即没有横穿也没有洞的多边形。
            /// </summary>
            /// <param name="points"></param>
            /// <param name="isYAxixToDown">true:Y轴向下为正(屏幕坐标系),false:Y轴向上为正(一般的坐标系)</param>
            /// <returns></returns>
            public static PolygonType CalculatePolygonType(List<PointF> points, bool isYAxixToDown)
            {
                int i, j, k;
                int flag = 0;
                double z;
    
                if (points == null || points.Count < 3)
                {
                    return (0);
                }
                int n = points.Count;
                int yTrans = isYAxixToDown ? (-1) : (1);
                for (i = 0; i < n; i++)
                {
                    j = (i + 1) % n;
                    k = (i + 2) % n;
                    z = (points[j].X - points[i].X) * (points[k].Y * yTrans - points[j].Y * yTrans);
                    z -= (points[j].Y * yTrans - points[i].Y * yTrans) * (points[k].X - points[j].X);
                    if (z < 0)
                    {
                        flag |= 1;
                    }
                    else if (z > 0)
                    {
                        flag |= 2;
                    }
                    if (flag == 3)
                    {
                        return (PolygonType.Concave);
                    }
                }
                if (flag != 0)
                {
                    return (PolygonType.Convex);
                }
                else
                {
                    return (PolygonType.None);
                }
            }
            #endregion
        }
    
    /// <summary>
        /// 时钟方向
        /// </summary>
        public enum ClockDirection
        {
            /// <summary>
            /// 无.可能是不可计算的图形,比如多点共线
            /// </summary>
            None,
    
            /// <summary>
            /// 顺时针方向
            /// </summary>
            Clockwise,
    
            /// <summary>
            /// 逆时针方向
            /// </summary>
            Counterclockwise
        }
    
        public enum PolygonType
        {
            /// <summary>
            /// 无.不可计算的多边形(比如多点共线)
            /// </summary>
            None,
    
            /// <summary>
            /// 凸多边形
            /// </summary>
            Convex,
    
            /// <summary>
            /// 凹多边形
            /// </summary>
            Concave
    
        }
    

    附原文:

    The following describes a method for determining whether or not a polygon has its vertices ordered clockwise or anticlockwise. As a consequence the test can also be used to determine whether or not a polygon is concave or convex. A polygon will be assumed to be described by N vertices, ordered

    (x0,y0), (x1,y1), (x2,y2), . . . (xn-1,yn-1)

    A convenient definition of clockwise is based on considerations of the cross product between adjacent edges. If the crossproduct is positive then it rises above the plane (z axis up out of the plane) and if negative then the cross product is into the plane.

    cross product = ((xi - xi-1),(yi - yi-1)) x ((xi+1 - xi),(yi+1 - yi))

    = (xi - xi-1) * (yi+1 - yi) - (yi - yi-1) * (xi+1 - xi)

    If the polygon is known to be convex then one only has to consider the cross product between any two adjacent edges. A positive cross product means we have a counterclockwise polygon. There are some tests that may need to be done if the polygons may not be "clean". In particular two vertices must not be coincident and two edges must not be colinear.

    For the more general case where the polygons may be convex, it is necessary to consider the sign of the cross product between adjacent edges as one moves around the polygon. If there are more positive cross products then the overall polygon is ordered counterclockwise. There are pathological cases to consider here as well, all the edges cannot be colinear, there must be at least 3 vertices, the polygon must be simple, that is, it cannot intersect itself or have holes.

    Test for concave/convex polygon

    A similar argument to the above can be used to determine whether a polygon is concave or convex. For a convex polygon all the cross products of adjacent edges will be the same sign, a concave polygon will have a mixture of cross product signs.
  • 相关阅读:
    error: Microsoft Visual C++ 14.0 is required.
    pip安装其他包报错
    MapReduce
    机器学习算法使用
    结巴分词使用实例
    大数据——hbase
    机房收费系统系列一:运行时错误‘-2147217843(80040e4d)’;用户‘sa’登陆失败
    耿建玲视频总结
    学生信息管理系统系列三:验收时的改进
    学生信息管理系统系列二:常见问题
  • 原文地址:https://www.cnblogs.com/sparkleDai/p/7604954.html
Copyright © 2011-2022 走看看