如果两个圆在处理中相交,我该如何检查?
How do i check, if two Circles are intersecting in Processing?
所以我目前正在学习 Java,并且我努力让我的代码正常工作。
我制作了一个 "Fun" 代码,其中弹出了一些圆圈,它计算了屏幕上方有多少个圆圈。 (我知道,一些愚蠢的代码。)
我在 "Processing" 环境中编码,语言是 Java.
这是我的主要文件:
Circle[] circles = new Circle[50];
int index = 0;
boolean finished = false;
void setup() {
size(900, 900);
background(0);
for(int i = 0; i < circles.length; i++) {
circles[i] = new Circle();
}
if(finished = true) {
}
}
void draw() {
if(index < circles.length) {
circles[index].show(circles);
index++;
} else {
finished = true;
}
}
void count(Circle[] arr) {
int n = 0;
for(Circle c : arr) {
if(c.getY() > height / 2) {
n++;
}
}
println(n);
}
这是 "Problem" 圈 class:
class Circle {
private int x, y;
private float r = random(10, 25);
Circle() {
this.x = Math.round(random(0 + r, width - r));
this.y = Math.round(random(0 + r, height - r));
}
public int getX() {
return this.x;
}
public int getY() {
return this.y;
}
public void show(Circle[] arr) {
if(isColliding(arr)) {
this.x = Math.round(random(0 + r, width - r));
this.y = Math.round(random(0 + r, height - r));
} else {
ellipse(this.x, this.y, r * 2, r * 2);
stroke(255);
fill(255);
}
}
public boolean isColliding(Circle[] arr) {
boolean result = false;
if(arr == null) {
result = false;
} else {
for(Circle c : arr) {
float d = dist(c.getX(), c.getY(), this.x, this.y);
if(d < r * 2) {
result = true;
println("Collision at: " + c.getX() + " " + c.getY());
break;
}
}
}
return result;
}
}
如您所见,我已经有一个 isColliding 方法,并且控制台中的输出似乎是正确的,但是它在 show() 方法中不起作用,圆圈不会停止彼此相交.
那么我怎样才能让它工作,在碰撞时重新计算位置?
你确定你的碰撞方法有效吗?除非我遗漏了什么,否则它应该始终 return 为真,因为您正在传递一个包含自身的数组。
除此之外,我将开始研究您的 show() 逻辑的布局方式。您正在检查是否存在重叠,如果发现重叠,则分配一个新的随机位置。这个新位置很可能在一个已经画好的圆圈上,而且位置很好。
将您的重新定位放在一个循环中,以便它检查以确保它不只是将自己放在现有的圆圈上。
public void show(Circle[] arr)
{
/*
You could potentially get into a situation where you will NEVER find an empty spot.
Add an escape method for the loop.
*/
int failLimit = 500;
while(failLimit-- > 0 && isColliding(arr))
{
this.x = Math.round(random(0 + r, width - r));
this.y = Math.round(random(0 + r, height - r));
}
ellipse(this.x, this.y, r * 2, r * 2);
stroke(255);
fill(255);
}
你可以简化这个并通过一次一个地生成一个圆圈来提高效率,然后检查以确保它们的位置正确。
所以我目前正在学习 Java,并且我努力让我的代码正常工作。 我制作了一个 "Fun" 代码,其中弹出了一些圆圈,它计算了屏幕上方有多少个圆圈。 (我知道,一些愚蠢的代码。) 我在 "Processing" 环境中编码,语言是 Java.
这是我的主要文件:
Circle[] circles = new Circle[50];
int index = 0;
boolean finished = false;
void setup() {
size(900, 900);
background(0);
for(int i = 0; i < circles.length; i++) {
circles[i] = new Circle();
}
if(finished = true) {
}
}
void draw() {
if(index < circles.length) {
circles[index].show(circles);
index++;
} else {
finished = true;
}
}
void count(Circle[] arr) {
int n = 0;
for(Circle c : arr) {
if(c.getY() > height / 2) {
n++;
}
}
println(n);
}
这是 "Problem" 圈 class:
class Circle {
private int x, y;
private float r = random(10, 25);
Circle() {
this.x = Math.round(random(0 + r, width - r));
this.y = Math.round(random(0 + r, height - r));
}
public int getX() {
return this.x;
}
public int getY() {
return this.y;
}
public void show(Circle[] arr) {
if(isColliding(arr)) {
this.x = Math.round(random(0 + r, width - r));
this.y = Math.round(random(0 + r, height - r));
} else {
ellipse(this.x, this.y, r * 2, r * 2);
stroke(255);
fill(255);
}
}
public boolean isColliding(Circle[] arr) {
boolean result = false;
if(arr == null) {
result = false;
} else {
for(Circle c : arr) {
float d = dist(c.getX(), c.getY(), this.x, this.y);
if(d < r * 2) {
result = true;
println("Collision at: " + c.getX() + " " + c.getY());
break;
}
}
}
return result;
}
}
如您所见,我已经有一个 isColliding 方法,并且控制台中的输出似乎是正确的,但是它在 show() 方法中不起作用,圆圈不会停止彼此相交.
那么我怎样才能让它工作,在碰撞时重新计算位置?
你确定你的碰撞方法有效吗?除非我遗漏了什么,否则它应该始终 return 为真,因为您正在传递一个包含自身的数组。
除此之外,我将开始研究您的 show() 逻辑的布局方式。您正在检查是否存在重叠,如果发现重叠,则分配一个新的随机位置。这个新位置很可能在一个已经画好的圆圈上,而且位置很好。
将您的重新定位放在一个循环中,以便它检查以确保它不只是将自己放在现有的圆圈上。
public void show(Circle[] arr)
{
/*
You could potentially get into a situation where you will NEVER find an empty spot.
Add an escape method for the loop.
*/
int failLimit = 500;
while(failLimit-- > 0 && isColliding(arr))
{
this.x = Math.round(random(0 + r, width - r));
this.y = Math.round(random(0 + r, height - r));
}
ellipse(this.x, this.y, r * 2, r * 2);
stroke(255);
fill(255);
}
你可以简化这个并通过一次一个地生成一个圆圈来提高效率,然后检查以确保它们的位置正确。