- Include
math.h
and globally declare a constant for PI
#include <math.h>
#define PI 3.14159
- Declare global arrays (for circles or other nGons that you intend to use), to be filled with values. Here is an example.
enum {x,y};
typedef GLdouble vertexType [2];
#define CIRCLE_SIZE 64
vertexType circle1[CIRCLE_SIZE];
vertexType circle2[CIRCLE_SIZE];
vertexType equilateral[3];
vertexType pentagon[5];
- Write a general nGon function based on the following pseudocode.
IN: an array (nGon) of vertexType,
the xCentre (double) of the nGon,the yCentre of the nGon,
the numVertices (integer) of the nGon,
the radius (double) of the nGon
angle = 2 * PI / numVertices
for(i=0;i<numVertices;i++){
nGon[i][x]=centreX+radius*cos(i*angle)
nGon[i][y]=centreY+radius*sin(i*angle)
}
- Write an
initialise()
function to be called from the top of main
eg.
calculateNgon(equilateral,2.0,1.0,3,1.5);
calculateNgon(circle1,0.0,0.0,CIRCLE_SIZE,1.0);
calculateNgon(pentagon,1.5,1.5,5,0.5);
- To render the nGons, reuse your general
render
function. Eg. call the following from display
render(circle1,CIRCLE_SIZE,GL_POLYGON);
render(equilateral,3,GL_LINE_LOOP);