• unity 凸多形顶点排序


    /// <summary>
    /// 多边形点集排序
    /// </summary>
    /// <param name="vPoints"></param>
    /// <returns></returns>
    public List<Point> SortPolyPoints(List<Point> vPoints)
    {
        if (vPoints == null || vPoints.Count == 0) return null;
        //计算重心
        Point center = new Point();
        double X = 0, Y = 0;
        for (int i = 0; i < vPoints.Count; i++)
        {
            X += vPoints[i].X;
            Y += vPoints[i].Y;
        }
        center = new Point((int)X / vPoints.Count, (int)Y / vPoints.Count, vPoints[0].Z);
        //冒泡排序
        for (int i = 0; i < vPoints.Count - 1; i++)
        {
            for (int j = 0; j < vPoints.Count - i - 1; j++)
            {
                if (PointCmp(vPoints[j], vPoints[j + 1], center))
                {
                    Point tmp = vPoints[j];
                    vPoints[j] = vPoints[j + 1];
                    vPoints[j + 1] = tmp;
                }
            }
        }
        return vPoints;
    }
    
    /// <summary>
    /// 若点a大于点b,即点a在点b顺时针方向,返回true,否则返回false
    /// </summary>
    /// <param name="a"></param>
    /// <param name="b"></param>
    /// <param name="center"></param>
    /// <returns></returns>
    private bool PointCmp(Point a, Point b, Point center)
    {
        if (a.X >= 0 && b.X < 0)
            return true;
        else if (a.X == 0 && b.X == 0)
            return a.Y > b.Y;
        //向量OA和向量OB的叉积
        double det = (a.X - center.X) * (b.Y - center.Y) - (b.X - center.X) * (a.Y - center.Y);
        if (det < 0)
            return true;
        if (det > 0)
            return false;
        //向量OA和向量OB共线,以距离判断大小
        double d1 = (a.X - center.X) * (a.X - center.X) + (a.Y - center.Y) * (a.Y - center.Y);
        double d2 = (b.X - center.X) * (b.X - center.X) + (b.Y - center.Y) * (b.Y - center.Y);
        return d1 > d2;
    }
    
  • 相关阅读:
    7、cad图纸打印
    对账
    练习10—去掉图片文字
    6、cad里面如何快速将图纸的线条全部变成黑色
    5、cad如何快速选中图纸上所有文字
    4、如何修改cad的背景色为白色
    练习九—快速修饰面部光影
    iOS alloc&init探索
    export default / export const
    Vue2.0+组件库总结
  • 原文地址:https://www.cnblogs.com/kingBook/p/14421611.html
Copyright © 2020-2023  润新知