Jump to content

Mouse-over effect altering the circle size and color of circles processing

Viz-man's Photo
Posted Aug 20 2010 10:02 PM
4182 Views

Hi,

I am new to processing. What i have done so far is to apply the circle packing algorithm to bunch circles of different sizes together in processing. The size of each individual circle and it's color is stored in an array list.

I have developed a mouse-over effect that when my mouse hover over the any of the circles - the size and color of that particular circle will change. This I got it right.

Now, i have another task that I want to extend the mouse over implementation that i have done to also change the size and colour of the surrounding circles that are touching the circle that I mouser over.

May I know how should I go about implementing his effect?

1 Reply

+ 2
  odewahn1's Photo
Posted Sep 09 2010 04:24 AM

Hi, Viz-man. The trick here is to figure out some quick method for collision detection between two circles. When I have questions like this, the first thing I do is draw a picture, just like in good old high-school geometry. For example:

Attached Image

Looking at the picture, it seems pretty clear that the two circles intersect if the sum of their radii is less than or equal to the distance between their centers. You can use the dist() function to find the distance b/t centers, so you're pretty much half-way home at this point.

So, in your original example, you probably have the index of the circle the mouse is already touching. Let's say that is stored in a variable called targetIdx. What you need to do now is loop through the array of circles, do this distance test, and then return a flag (or set a value in another array, or update a field in a class, or whatever).

Here's a snippet that will show you how to do this if you're using a class


Class circle {

   int x, y, r;
   boolean is_touching_flag = false;

  ...
}

...

Circle[] c = new Circle[3];  //Suppose this is the array of circles, and that they get initialized somewhere

...

int targetIdx = 0;   //index of the circle we're hovering over
for (int j=0; j < c.length; j++) {
   float d = dist(c[targetIdx].x,c[targetIdx].y, c[j].x, c[j].y);
   if ( d < (c[targetIdx].r + c[j].r) {
      c[j].is_touching_flag = true;
   } else {
      c[j].is_touching_flag = false;  //be sure to reset this each time, or it will be wonky!
   }
}




Hope this helps!

Also, as a plug, O'Reilly has a great book called Getting Started with Processing that you should check out. It's by the authors of the language, and it's a fun, quick read.