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 & HelpPrograms › Shape() with hole
Page Index Toggle Pages: 1
Shape() with hole (Read 617 times)
Shape() with hole
Aug 15th, 2007, 9:24pm
 
This will fill a 2D polygonal shape, the 2nd overlapping the 1st:

fill(somecolor,somealpha);  
beginShape();
vertex(10,10);vertex(5,10);vertex(5,5);vertex(10,5);
endShape(CLOSE);fill(somecolor,somealpha);  
beginShape();
vertex(20,20);vertex(0,20);vertex(0,0);vertex(20,0);
endShape(CLOSE);

Can I fill a shape with a hole ? without filling the hole ?
I mean fill the 2nd polygon without overlapping the polygonal hole?

Of course it "can" be done, but can it be done simply ?
using only the ..Shape() methods ?

Any suggestion ? Thanks, alphachapmtl

(if you email me, I can send you a .png image showing polygonal rings I need to fill)
Re: Shape() with hole
Reply #1 - Aug 15th, 2007, 10:54pm
 
there's currently no built-in means for doing it using the main API. you might try a way to hack around it, or use a library (geomerative might support this).
Re: Shape() with hole
Reply #2 - Aug 16th, 2007, 8:03pm
 
Can I cut/copy/paste a polygonal region ?

That way I could save the hole and restore it later.
Re: Shape() with hole
Reply #3 - Aug 19th, 2007, 12:08pm
 
The following message from <JohnG> gives a solution:

One possible way is to pick a vertex on the outer poly, and then find the closest vertex on the inner, and use that as an invisible break.. so you just draw one poly, like so:

Draw from "outside break" vertex around the outside back to itself, but instead of ending there, then draw the inside one, and go around that, in the opposite direction, back to the inside break, and then the last vertex is the outside break again.

This test program shows an example:

//polygonhole1
//2007-aug-17
//alphachapmtl

int x0,y0;

void setup() {
 size(900,400);  
 background(0);

 // square_cyclic
 x0=width/6;
 y0=height/2;
 for (int i=0;i<8;i++) {
   int delta=20*i;
   stroke(0,0,200,50);
   fill(200,0,0,50);
   beginShape();//cyclic order
   vertex(x0-delta, y0-delta);
   vertex(x0-delta, y0+delta);
   vertex(x0+delta, y0+delta);
   vertex(x0+delta, y0-delta);
   endShape(CLOSE);
 }//for

 // square_cyclic + insquare_cyclic
 x0=3*width/6;
 y0=height/2;
 for (int i=0;i<8;i++) {
   int delta=20*i;
   stroke(0,0,200,50);
   fill(200,0,0,50);
   beginShape();//cyclic order
   vertex(x0-delta, y0-delta);
   vertex(x0-delta, y0+delta);
   vertex(x0+delta, y0+delta);
   vertex(x0+delta, y0-delta);
   vertex(x0-delta, y0-delta);
   if (i>0) {//cyclic order
     int deltaOld=delta-20;
     vertex(x0-deltaOld, y0-deltaOld);
     vertex(x0-deltaOld, y0+deltaOld);
     vertex(x0+deltaOld, y0+deltaOld);
     vertex(x0+deltaOld, y0-deltaOld);
     vertex(x0-deltaOld, y0-deltaOld);
     vertex(x0-delta, y0-delta);
   }
   endShape();
 }//for

 // square_cyclic + insquare_anticyclic >> THIS WORKS !!
 x0=5*width/6;
 y0=height/2;
 for (int i=0;i<8;i++) {
   int delta=20*i;
   stroke(0,0,200,50);
   fill(200,0,0,50);
   beginShape();//cyclic order
   vertex(x0-delta, y0-delta);
   vertex(x0-delta, y0+delta);
   vertex(x0+delta, y0+delta);
   vertex(x0+delta, y0-delta);
   vertex(x0-delta, y0-delta);
   if (i>0) {//anticyclic order
     int deltaOld=delta-20;
     vertex(x0-deltaOld, y0-deltaOld);
     vertex(x0+deltaOld, y0-deltaOld);
     vertex(x0+deltaOld, y0+deltaOld);      
     vertex(x0-deltaOld, y0+deltaOld);
     vertex(x0-deltaOld, y0-deltaOld);
     vertex(x0-delta, y0-delta);
   }
   endShape();
 }//for

 exit();
}

void draw() {
}
Page Index Toggle Pages: 1