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.
Page Index Toggle Pages: 1
square with hole (Read 1738 times)
square with hole
Aug 25th, 2009, 9:23pm
 
hi, can someone tell me whats wrong with my code? I juste want a sqare with a hole in middle of it. When i finally close all the thing up, it removes the fill color ... can you guys tell me why?
thank you!
here is the code:

Code:
import processing.opengl.*;

int pts = 4;
float angle = 0;
float radius = 99;
float cylinderLength = 95;

//vertices
PVector vertices[][];

float angleInc;

void setup(){
size(640, 360, OPENGL);
stroke(255,0,0);
angleInc = PI/300.0;

// initialize vertex arrays
vertices = new PVector[2][pts+1];

// fill arrays
for (int i = 0; i < 2; i++){
angle = 0;
for(int j = 0; j <= pts; j++){
vertices[i][j] = new PVector();

vertices[i][j].x = cos(radians(angle)) * radius;
vertices[i][j].y = sin(radians(angle)) * radius;

vertices[i][j].z = cylinderLength;
// the .0 after the 360 is critical
angle += 360.0/pts;
}
cylinderLength *= -1;
}

}

void draw(){
background(170, 170, 170);
lights();
translate(width/2, height/2);
rotateX(QUARTER_PI);
rotateZ(QUARTER_PI);

fill(200, 255, 200);
beginShape();

for(int j = 0; j <= pts; j++){
vertex(vertices[0][j].x*2, vertices[0][j].y*2, vertices[0][j].z);
}

for(int j = pts; j >= 0 ; j--){
vertex(vertices[0][j].x, vertices[0][j].y, vertices[0][j].z);
}

endShape(CLOSE);

noLoop();
}

Re: square with hole
Reply #1 - Aug 25th, 2009, 10:25pm
 
If you increase 'pts' to 6 in your code, you'll see a swath of the fill color, so the fill's still happening but something's going wrong when you define the shape.

This is a simplified donut that uses the same pattern your code appears to, and it works, so there is a way...

Quote:
float x1=50, y1=50, x2=350, y2=350;
float xx1=150, yy1=150, xx2=250, yy2=250;

void setup(){
  size(400,400);
  fill(255);
  stroke(0);
  strokeWeight(2);
  beginShape();
  vertex(x1,y1);
  vertex(x2,y1);
  vertex(x2,y2);
  vertex(x1,y2);
  vertex(x1,y1);
  
  vertex(xx1,yy1);
  vertex(xx1,yy2);
  vertex(xx2,yy2);
  vertex(xx2,yy1);
  vertex(xx1,yy1);
  
  endShape(CLOSE);
}


Good luck -- Ben

Edit: oh wait -- looks like my sketch fails once put into OPENGL mode.  Looks like you might need to use TRIANGLE_STRIP  or TRIANGLES.
Re: square with hole
Reply #2 - Aug 25th, 2009, 10:41pm
 
Here's the basic form for a triangle strip method.

Quote:
import processing.opengl.*;

float x1=50, y1=50, x2=350, y2=350;
float xx1=150, yy1=150, xx2=250, yy2=250;

void setup(){
  size(400,400,OPENGL);
  fill(255);
  stroke(0);
  strokeWeight(2);
  beginShape(TRIANGLE_STRIP);
  vertex(x1,y1);
  vertex(xx1,yy1);
  
  vertex(x2,y1);
  vertex(xx2,yy1);
  
  vertex(x2,y2);
  vertex(xx2,yy2);
  
  vertex(x1,y2);
  vertex(xx1,yy2);
  
  vertex(x1,y1);
  vertex(xx1,yy1);
  endShape(CLOSE);
}
Re: square with hole
Reply #3 - Aug 26th, 2009, 3:26am
 
Ha! or use breakShape; Cool
learnt from dave bollinger... undocumented but pretty useful

size(400,400);
fill(255);
stroke(0);

beginShape();
// outer shape
vertex(100,100);
vertex(300,100);
vertex(300,300);
vertex(100,300);
vertex(100,100);  

breakShape();
//cutout
vertex(150,150);
vertex(150,250);
vertex(250,250);
vertex(250,150);
endShape(CLOSE);  
Re: square with hole
Reply #4 - Aug 26th, 2009, 10:11am
 
whoa!
Re: square with hole
Reply #5 - Aug 27th, 2009, 1:38pm
 
Thanks guys, very helpful

FYI, breakShape() is only for P2D

in OPENGL and P3D, i got this error : This renderer cannot currently handle concave shapes, or shapes with holes.

thanks  Smiley
Page Index Toggle Pages: 1