Interaction among objects in an array
in
Programming Questions
•
2 months ago
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;
- }
- }
- }
1