#Simple way to make Venn diagram (intersection of shapes detection) in Processing Java

4 messages · Page 1 of 1 (latest)

winter tinsel
#

A total programming beginner that I am helping started going through the "Learning Processing" course and already found this hard to do thing.

Set operations on shapes that you could check and draw pixels differently based on.
Say two or more circles, A, B, C etc.

I want intersection of A and B to be red, A and C to be green and B and C to be blue. He tried all the blendModes and didn't figure it out.
I, with years and years of coding experience, have only found pretty hard and unsatisfying ways to do this, some included bringing in more Java, many included imperfect solutions.

For example, filter solution from this post:
#1171532567489560576 message

Is there no simple way to iterate all (or some) pixels in the window and ask "Are you part of shape X?". Or precise shape collision? Or programmable blend mode?

Thanks a lot for any answers.

quasi furnace
#

Is there no simple way to iterate all (or some) pixels in the window and ask "Are you part of shape X?"
Here's a way to do that: ```processing

PVector[] circles;
float diameter;

color FILL_AB = color(255, 0, 0);
color FILL_AC = color(0, 255, 0);
color FILL_BC = color(0, 0, 255);
color FILL_ABC = color(255, 255, 255);

void setup() {
size(600, 600);
ellipseMode(CENTER);

diameter = 300;
circles = new PVector[] {
new PVector(width/2 - diameter/3, height/2 - diameter/3),
new PVector(width/2 + diameter/3, height/2 - diameter/3),
new PVector(width/2, height/2 + diameter/3)
};
}

void draw() {
background(200);

int smallX = width, bigX = -1;
int smallY = height, bigY = -1;

for (PVector center : circles) {
float x1 = center.x - diameter/2;
float x2 = center.x + diameter/2;

float y1 = center.y - diameter/2;
float y2 = center.y + diameter/2;

smallX = (int) min(smallX, x1);
bigX = (int) max(bigX, x2);

smallY = (int) min(smallY, y1);
bigY = (int) max(bigY, y2);

}

smallX = max(smallX, 0);
bigX = min(width, bigX);

smallY = max(smallY, 0);
bigY = min(height, bigY);

loadPixels();
for (int x = smallX; x < bigX; x++) {
for (int y = smallY; y < bigY; y++) {
boolean[] con = contains(x, y);
color c = color(0);

  if (con[0] && con[1] && con[2])
    c = FILL_ABC;
  else if (con[0] && con[1])
    c = FILL_AB;
  else if (con[0] && con[2])
    c = FILL_AC;
  else if (con[1] && con[2])
    c = FILL_BC;
  else
    continue;
  
  int index = width * y + x;
  pixels[index] = c;
}

}
updatePixels();
}

boolean[] contains(int x, int y) {
boolean[] out = new boolean[circles.length];

for (int i = 0; i < circles.length; i++) {
PVector center = circles[i];

if ( dist(x, y, center.x, center.y) < diameter/2 )
  out[i] = true;

}

return out;
}

#

Here's what it looks like:

#

And here's where the circles are for reference: