gpt4 book ai didi

java - Java 中的球体绘制

转载 作者:行者123 更新时间:2023-11-30 04:55:34 25 4
gpt4 key购买 nike

出于某种原因,当我尝试通过检查点的半径在 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...
}
}
}
}

最佳答案

假设你的球体的原点是(0,0,0),我认为你在那里有一个额外的平方根。

此外,乘以 X*X 的速度比 Math.pow(X,2) 快几倍...

我还将半径计算移到循环之外,并将其设置为像其余部分一样的 double,以防万一舍入误差会困扰您。

(您可以将 X++ 增量替换为 X += foo,以使此版本也可以使用更小或更大的步长。)

     double radius = diameter / 2;

for(double X = -radius; X < radius; X++ )
for(double Y = -radius; Y < radius; Y++ )
for(double Z = -radius; Z < radius; Z++ )
if(Math.sqrt((X * X) + (Y * Y) + (Z * Z)) <= radius)
SetInShere(X,Y,Z);

关于java - Java 中的球体绘制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8671385/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com