Loading...
Processing Forum
Recent Topics
All Forums
Screen name:
josem.93
josem.93's Profile
1
Posts
1
Responses
0
Followers
Activity Trend
Last 30 days
Last 30 days
Date Interval
From Date :
To Date :
Go
Loading Chart...
Posts
Responses
PM
Show:
All
Discussions
Questions
Expanded view
List view
Private Message
Interaction among objects in an array
[3 Replies]
10-Aug-2013 04:03 AM
Forum:
Programming Questions
The code consists on some moving points. I'd like to draw lines from point to point when the distance that separates them is less than a certain number, but i don't know how can i make the point coordinates interact among them. HELP!
Dots [] dots = new Dots [20];
void setup(){
size(800,600);
smooth();
for(int i = 0; i < dots.length; i++){
dots[i] = new Dots();
}
}
void draw(){
background(255);
for(int i = 0; i < dots.length; i++){
dots[i].move();
dots[i].display();
if (dots[i].intersect(dots[i])){
dots[i].drawLines(dots[i]);
}
}
}
class Dots{
float x,y; // position
float xspeed,yspeed;
float d; //diameter
color c;
Dots(){
x = width/2;
y = height/2;
d = 5;
c = color (200);
xspeed = random(-5,5);
yspeed = random(-5,5);
}
void display(){
noStroke();
fill(c);
ellipse(x,y,d,d);
}
void move(){
x += xspeed;
y += yspeed;
if(x > width || x < 0){
xspeed *= -1;
}
if(y > height || y < 0){
yspeed *= -1;
}
}
void drawLines(Dots b){
stroke(c);
line(x,y,b.x,b.y);
}
boolean intersect(Dots b){
float distance = abs(dist (x,y,b.x,b.y));
if(distance < 30){
return true;
} else {
return false;
}
}
}
«Prev
Next »
Moderate user : josem.93
Forum