How do you use distance and println to make two ellipses change color when they pass through each other?
in
Programming Questions
•
2 years ago
How do you use distance and println to make two ellipses change color when they pass through each other?
Can someone give me a sample code? Pleasee helpp . I'm getting a headache from this . And this is what I have so far:
int xpos=100;
int ypos=100;
int xspeed=3;
int yspeed=7;
int xpos1=80;
int ypos1=100;
int xspeed1=4;
int yspeed1=3;
float distance;
float radius;
float diameter;
float value;
float r,g,b;
int x1;
int x2;
int y1;
int y2;
void setup() {
size(400,400);
}
void draw() {
background(255);
if(distance>width)
{
fill(120,230,120);
}
radius=distance;
distance=sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
x1=xpos;
y1=ypos;
x2=width/2;
y2=height/2;
println(distance);
ellipse(xpos,ypos,100,100);
xpos=xpos+xspeed;
ypos=ypos+yspeed;
if(xpos>width)
{
xspeed=-xspeed;
}
if(xpos<0)
{
xspeed=-xspeed;
}
if(ypos>height)
{
yspeed=-yspeed;
}
if(ypos<0)
{
yspeed=-yspeed;
}
fill(230,120,103);
ellipse(xpos1,ypos1,50,50);
xpos1=xpos1+xspeed1;
ypos1=ypos1+yspeed1;
if(xpos1>width)
{
xspeed1=-xspeed1;
}
if(xpos1<0)
{
xspeed1=-xspeed1;
}
if(ypos1>height)
{
yspeed1=-yspeed1;
}
if(ypos1<0)
{
yspeed1=-yspeed1;
}
}
Can someone give me a sample code? Pleasee helpp . I'm getting a headache from this . And this is what I have so far:
int xpos=100;
int ypos=100;
int xspeed=3;
int yspeed=7;
int xpos1=80;
int ypos1=100;
int xspeed1=4;
int yspeed1=3;
float distance;
float radius;
float diameter;
float value;
float r,g,b;
int x1;
int x2;
int y1;
int y2;
void setup() {
size(400,400);
}
void draw() {
background(255);
if(distance>width)
{
fill(120,230,120);
}
radius=distance;
distance=sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
x1=xpos;
y1=ypos;
x2=width/2;
y2=height/2;
println(distance);
ellipse(xpos,ypos,100,100);
xpos=xpos+xspeed;
ypos=ypos+yspeed;
if(xpos>width)
{
xspeed=-xspeed;
}
if(xpos<0)
{
xspeed=-xspeed;
}
if(ypos>height)
{
yspeed=-yspeed;
}
if(ypos<0)
{
yspeed=-yspeed;
}
fill(230,120,103);
ellipse(xpos1,ypos1,50,50);
xpos1=xpos1+xspeed1;
ypos1=ypos1+yspeed1;
if(xpos1>width)
{
xspeed1=-xspeed1;
}
if(xpos1<0)
{
xspeed1=-xspeed1;
}
if(ypos1>height)
{
yspeed1=-yspeed1;
}
if(ypos1<0)
{
yspeed1=-yspeed1;
}
}
1
