public class Pond {
public static void allcreationco(){
for (int i = 0; i < 100; i++){
int radius = 100;
int x = (int) (Math.random() * 2 * radius - radius);
int ylim = (int) Math.sqrt(radius * radius - x * x);
int y = (int) (Math.random() * 2 * ylim - ylim);
Fish.xfishc.add((int) x);
Fish.yfishc.add((int) y);
}
allcreationdir();
}
public static void allcreationdir(){
for (int i = 0; i < Fish.xfishc.size(); i++){
double radius = Math.random()*1;
double angle = Math.random()*2*Math.PI;
double x = Math.cos(angle)*radius + 0;
if (x > 0){
Fish.xfishcb1.add(true);
}
else {
Fish.xfishcb1.add(false);
}
}
for (int i = 0; i < Fish.yfishc.size(); i++){
double radius = Math.random()*1;
double angle = Math.random()*2*Math.PI;
double x = Math.cos(angle)*radius + 0;
if (x > 0){
Fish.yfishcb1.add(true);
}
else {
Fish.yfishcb1.add(false);
}
}
嗨,我的目标是创建一个圆形池塘的模拟(不需要视觉绘图,只需轻松打印信息),鱼在其中随机游动。上面的代码是一种将 100 条假设的鱼以基于半径为 100 的假设圆的 x 和 y 坐标的形式启动到 Arraylist 中的方法(必须有更好的方法来做到这一点)。我想让这 100 条鱼中的每一条都能够向随机方向游动,并在每次到达池塘尽头时改变到新的随机方向。也许我希望它们在一定时间后繁殖,或者包括另一条直线移动并可以吃掉另一条鱼的鱼。
public class Salmon extends Fish {
public static int salmonre = 0;
public static void salmonmove(){
for (int i = 0; i < xfishc.size(); i++){
if (xfishcb1.get(i) == true){
int d = xfishc.get(i);
int e = d + 1;
xfishc.set(i , e);
}
else{
int d = xfishc.get(i);
int e = d - 1;
xfishc.set(i , e);
}
}
for (int i = 0; i < yfishc.size(); i++){
if (yfishcb1.get(i) == true){
int d = yfishc.get(i);
int e = d + 1;
yfishc.set(i , e);
}
else{
int d = yfishc.get(i);
int e = d - 1;
yfishc.set(i , e);
}
}
salmonre++;
}
}
我还使用 boolean 数组列表来随机确定鱼应该移动的方向。请在你的言辞中温柔地对待我,因为我很清楚我的方法是荒谬的。我知道在模拟圆形中的对象及其行为时最好使用三角学,但出于某种原因,我在互联网上查找时无法理解这一点(我不断发现涉及视觉插图的更复杂的事情) 。您能“请”给我全面的答案和想法吗?我很沮丧。
最佳答案
我无法完全弄清楚您希望 Fish 类如何根据您的代码工作,但有一些提示:
在面向对象编程中,您不希望 Fish 类具有静态方法来更新两个包含所有鱼的 X 和 Y 坐标的列表。
相反,您需要 Fish 类的对象来表示有关单条鱼的所有内容。然后您可以获得 Fish 对象的列表。
一对 boolean 值对于方向来说确实太粗糙了。请改用
double
,每条鱼一个(存储在 Fish 实例中)。要实现方向改变行为,只需检查下一步 Action 是否会将鱼移出水面,如果是,则选择不同的方向。
这是一个简单的、独立的示例,适用于两条鱼。它们一起出发,朝同一方向,但当它们到达边缘时,它们会分道扬镳,朝不同的随机方向游动:
class Fish {
private double x, y;
private double angle, speed;
public Fish() {
x = y = angle = 0;
speed = 5;
}
void move() {
// If we swim at this angle, this is where we'll end up
double newX = x + Math.cos(angle) * speed;
double newY = y + Math.sin(angle) * speed;
if (isInPond(newX, newY)) {
// That's still in the pond, go there
x = newX;
y = newY;
} else {
// That's outside the pond, change direction
angle = Math.random() * 2 * Math.PI;
}
}
public String toString() {
return String.format(
"Position: %.0f,%.0f. Angle: %.0f degrees.",
x, y, angle * 180/Math.PI);
}
// Check whether some coordinates are within a circular pond with radius 100
static boolean isInPond(double x, double y) {
return Math.sqrt(x*x+y*y) < 100;
}
public static void main(String[] args) throws Exception {
Fish nemo = new Fish();
Fish marlin = new Fish();
while(true) {
nemo.move();
marlin.move();
System.out.println("Nemo: " + nemo);
System.out.println("Marlin: " + marlin);
Thread.sleep(500);
}
}
}
关于java - 模拟圆形池塘中游泳的物体(如鱼)(Java 中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29737200/