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 › Can you tell me why
Page Index Toggle Pages: 1
Can you tell me why? (Read 467 times)
Can you tell me why?
Dec 1st, 2007, 10:43pm
 
Hey everyone,

I'm currently learning Processing so I'm currently writing many programs just to practice. Recently  I tried to make a polygon drawing app but it doesn't seem to work. Can you spot the error? Thank you.

Quote:

//Geometry
int polygonSides = 3;
float radius = 20;

void setup()
{
 size(800,600);
 smooth();
 fill(255);
 stroke(255);  
 background(0);
}

void draw()
{
 pushMatrix();  
 translate(width/2,height/2);

 beginShape();
 for(int i=0; i<polygonSides; i++)
 {
   vertex(0,radius);
   rotate(TWO_PI/polygonSides);
 }
 endShape(CLOSE);

 popMatrix();
}
Re: Can you tell me why?
Reply #1 - Dec 2nd, 2007, 12:07am
 
Quote from the beginShape() reference:
"
Transformations such as translate(), rotate(), and scale() do not work within beginShape()
"

So you'll have to calculate each vertex's cartesian coordinates using sin()...
Re: Can you tell me why?
Reply #2 - Dec 7th, 2007, 7:20pm
 
So here's a slightly modified code that should work ...

//Geometry
int polygonSides = 12;
float radius = 20;
float ypos, xpos;

void setup()
{
 size(200,200);
 smooth();
 fill(255);
 stroke(255);  
 noLoop();
 background(0);
}

void draw()
{
 // pushMatrix();  
 translate(width/2,height/2);

 beginShape();
 for(int i=1; i <= polygonSides; i++)
 {
   xpos = cos((TWO_PI*i/polygonSides);
   println("xpos :"+xpos);
   ypos = sin((TWO_PI*i/polygonSides);
    println("ypos :"+ypos);
   vertex(xpos*radius, ypos*radius);
 //  rotate(TWO_PI/polygonSides);
 println(i);
 }
 endShape(CLOSE);

//  popMatrix();
}
Page Index Toggle Pages: 1