n sided pyramid

Started by
2 comments, last by BungoMan85 21 years ago
i seem to be having some trouble with creating an n sided pyramid (where n is greater than 3)... anyone know of any algorithms that can be used to create one? or at least an n sided equilateral polygon. im thinking its gonna involve lots of triangles... but im not sure =/ Bungo!
Bungo!
Advertisement
You should be able to create an n-sided polygon fairly easily using a little trig and a triangle fan.

The unit circle contains 2Pi radians. For an N-sided polygon, there are N points each spaced 2*Pi / N radians apart. To draw such a polygon, you could use a triangle fan, with the first vertex being the center, and the rest of the vertices being points around the circle.

Asumming the center is at 0,0 you could have something liek this:


  void DrawPolygon(int nSides, float fRadius){    float fStep = 2 * Pi / nSides;    glBegin(GL_TRIANGLE_FAN);    glVertex3f(0, 0, 0);        for(float f = 0.0f; f <= 2 * Pi; f+= fStep)    {        glVertex3f(fRadius * cosf(f), fRadisu * sinf(f), 0);    }   glEnd();}  


I think that would work, I will try it later when I get home.

A pyramid could be drawn the same way, except the z value of the center point as opposed to the outside points would be different.

-Evan

---------------------------
Those who dance are considered insane by those who cannot hear the music.

Focus On: 3-D Models
Evan Pipho (evan@codershq.com)
Those who dance are considered insane by those who cannot hear the music.
here is what i got so far... it sorta does what i need it to do


void pyramid(int sides, float radius, float height, float xoffset, float yoffset, float zoffset, unsigned char red, unsigned char green, unsigned char blue)
{
float degrees=2*Pi/sides;
glBegin(GL_TRIANGLE_FAN);
glVertex3f(xoffset,yoffset,zoffset);

for (float f=0.0f;f<=2*Pi;f+=degrees)
{
glColor3ub(red,green,blue);
glVertex3f(radius*cosf(f)+yoffset,radius*sinf(f)+xoffset,zoffset);
}

glEnd();

glBegin(GL_TRIANGLE_FAN);
glVertex3f(xoffset,yoffset,zoffset+height);


for (f=0.0f;f<=2*Pi;f+=degrees)
{
red++;
green++;
blue++;
glColor3ub(red,green,blue);
glVertex3f(radius*cosf(f)+yoffset,radius*sinf(f)+xoffset,zoffset);
}

glEnd();
}


the problem is it isnt oriented the way i need it..
it looks like

/|
/ |
/ |
\ |
\ |
\|

and i need to to look like

/\
/ \
/ \
/______\

really if i could get it so that it can be oriented any which way would be optimal. i just dont know what quite to do, i know it will involve changing the relative position of the coordinates for the vertecies in the for loops, but i dont know which ones to change (bah, i wish i knew more geometry)

Bungo!
Bungo!
I swapped z and y
............................
void pyramid(int sides, float radius, float height, float xoffset, float yoffset, float zoffset, unsigned char red, unsigned char green, unsigned char blue)
{
float degrees=2*Pi/sides;
glBegin(GL_TRIANGLE_FAN);
glVertex3f(xoffset,yoffset,zoffset);

for (float f=0.0f;f<=2*Pi;f+=degrees)
{
glColor3ub(red,green,blue);
glVertex3f(radius*cosf(f)+yoffset,radius*sinf(f)+xoffset,zoffset);
}

glEnd();

glBegin(GL_TRIANGLE_FAN);
glVertex3f(xoffset,yoffset+height,zoffset);


for (f=0.0f;f<=2*Pi;f+=degrees)
{
red++;
green++;
blue++;
glColor3ub(red,green,blue);
glVertex3f(radius*cosf(f)+xoffset,yoffset, radius*sinf(f)+zoffset);
}

glEnd();
}

This topic is closed to new replies.

Advertisement