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);
}
C#中得到多个物体重心位置的方法
最新推荐文章于 2023-03-01 13:04:57 发布
这段代码展示了如何在C#中计算一组Vector3坐标数组的重心位置。通过遍历数组,找出每个维度的最小值和最大值,然后取平均值来确定重心坐标。
摘要由CSDN通过智能技术生成