由于某些原因,当我尝试通过检查点的半径来制作Java中的球体时,它给了我一个立方体而不是球体。我的代码或我的公式存在问题吗?Java中的球体绘图
for(double X = 0; X < diameter; X++)
{
//mcspace.logger.info("X = " + Double.toString(X));
for(double Y = 0; Y < diameter; Y++)
{
//mcspace.logger.info("Y = " + Double.toString(Y));
for(double Z = 0; Z < diameter; Z++)
{
//mcspace.logger.info("Z = " + Double.toString(Z));
int radius = diameter/2;
double cX = X;
double cZ = Z;
double cY = Y;
if (X > radius){cX -= radius;}
if (Y > radius){cY -= radius;}
if (Z > radius){cZ -= radius;}
double Cr = Math.sqrt(Math.sqrt(Math.pow(cX,2) + Math.pow(cY,2)) + Math.pow(cZ,2));
if(Cr <= radius)
{
SetInShere(X,Y,Z);
// This is just a function that is in my code but the problem is that almost all the points are in the sphere, I almost always get a cube instead of a sphere...
}
}
}
}
我不我不明白这个问题。什么是“一个点的半径”?另外,尝试提供一个实际编译和运行的最小示例。没有这些,很难理解你的代码应该做什么。 – sleske 2011-12-29 18:37:14
移动int radius =直径/ 2;出于效率的原因,在你的循环之外。 And do double Cr = Math.hypot(Math.hypot(cX,cY),cZ); 这个更简单,更不容易出错,并且(除非我的数学学位是完全浪费时间)应该可以工作。 编辑:@BRPocock的想法更好 – 2012-01-03 00:12:54