Loading...
Logo
Processing Forum
Hi guys, i have classic moving circles and line between them if they are less than certain distance away.

Then i make an eraser with the mouse  (as you see in mouseMoved).

Now the problem is: i did erase the circles with my mouse, but those lines between them didn't.

So how can i erase the circle and meanwhile erase the line related to it ???

Copy code
  1. int maxCircles = 20;
  2. Circle[] circles = new Circle[maxCircles];

  3. void setup(){
  4.   size(400,400);
  5.   smooth();
  6.   for(int i = 0; i < maxCircles; i++) circles[i] = new Circle(random(width),random(height),random(20,40));
  7. }

  8. void draw(){
  9.   background(255);
  10.   for(int i = 0; i < maxCircles; i++){
  11.     circles[i].update();
  12.     circles[i].display();
  13.     
  14.     for(int j = 0; j < maxCircles; j++){
  15.     if(dist(circles[i].x,circles[i].y,circles[j].x,circles[j].y)<40){
  16.     stroke(0,50);
  17.     line(circles[i].x,circles[i].y,circles[j].x,circles[j].y);}
  18.     }
  19.   }
  20. }

  21. void mouseMoved(){

  22.    for(int i = 0; i < maxCircles; i++){
  23.     float distance = dist(mouseX,mouseY,circles[i].x,circles[i].y);
  24.     if(distance<20){
  25.     circles[i].r--;
  26.     }
  27.    }
  28.   }
  29.     
  30. class Circle{
  31.   float x,y,vx,vy,r;
  32.   Circle(float ax,float ay,float ar){
  33.     x = ax;
  34.     y = ay;
  35.     r = ar;
  36.     
  37.     vx = random(-.1,.1);
  38.     vy = random(-.1,.1);
  39.   }
  40.   void update(){
  41.     x += vx;
  42.     y += vy;
  43.     r=constrain(r,0,40);
  44.     
  45.   }
  46.   void display(){
  47.     noStroke();
  48.     fill(0,50);
  49.     ellipse(x,y,r,r);
  50.     }
  51. }

Many thanks.

Replies(3)

Hi Karen, looks like thats the question you mailed me for.
As said it really depends what you are trying to do how "erasing" works.
in your case you are changing the radius of the circles to erase them. so before you draw the line you can check if the circles are big enough. 
so i added these lines :


Copy code
  1.   if(circles[i].r>2 && circles[j].r>2)
  2.     stroke(0,50);
  3.     else noStroke();
  4.     line(circles[i].x,circles[i].y,circles[j].x,circles[j].y);}

which means, only if either one or the other circle has at least a radius of 2 draw a line.


Hope that helps!



Copy code

  1. int maxCircles = 20;
  2. Circle[] circles = new Circle[maxCircles];

  3. void setup(){
  4.   size(400,400);
  5.   smooth();
  6.   for(int i = 0; i < maxCircles; i++) circles[i] = new Circle(random(width),random(height),random(20,40));
  7.   }

  8. void draw(){
  9.   background(255);
  10.   for(int i = 0; i < maxCircles; i++){
  11.     circles[i].update();
  12.     circles[i].display();
  13.     
  14.     for(int j = 0; j < maxCircles; j++){
  15.     if(dist(circles[i].x,circles[i].y,circles[j].x,circles[j].y)<40){
  16.     noStroke();
  17.     if(circles[i].r>2 && circles[j].r>2)
  18.     stroke(0,50);
  19.     else noStroke();
  20.     line(circles[i].x,circles[i].y,circles[j].x,circles[j].y);}
  21.     }
  22.     }
  23. }

  24. void mouseMoved(){

  25.    for(int i = 0; i < maxCircles; i++){
  26.     float distance = dist(mouseX,mouseY,circles[i].x,circles[i].y);
  27.     if(distance<20){
  28.     circles[i].r--;
  29.      }
  30.     }}
  31.     
  32. class Circle{
  33.   float x,y,vx,vy,r;//store position(x,y), velocity(vx,vy) and size
  34.   Circle(float ax,float ay,float ar){
  35.     x = ax;
  36.     y = ay;
  37.     r = ar;
  38.     
  39.     vx = random(-.1,.1);
  40.     vy = random(-.1,.1);
  41.   }
  42.   void update(){
  43.     x += vx;//update position based on velocity
  44.     y += vy;
  45.     r=constrain(r,0,40);
  46.     
  47.   }
  48.   void display(){
  49.     noStroke();
  50.     fill(0,50);
  51.     ellipse(x,y,r,r);
  52.     
  53.   }
  54. }

Thank you. That helps.
btw, can i delete the item from the array directly?
Hey,

if the number of circles changes, you should use an ArrayList instead of an Array. Then you can use remove(). Be aware, that you should iterate over the array/arraylist in reverse order, if you remove items from it. Otherwise you would get errors when trying to access the last element.
Then you loop through all circles and check the distance to every other circle. That is not necessary, only check the distance to those circles that have not been checked before.
Ah, and you don't have to draw the lines when you call noStroke() before, just put the line() inside the if-clause.
Copy code
  1. int maxCircles = 50;
    ArrayList<Circle> circles = new ArrayList<Circle>();

    void setup() {
      size(400, 400);
      smooth();
      for (int i = 0; i < maxCircles; i++) circles.add(new Circle(random(width), random(height), random(20, 40)));
    }

    void draw() {
      background(255);
      // remove circles, if radius is smaller than 1
      for (int i = circles.size()-1; i >= 0; i--) {
        if (circles.get(i).r < 1) {
          circles.remove(i);
        }
      } 

      //update and draw circles
      for (int i = 0; i < circles.size(); i++) {
        Circle curCircle = circles.get(i);

        curCircle.update();
        curCircle.display();

        stroke(0, 50);
        // check distance to all circles with greater index
        for (int j = i+1; j <circles.size(); j++) {
          Circle compCircle = circles.get(j);
          if (dist(curCircle.x, curCircle.y, compCircle.x, compCircle.y)<40) {
            if (curCircle.r>2 && compCircle.r>2) {
              line(curCircle.x, curCircle.y, compCircle.x, compCircle.y);
            }
          }
        }
      }
      println("circles: "+circles.size());
    }

    void mouseMoved() {

      for (int i = 0; i < circles.size(); i++) {
        float distance = dist(mouseX, mouseY, circles.get(i).x, circles.get(i).y);
        if (distance<20) {
          circles.get(i).r--;
        }
      }
    }

    class Circle {
      float x, y, vx, vy, r;//store position(x,y), velocity(vx,vy) and size
      Circle(float ax, float ay, float ar) {
        x = ax;
        y = ay;
        r = ar;

        vx = random(-.1, .1);
        vy = random(-.1, .1);
      }
      void update() {
        x += vx;//update position based on velocity
        y += vy;
        r=constrain(r, 0, 40);
      }
      void display() {
        noStroke();
        fill(0, 50);
        ellipse(x, y, r, r);
      }
    }