#### Archived

This topic is now archived and is closed to further replies.

# n sided pyramid

This topic is 5684 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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!

##### Share on other sites
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)

##### Share on other sites
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);
}

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);
}

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!

##### Share on other sites
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);
}

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);
}

glEnd();
}

1. 1
2. 2
3. 3
4. 4
Rutin
19
5. 5

• 14
• 14
• 9
• 9
• 9
• ### Forum Statistics

• Total Topics
632927
• Total Posts
3009253
• ### Who's Online (See full list)

There are no registered users currently online

×