我正在尝试编写一个处理程序,以绘制100个不重叠的随机x和y坐标的椭圆。显示正在处理的东西
我写了这段代码,但由于某种原因,我运行它时没有显示任何内容。它不会抛出任何错误或任何东西,但它不起作用。
static person[] population;
void setup() {
population=new person[100];
size(1000,700);
stroke(0);
createPopulation(50);
for(int i=0; i<100; i++){
fill(population[i].getR(), population[i].getG(), population[i].getB());
ellipse(population[i].getX(), population[i].getY(), 50, 50);
}
}
void draw() {
for(int i=0; i<100; i++){
fill(population[i].getR(), population[i].getG(), population[i].getB());
ellipse(population[i].getX(), population[i].getY(), 50, 50);
}
}
class person {
int x,y;
boolean immune;
boolean sick;
int r;
int g;
int b;
person(int xPos, int yPos, boolean isImmune, boolean isSick){
x=xPos;
y=yPos;
immune=isImmune;
sick=isSick;
}
void checkIfSick(){
if(sick){
r=255;
g=0;
b=0;
}
else{
r=100;
g=100;
b=255;
}
}
int getR(){
return this.r;
}
int getG(){
return this.g;
}
int getB(){
return this.b;
}
int getX(){
return this.x;
}
int getY(){
return this.y;
}
}
void createPopulation(int percentImmune){
int[] xPositions=new int[100];
int[] yPositions=new int[100];
int i=0;
while(i<percentImmune){
boolean xMatch=false;
boolean yMatch=false;
int randX=(int)Math.round(Math.random()*1000);
int randY=(int)Math.round(Math.random()*700);
population[i]=new person(randX, randY, true, false);
for(int n=0; n<i; n++){
if(randX/25==xPositions[n]){
xMatch=true;
}
if(randY/25==yPositions[n]){
yMatch=true;
}
}
if(xMatch==false || yMatch==false){
xPositions[i]=randX/25;
yPositions[i]=randY/25;
i++;
}
}
while(i<100){
boolean xMatch=false;
boolean yMatch=false;
int randX=(int)Math.round(Math.random()*1000);
int randY=(int)Math.round(Math.random()*700);
population[i]=new person(randX, randY, false, false);
for(int n=0; n<i; n++){
if(randX/25==xPositions[n]){
xMatch=true;
}
if(randY/25==yPositions[n]){
yMatch=true;
}
}
if(xMatch==false || yMatch==false){
xPositions[i]=randX/25;
yPositions[i]=randY/25;
i++;
}
}
}
不幸的是,SO不提供和调试服务。 –
@WasiAhmad对不起,如果它出现,就像我要求你调试我的代码,但这是我第一次使用处理,所以我只是希望有人让我知道我在这里做错了什么。我很确定它与变量的范围有关,但我对这个东西还是比较新的,所以如果你能让我知道是否是这种情况,我会很感激。 – ldepablo
这段代码是如何被调用的? –