We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › overlapping circles.
Page Index Toggle Pages: 1
overlapping circles. (Read 793 times)
overlapping circles.
Jan 17th, 2008, 8:12pm
 
Hi all!  I designed my program so that the circles inside the applet will move 80 pixels in length when I hold down the mouse.  The circles move around randomly across the screen, but the circles should overlap perfectly with one another.  I am getting a bug where if I hold down the mouse button for a bit the circles deviate from its 80 pixels restriction... to 79 78 77 etc.... does anyone know what is going on and also how to resolve this?

here's the code:

int count = 70;
Thing[]things = new Thing[count];

void setup(){
 background(0);
 size(640, 800);
 frame.setResizable(true);
 frameRate(20);
 
 
 for(int i = 0; i < things.length; i++){
   things[i] = new Thing(40,40, int(random(100,210)), 250, 40);
 }
}

void draw(){
 background(0);
 noFill();
 rect(0, 0, width, height);
 for(int i = 0; i < things.length; i++){
   things[i].display();
   
   if(mousePressed == true){
     things[i].move();
     println(things[i].x);
     println(things[i].y);
  }
 }
}

class Thing{
 float x, y;
 int r;
 int g;
 int b;
 int vd = 80;
 int opacity = 100;
 int diameter = 80;

 Thing(int xpos, int ypos, int rr, int gg, int bb){
   x = xpos;
   y = ypos;
   r = rr;
   g = gg;
   b = bb;
 }
 void display(){
   fill(r, g, b, opacity);
   smooth();
   noStroke();
   //ellipseMode(CORNER);
   ellipse(x, y, diameter, diameter);  
 }
 
 void move() {
   int t = int(random(-2, 2));
   int v = int(random(2));
   
   if(v == 0){
     x += vd*t;
     println("5");
     println(v);
     println(t);

   }else if(v==1){
     y += vd*t;
     println("6");
     println(v);
     println(t);
   }
   
   if (x + diameter/2 > width) {
     x = width - diameter/2;
     vd += -1;
     println("1");
   }
   else if (x - diameter/2 < 0) {
     x = diameter/2;
     vd *= -1;
     println("2");
   }
   if (y + diameter/2 > height) {
     y = height - diameter/2;
     vd *= -1;
     println("3");
   }
   else if (y - diameter/2 < 0) {
     y = diameter/2;
     vd *= -1;
     println("4");
   }
 }
}
 
 
 


 
Re: overlapping circles.
Reply #1 - Jan 17th, 2008, 8:44pm
 
line 74, you wrote : vd += -1;
instead of vd *= -1;
Re: overlapping circles.
Reply #2 - Jan 17th, 2008, 8:56pm
 
Thank you!
Page Index Toggle Pages: 1