INT32GP Graphics Programming: Tutorial #05
N-gons and Circles

B2.15 3:00pm Mon 12 Aug 02
B1.38 12midday Tues 13 Aug 02

The Babylonians (approximately 2000 years BC) used the degree system to describe the process of turning around in a circle. Some people think they divided the circle into 360 equal parts because they divided one year into 360 days. Others believe the Babylonians knew that a chord equal in length to the radius can be laid off around a circle six times forming a regular 6-gon (hexagon) and that, since their notation for numbers was based on 60, they divided the arc subtended by each chord into 60 equal parts.

In the degree system, if you face in the direction of the x-axis and then turn all the way round in a counter clockwise direction, you turn from 0o to 360o.

  1. Given a unit circle (radius (r)=1) with its centre at the origin (0,0). What are the x and y coordinates of the point at the end of the radius a quarter of the way round the circle?
The circumference of a unit circle in radians is equivalent to 360o. The circumference of a circle is given by 2*PI*r.
  1. What is the circumference of a unit circle?
  2. Express 1o in radians.
  3. Express 1 radian in degrees.
In C, we express angles in radians, so we need to specify a constant value for PI. At school, I was taught that PI is equivalent to 22/7. However, this is a poor approximation.
22/7 = 3.14285 ...      PI = 3.14159 ...
Therefore, we should define PI at the beginning of our programs.
#define PI 3.14159

We can find the values of x1 and y1, a point with distance r from the origin (0,0), using the value of angle according to the following.

x1 = r*cos(angle)

y1 = r*sin(angle)



  1. Given that r=1 and angle=45o, write down the the values of x1 and y1 in radians.

A circle is the set of points in a plane such that each of those points is the same non-negative distance from a fixed point (the centre of the circle) on that plane.

  1. How might we find the values of the x, y coordinates of the points on a unit circle that we reach by turning from 0o to 30o, 60o, and 140o.
In this case, if we know the values of x1, y1, we can calculate x2, y2 according to the following.
x2 = x1 + r*cos(angle)

y2 = x2 + r*sin(angle)



  1. Given that x1=y1=1.5, angle=25o, and r=2.5, what are the values of x2and y2?

If x1,y1 is the centre of a circle, we can calculate the values of points lying on the circumference of the circle.

Here is a general algorithm for calculating the the x,y values of a number of points (vertices) lying at equal distances around the circumference of that circle. Using this we can construct n-gons. If we have a large enough number of vertices, the n-gon approximates the circumference of a circle.

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);
}
  1. Convert this algorithm to C code that renders the resulting shape vertex by vertex.
  2. Convert this algorithm to C code that calculates the x and y values of each vertex, placing them in an array. (The vertices in the array can be used later for rendering).
  3. If we calculate 6 vertices and then join them together, what shape will we have?
  4. Given a radius of 1 unit, what is the length of each side of this shape?

Advanced

The circle is an arc that arcs around from 0o to360o.
  1. Change the n-gon algorithm(s) so that it calculates vertices for any arc (eg. from 0o to180o or from 45o to135o)
Such an algorithm can help you design images with curved lines. Think of sections of the curve as arcs - perhaps of different circles.
  1. Describe the way in which the above image can be constructed.
  2. Design your own images using arcs in some way.
Fran Soddell email:F.Soddell@bendigo.latrobe.edu.au
last updated 12 August 2002