2017-03-16 47 views
-2

我正在尝试编写一个处理程序,以绘制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++; 
    } 
    } 
} 
+0

不幸的是,SO不提供和调试服务。 –

+0

@WasiAhmad对不起,如果它出现,就像我要求你调试我的代码,但这是我第一次使用处理,所以我只是希望有人让我知道我在这里做错了什么。我很确定它与变量的范围有关,但我对这个东西还是比较新的,所以如果你能让我知道是否是这种情况,我会很感激。 – ldepablo

+0

这段代码是如何被调用的? –

回答

0

您需要养成调试程序的习惯。无耻的自我推销:我写了关于调试处理草图here的教程。

我的猜测是,您的代码卡在您的createPopulation()函数中的一个while循环中。调试会告诉你哪一个,然后你可以修复你的错误。

你也应该养成breaking your problem down into smaller steps的习惯。你不应该写你的整个程序,然后想知道为什么它不工作。相反,只能在一次只测试一件东西的小块中工作。然后,当您遇到错误时,您可以发布MCVE而不是整个草图。

+0

是的,在'createPopulation'中,'i'的值在每种情况下都没有增加,可能导致无限循环 –

+0

@ScaryWombat正确。但真正的教训是,OP需要学习如何调试他们的程序。 –

+1

OP,另请参见[与鸭子交谈](https://en.wikipedia.org/wiki/Rubber_duck_debugging) –

0

使用“随机()”来显示随机位置的人