We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Changing color of overllapping circles
Page Index Toggle Pages: 1
Changing color of overllapping circles (Read 1282 times)
Changing color of overllapping circles
Nov 24th, 2006, 6:01am
 
Hi -

Im working on a project where I'm using circles to represent the number of wireless users on a map.  What I'd like to have happen, is where the circles overlap, change the color.  So the circles are all yellow, but when they overlap, I'd like them to be red, lets say (just in the overlapping region).  Think ven diagrams - you know, like this:
http://www.cloudappreciationsociety.org/3content/of_interest/assets_of_interest/us_stamps/ven_dia.gif

I'm having trouble thinking about how to approach this problem.  Is this possible, and if so, could one of you give me a hint about how to start tackling this?  I think I can figure out if two circles are overlapping, but what I don't know how to do is change the color of the overlapping region.

Thanks!
Re: Changing color of overllapping circles
Reply #1 - Nov 24th, 2006, 7:53am
 
Unfortunately, as far as I know, drawing overlapping shapes on the fly is difficult, if not overly computationally taxing (which is what you'd essentially be doing when making that shape a new color.

If I might suggest a simpler option that may retain the same desired aesthetic: give your fill on the colors an alpha value, a la color(R, G, B, A) where that last value represents the opacity of the color. When those shapes overlap, the color intensity at their intersection will be noticable.

Hope this helps Smiley
Re: Changing color of overllapping circles
Reply #2 - Nov 24th, 2006, 4:06pm
 
Thanks Matthew - actually thats how I have it set up now - the opacity of the circles is 70% or so.  It's alright, though not really the desired effect.  I suppose I may just have to make do. Smiley
Re: Changing color of overllapping circles
Reply #3 - Nov 27th, 2006, 9:04pm
 
Thats why we're all praying that our esteemed gurus fry and reas will impliment geometry color blending modes at some point!
Re: Changing color of overllapping circles
Reply #4 - Nov 30th, 2006, 5:40am
 
I couldn't resist this problem in spite of being massively behind in, well everything. My solution is not elegant nor terribly efficient, but the basic math seems to get the job done–at least for ellipses with the same diameter.

Hope this is sort of what you were looking for.

-ira

Code:

/* Dynamically calculate overlap shape &
average color between 2 intersecting
ellipses (of equal diameter) */

Circle c1, c2;
void setup(){
size(400, 400);
smooth();

c1 = new Circle(50, color(255, 0, 0));
c2 = new Circle(50, color(0, 0, 255));
}

void draw(){
background(225, 225, 20);
noStroke();
c1.createCircle(200, 200);
c2.createCircle(mouseX, mouseY);

float d = dist(c1.x, c1.y, c2.x, c2.y);
if (d < c1.r+c2.r){
// calculate vector between ellipse centers
float dragAng = atan2(c2.y-c1.y, c2.x-c1.x);

// calculate angle from ellipse
// center to overlap point(s)
float overlapAng = acos(d/2/c1.r);

// calculate overlap points
float px1 = c1.x+cos(dragAng+overlapAng)*c1.r;
float py1 = c1.y+sin(dragAng+overlapAng)*c1.r;
float px2 = c1.x+cos(dragAng-overlapAng)*c1.r;
float py2 = c1.y+sin(dragAng-overlapAng)*c1.r;

// create overlapping section
/* calculate average color for overlap
obviously bitwise ops would be faster */
float r = (red(c1.c)+red(c2.c))/2;
float g = (green(c1.c)+green(c2.c))/2;
float b = (blue(c1.c)+blue(c2.c))/2;
fill(r, g, b);
//plot overlap shape
noStroke();
int detail = 20;
float ang = degrees(dragAng-overlapAng);
float arcAng = 0;
float cx=0, cy=0;
beginShape();
// arc 1
// not efficient but it works
for (int i=0; i<detail; i++){
cx = c1.x+cos(radians(ang+arcAng))*c1.r;
cy = c1.y+sin(radians(ang+arcAng))*c1.r;
vertex(cx, cy);
arcAng+=degrees(((overlapAng)*2)/detail);
}
// arc 2
arcAng = 0;
for (int i=0; i<detail; i++){
cx = c2.x+cos(radians(-180+ang+arcAng))*c2.r;
cy = c2.y+sin(radians(-180+ang+arcAng))*c2.r;
vertex(cx, cy);
arcAng+=degrees(((overlapAng)*2)/detail);
}
endShape(CLOSE);

// draw drag vector
stroke(255);
line(c1.x, c1.y, c2.x, c2.y);

// draw overlap points with connecting line
noStroke();
fill(255);
ellipse(px1, py1, 5, 5);
ellipse(px2, py2, 5, 5);
stroke(200);
line(px1, py1, px2, py2);

/* draw overlap triangles -
illustrating trig relationships */
line(c1.x, c1.y, px1, py1);
line(c1.x, c1.y, px2, py2);
line(c2.x, c2.y, px1, py1);
line(c2.x, c2.y, px2, py2);
}
}

class Circle{
float x, y, r;
color c;

Circle(float r, color c){
this.r = r;
this.c = c;
}

void createCircle(float x, float y){
fill(c);
this.x = x;
this.y = y;
ellipse(x, y, r*2, r*2);
}
}
Re: Changing color of overllapping circles
Reply #5 - Nov 30th, 2006, 6:28am
 
introducing...
geomerataive a library for processing 2d geometry...

http://www.ricardmarxer.com/processing/geomerative/documentation/index.htm

read about the these methods today...

xor - http://www.ricardmarxer.com/processing/geomerative/documentation/rpolygon_method_xor.htm
intersection - http://www.ricardmarxer.com/processing/geomerative/documentation/rpolygon_method_intersection.htm
union -
http://www.ricardmarxer.com/processing/geomerative/documentation/rpolygon_method_union.htm
diff -
http://www.ricardmarxer.com/processing/geomerative/documentation/rpolygon_method_diff.htm

and cure nights of fustration...
Re: Changing color of overllapping circles
Reply #6 - Nov 30th, 2006, 10:18pm
 
Wow - the code Ira posted is fantastic!  Thanks Ira - that is beautiful code.  

Also, kidult -- it looks like you're putting together a great library.  I wasn't actually able to get it to work right off the bat, but I'll spend some more time with it this evening.  

Thanks for both of your responses.  Processing has a great community supporting it.
Re: Changing color of overllapping circles
Reply #7 - Dec 1st, 2006, 4:39pm
 
i didnt write geomerative ricard marxer did...
http://www.ricardmarxer.com/.

you should try geomerative though because you can easily expand upon the geometry require to determine the shape of two intersecting shapes... what issues were you having?

Re: Changing color of overllapping circles
Reply #8 - Dec 1st, 2006, 4:45pm
 
tedpower,
I'm glad the code helped.
And I completely agree–Processing is/has an awesome community!
Re: Changing color of overllapping circles
Reply #9 - Dec 2nd, 2006, 6:27pm
 
If you don't actually need the geometry of the overlapping area, that is if this is simply a coloring problem, then there is perhaps a rather simple solution.

Draw translucently as matthew suggests, then recolor the output as desired, for example:

http://www.davebollinger.com/misc/CircleOverlap/

This is similar in concept to how collision planes of old sprite hardware used to work. (just not with 32-bit pixels!)
Page Index Toggle Pages: 1