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 › dist() help, please
Page Index Toggle Pages: 1
dist() help, please (Read 939 times)
dist() help, please
Jan 4th, 2010, 12:02pm
 
Hi all, im having trouble working out how to use the dist() function. I want the ellipses to turn red when the line hits them. I know i should use the dist function but cant work out how to use it. any help would be greatly appreciated.
cheers

Code:
ArrayList balls;
int numBalls = 100;
int currentBall = 0;
int ballWidth = 25;
float y = 0.0;
boolean ball = false;
boolean start = false;
PFont font;



void setup(){
 size(500,550);
 smooth();
 balls = new ArrayList();
 font = loadFont("GillSans-48.vlw");
 textFont(font);
 
 
}
 


void draw(){
 background(0);
 stroke(247,15,15,200);
 strokeWeight(8);
 
 
 
 if(start == true){
 y = y + 2.0;
 line(y,0.0,y,500);
 if(y>=500){
   y=0;
 }
 }



 stroke(255);
 strokeWeight(4);
 line(0,100,500,100);
 line(0,200,500,200);
 line(0,300,500,300);
 line(0,400,500,400);
 fill(250,25,25);
 noStroke();


 rect(0,500,500,500);
   if((keyPressed == true) && (key == 's')){
   start = true;
 }
 
 
 fill(255);
 text("Press 's' to start",10,545);
 
 
 
 for(int i = balls.size()-1; i>=0;i--){
   Ball ball = (Ball) balls.get(i);
   ball.display();
 }
 }

 
void mousePressed(){
  ball =true;
  balls.add(new Ball(mouseX, mouseY,ballWidth));
}


BALL CLASS CODE:
Code:
class Ball{
 float x,y,w;
 

 Ball(float tempX, float tempY, float tempW){
   x = tempX;
   y = tempY;
   w = tempW;

 }
 
 void display(){
   fill(255);
   noStroke();
   ellipse(x,y,w,w);
 }
}
Re: dist() help, please
Reply #1 - Jan 4th, 2010, 1:32pm
 
Perhaps something like this:
Code:

 for(int i = balls.size()-1; i>=0;i--){
   Ball ball = (Ball) balls.get(i);
   if(ball.x-ballWidth/2 < y && ball.x+ballWidth/2 > y) {
fill(255,0,0);
   } else {
fill(255);
   }
   ball.display();
 }


Note that you need to remove the fill(255) from the ball.display() class code.

Also note that your use of the variable "y" for your line is really the x dimension (little confusing).
Re: dist() help, please
Reply #2 - Jan 4th, 2010, 1:48pm
 
you need to check the distance to y that actually should be x...
but you cant cause you create another x withing your class so i renamed it, and also made it flexible. that you can change the circlewidth...


Code:
ArrayList balls;
int numBalls = 100;
int currentBall = 0;
int ballWidth = 25;
float linex = 0.0;
boolean ball = false;
boolean start = false;
PFont font;



void setup(){
size(500,550);
smooth();
balls = new ArrayList();
font = createFont("GillSans-48.vlw",42);
textFont(font);
}



void draw(){
background(0);
stroke(247,15,15,200);
strokeWeight(8);



if(start == true){
linex = linex + 2.0;
line(linex,0.0,linex,500);
if(linex>=500){
linex=0;
}
}



stroke(255);
strokeWeight(4);
line(0,100,500,100);
line(0,200,500,200);
line(0,300,500,300);
line(0,400,500,400);
fill(250,25,25);
noStroke();


rect(0,500,500,500);
if((keyPressed == true) && (key == 's')){
start = true;
}


fill(255);
text("Press 's' to start",10,545);



for(int i = balls.size()-1; i>=0;i--){
Ball ball = (Ball) balls.get(i);
ball.display();
}
}


void mousePressed(){
ball =true;
balls.add(new Ball(mouseX, mouseY,ballWidth));
}

class Ball{
float x,y,w;


Ball(float tempX, float tempY, float tempW){
x = tempX;
y = tempY;
w = tempW;

}

void display(){
fill(255);
noStroke();
if(dist(x,0,linex,0)<=ballWidth/2)fill(255,0,0);
if(dist(x,0,linex,0)>=ballWidth/2)fill(255);
ellipse(x,y,w,w);
}
}
Re: dist() help, please
Reply #3 - Jan 4th, 2010, 3:05pm
 
Thanks so much for all your help. Seriously first class help. Really appreciate it.
p.s i know i stupidly called my variable y when it was referring to the x coordinate, kept meaning to change it but always had something else to fix.
Cheers
Page Index Toggle Pages: 1