C#中得到多个物体重心位置的方法

    public Vector3 GetCenterOfGravity(Vector3[] vect3s)
    {
        Vector3 gravityCenter = new Vector3(0, 0, 0);

        float minx = vect3s[0].x;
        float maxx = vect3s[0].x;
        float miny = vect3s[0].y;
        float maxy = vect3s[0].y;
        float minz = vect3s[0].z;
        float maxz = vect3s[0].z;

        for (int i = 0; i < vect3s.Length; i++)
        {
            if (minx > vect3s[i].x)
            {
                minx = vect3s[i].x;
            }
            if (maxx < vect3s[i].x)
            {
                maxx = vect3s[i].x;
            }
            if (miny > vect3s[i].y)
            {
                miny = vect3s[i].y;
            }
            if (maxy < vect3s[i].y)
            {
                maxy = vect3s[i].y;
            }
            if (minz > vect3s[i].z)
            {
                minz = vect3s[i].z;
            }
            if (maxz < vect3s[i].z)
            {
                maxz = vect3s[i].z;
            }
        }
        return gravityCenter = new Vector3((minx + maxx) / 2, (miny + maxy) / 2, (minz + maxz) / 2);
    }
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页