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 › polygon drawing Help!!
Page Index Toggle Pages: 1
polygon drawing Help!! (Read 788 times)
polygon drawing Help!!
Feb 12th, 2008, 1:07pm
 
I am trying to make a program which draws polygons. depending on the amount of sides I set, all polygons must keep the same perimiter and no vertex of the polygon can be less than 100 pixels from the nearest window edge.

I know I must use turtle graphics to draw regular N sided polygons. But i do not know how to keep the same perimiter and to keep the vertices of the polygon 100 pixels away from the window edge.
This is what i have doen so far
int N = 4;
int l = 100;
void setup ()
{
 size(513,513);
 background(255);
 strokeWeight(2);
 smooth();

//ellipse(256,256,313,313);
}
void translateDrawPolygon(int x, int y , int N,int l)
 {
 translate (x,y);
 drawPolygon(N,l);
 }
void drawPolygon(int N, int l)
{
for(int i=0;i<N;i++)
{
line(0,0,l,0);
translate(l,0);
rotate(TWO_PI/N);
}
}
void draw ()
{
translateDrawPolygon(145,145,N,l);
}

Please Help
Re: polygon drawing Help!!
Reply #1 - Feb 13th, 2008, 5:20am
 
I think you'll find it easier to use beginShape() and vertex().

http://processing.org/reference/beginShape_.html
Re: polygon drawing Help!!
Reply #2 - Feb 13th, 2008, 9:21am
 
beginShape is the right way to start and as you want draw an regular polygon its very simple to check the distance. All your vertices must lie on a circle with a given radius, so you have to check if the middle point of your polygon is fulfilling the following rules:

Code:

x>radius+100 && x<width-radius-100 && y>radius+100 && y<height-radius-100


And so you can calculated the vertices:
Code:

for(int i=0; i<vertexCount; i++){
float vertexX=x+cos(i*TWO_PI/vertexCount)*radius;
float vertexY=y+sin(i*TWO_PI/vertexCount)*radius;
}
Re: polygon drawing Help!!
Reply #3 - Feb 13th, 2008, 1:42pm
 
thanks guys
Page Index Toggle Pages: 1