我做了一个小程序来近似圆周率,我想把它表示出来。我开始了,但我想如果圆圈外面的点是红色的,里面的点是绿色的,会看起来更好。我做不到,我也不明白为什么会有这样的问题。我不知道它是在纯数学中还是在开发中。
for(int i = 0; i<1000; i++) {
double x = Math.random() * (500-250) + 250;
double y = Math.random() * 250;
double applyformula = (x - 250)*(x - 250) + (y - 250) * (y - 250);
if(applyformula == y || (y*y) - 500 * y < applyformula ) {
g.fillOval((int) x,(int) y, 5, 5);
g.setColor(Color.GREEN);
} else {
g.fillOval((int) x,(int) y, 5, 5);
g.setColor(Color.RED);
}
}如果有人能帮我,那就太好了。
发布于 2021-06-05 01:12:04
这可以用一种更简单的方式来完成。
for (int i = 0; i < 1000; i++) {
double x = Math.random();
double y = Math.random();
double applyformula = (x * x) + (y * y);
if (applyformula <= 1) {
g.setColor(Color.GREEN);
} else {
g.setColor(Color.RED);
}
// Set the actual coordinates in a 250 pixels wide square.
x *= 250;
y *= 250;
g.fillOval((int) x, (int) y, 5, 5);
}https://stackoverflow.com/questions/67839284
复制相似问题