由於某些原因,當我嘗試通過檢查點的半徑來製作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