How to change colors between circles when overlapping?

Circle [] circles = new Circle [10];

class Circle
{
  float x;
  float y;
  float speed;
  float diam;
  color c;
  boolean valid;
 
  final int MAX_COLOR = 255;
  final int MIN_X = 50, MAX_X = 750;
  final int MIN_Y = -300, MAX_Y = -100;
  final int MIN_SPEED = 1, MAX_SPEED = 2;
  final int MIN_DIAM = 70, MAX_DIAM = 110;
 
  Circle()
  {
    fallingSky();
  }
 
  void fallingSky() {
    valid = true;
    c = color(random(255), random(255), random(255));
    x = random(MIN_X, MAX_X); 
    y = random(MIN_Y, MAX_Y);
    speed = random(MIN_SPEED, MAX_SPEED);
    diam  = random(MIN_DIAM, MAX_DIAM);
  }
 
  void update() {
    if (!valid) {
      fallingSky();
      return;
    }
    move();
    draw_ellipse();
  }
  void draw_ellipse()
  {
    fill(c);
    ellipse (x, y, diam, diam);
  }
 
  void move()
  {
    if (y-diam <= height)
    {
      y += speed;
    } else if (y-diam > height )
    {
      valid = false;
      
    }
  }
 
  boolean click(int mx, int my) {
    float dX = x - mx;
    float dY = y - my;
    if (sqrt(sq(dX) + sq(dY)) < diam/2 ) {
      return true;
    } else {
      return false;
    }
  }
}
 
 
void setup() {
  size (1000, 600);
  for (int i = 0; i < circles.length; i++)
  {
    circles[i] = new Circle();
  }
}
 
 
void draw() {
  background(100, 100);
  for (int i = 0; i < circles.length; i++) {
    circles[i].update();
    
  }
}
  
void mousePressed()
{
  for (Circle s : circles )
  {
    if (s.click(mouseX, mouseY))
    {
      s.valid = false;
      break;
    }
  }
}
1 Like

my code has random colored circles, and random attributes. For my project, I’m trying to make it when more than 1 circle overlaps, then the color of the circle change. I’m stuck on how I should approach this. Any ideas???

Circle [] circles = new Circle [10];

class Circle
{
  float x;
  float y;
  float speed;
  float diam;
  color c;
  boolean valid;

  final int MAX_COLOR = 255;
  final int MIN_X = 50, MAX_X = 750;
  final int MIN_Y = -300, MAX_Y = -100;
  final int MIN_SPEED = 1, MAX_SPEED = 2;
  final int MIN_DIAM = 70, MAX_DIAM = 110;

  Circle()
  {
    fallingSky();
  }

  void fallingSky() {
    valid = true;
    c = color(random(255), random(255), random(255));
    x = random(MIN_X, MAX_X);
    y = random(MIN_Y, MAX_Y);
    speed = random(MIN_SPEED, MAX_SPEED);
    diam = random(MIN_DIAM, MAX_DIAM);
  }

  void update() {
    if (!valid) {
      fallingSky();
      return;
    }
    move();
    draw_ellipse();
  }
  void draw_ellipse()
  { 
    fill(c);
    noStroke();
    ellipse (x, y, diam, diam);
  }

  void move()
  {
    if (y-diam <= height)
    {
      y += speed;
    } else if (y-diam > height )
    {
      valid = false;
    }
  }

  boolean click(int mx, int my) {
    float dX = x - mx;
    float dY = y - my;
    if (sqrt(sq(dX) + sq(dY)) < diam/2 ) {
      return true;
    } else {
      return false;
    }
  }
}

void setup() {
  size (1000, 600);
  for (int i = 0; i < circles.length; i++)
  {
    circles[i] = new Circle();
  }
}

void draw() {
  background(100, 100);
  for (int i = 0; i < circles.length; i++) {
    circles[i].update();
  }
      stroke(10);
  for (int i = 0; i < circles.length; i++) {
    for (int j = 0; j < circles.length; j++) {
      if(dist(circles[i].x,circles[i].y,circles[j].x,circles[j].y)<100){
         circles[j].c = color(0,0,255);
         line(circles[i].x,circles[i].y,circles[j].x,circles[j].y);
      }else circles[i].c = color(255);
    }
  }
}

void mousePressed()
{
  for (Circle s : circles )
  {
    if (s.click(mouseX, mouseY))
    {
      s.valid = false;
      break;
    }
  }
}

One method for checking is circle-circle collision detection. Here is example code with a demo:

Another option is to use a height map, but this is more involved.