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 › a self intersecting polygon be filled correctly
Page Index Toggle Pages: 1
a self intersecting polygon be filled correctly? (Read 1042 times)
a self intersecting polygon be filled correctly?
Dec 8th, 2009, 1:22pm
 
Hello forum,
this is my first thread here. I hope I am in the right forum.
My question is:
Will a self intersecting polygon be filled correctly with processing?
I found out that it will *not*, on my system with processing v 1.0.9.
example draws two polygons, one is filled and the other is not:
Code:

size(800, 600, P2D);
stroke(200, 0, 0);
strokeWeight(4);
fill(0, 0, 200);
translate(width / 2, height / 2);
scale(1.0, -1.0);
scale(100.0);

translate(-2.0, 0.0);
beginShape();
vertex(0.0, 2.0);
vertex(1.0, 0.0);
vertex(0.0, -2.0);
vertex(-1.0, 0.0);
vertex(0.0, 2.0);
endShape();
//yeah it's filled!

translate(4.0, 0.0);
beginShape();
vertex(0.0, 2.0);
vertex(0.0, -2.0);
vertex(1.0, 0.0);
vertex(-1.0, 0.0);
vertex(0.0, 2.0);
endShape();
//Oo it is not filled ... but why ...

//question:
//how is it possible to draw a _filled_ _self_intersecting_ polygon?

java: 1.6.17

many thanks for every answer!
OlimilO
Re: a self intersecting polygon be filled correctly?
Reply #1 - Dec 8th, 2009, 1:51pm
 
seems like it is not working when using other renderer than JAVA2D.
So you can remove P2D or put JAVA2D in there.
Re: a self intersecting polygon be filled correctly?
Reply #2 - Dec 8th, 2009, 3:33pm
 
Or you could simply split it into two triangles:

Code:

void setup() {
 size(800, 600, P2D);
  stroke(200, 0, 0);
  strokeWeight(2);
 // noStroke();
  fill(0, 0, 200);
  translate(width / 2, height / 2);

  translate(-200.0, 0.0);
  beginShape();
vertex(0.0, 200.0);
vertex(100.0, 0.0);
vertex(0.0, -200.0);
vertex(-100.0, 0.0);
vertex(0.0, 200.0);
  endShape();
  //yeah it's filled!
 
  translate(400.0, 0.0);  

  beginShape();
vertex(0.0, -200.0);
       vertex(100.0, 0.0);
       vertex(0.0,0.0);
   endShape(CLOSE);

   beginShape();
       vertex(0.0,0.0);
vertex(-100.0, 0.0);
vertex(0.0, 200.0);
  endShape(CLOSE);
}

void draw() {

}


Sorry - not sure what that scaling was about so stripped it  Undecided
Re: a self intersecting polygon be filled correctly?
Reply #3 - Dec 14th, 2009, 8:00am
 
@Cedric thanks for the answer, it's much better, but it's still not best.
I think of a polygon like this:
Code:

size(800, 600, JAVA2D); //OK
stroke(200, 0, 0);
//strokeWeight(4);
strokeWeight(0.04);
fill(0, 0, 200);
translate(width / 2, height / 2);
scale(1.0, -1.0);
scale(100.0);

beginShape(POLYGON);
vertex( 0.0, 1.0);
vertex( 0.0, -1.0);
vertex( 1.0, -0.3);
vertex(-1.0, 0.7);
vertex(-1.0, -0.7);
vertex( 1.0, 0.3);
vertex( 0.0, 1.0);
endShape();

the problem here is: the inner triangle will be filled but it should not.

hmm ... I guess I have to decomposition the polygon on my own?

is there a certain geometry-library that works best with processing?

many thanks in advance

OlimilO
Re: a self intersecting polygon be filled correctly?
Reply #4 - Dec 14th, 2009, 9:28am
 
maybe you wanna take a look at breakShape()
its undocumented but its working fine to cut out shapes of another shape.

// Code by davebollinger
//
// oi.pde
// demo of undoc'd/unsup'd breakShape()
// (tho it's used by SVG et al, so likely "safe" to use elsewhere)
// note that there are only two discrete shapes here...
size(500,500,JAVA2D);
background(#CC9966);
fill(255);
stroke(0);
// "o" is compound because it has a hole
beginShape();
// outer portion
vertex(100,200);
vertex(250,200);
vertex(250,400);
vertex(100,400);
vertex(100,200); // must close manually (if you want the stroke)
// the hole
breakShape();
vertex(150,250);
vertex(150,350);
vertex(200,350);
vertex(200,250);
endShape(CLOSE); // ok to let p5 close it
// "i" is compound because it is disjoint
beginShape();
// base portion
vertex(300,200);
vertex(400,200);
vertex(400,400);
vertex(300,400);
vertex(300,200); // must close manually (if you want the stroke)
// the dot
breakShape();
vertex(300,100);
vertex(400,100);
vertex(400,150);
vertex(300,150);
endShape(CLOSE); // ok to let p5 close it
// now try making a capital greek theta
// (hint: it'll require 2 breakShapes()'s)
Page Index Toggle Pages: 1