Drawing Circles

It's not important for National Curriculum Computing at KS3 that you understand how to create a program to draw a circle, but, if you're interested, here are two techniques explained. GCSE Maths now includes trigonometry and Pythagoras in both tiers, so these techniques should be suitable for students of GCSE Computer Science.

Trigonometry

Trigonometry

GCSE Maths students will have been introduced to trigonometry - sines and cosines - for their Maths exam. If you don't know what sines and cosines are, or you would like a reminder of what they are, check out the trigonometry page.

This method of drawing a circle works by varying the angle n and using trigonometry to work out where the point (x,y) would be for that angle. As you could hopefully see from the animation, for a circle of radius 1, x would be cos(n) and y would be sin(n). For a larger circle, you just need to multiply by the radius. The program draws a circle of radius 400, so the x-coordinate is 400 x cos(n), and the y-coordinate is 400 x sin(n).

There is, however, one slight complication. Unlike your calculator, most programming languages (and applications such as Excel) measure angles in radians, rather than degrees. The program therefore uses the RAD() function to convert the angle from degrees to radians before calculating the sine or cosine.

Pythagoras

Pythagoras

You might find the Maths for this method more straightforward - and so does the computer! Square roots are quicker for the computer to calculate than sines and cosines, which explains why this method is quicker.

Pythagoras works with right-angled triangles. Imagine a triangle with one corner in the centre of the circle, and another on the circumference. The hypotenuse (the longest side) will always be equal in length to the radius.

This method of drawing the circle varies the x-position. From the x-position, we know the width of the triangle, and we know the length of the hypotenuse, so we can use Pythagoras to work out the height of the triangle to give us the y-position.

If we assume that (0, 0) is in the middle of the circle, then the value of x is the width of the triangle - it might be negative, but as we're going to square it, it doesn't matter. If the hypotenuse is 400, then y2 = 4002 - x2, or y is the square root of (160000 - x2).

Randomness

More recently I've discovered that you can use randomness to plot points on the circumference of a circle - the method is described on Guilherme Kunigami's blog, Circles and Randomness and is included my my file of BBC BASIC programs (see my blog In Praise of Slowness).

This method has the benefit of only using arithmetic - no sines, cosines, or square roots - but the downside is that the points on the circumference aren't joined, so you need more of them to give the appearance of a full circle.

I've created a version of the random circle using Python with Turtle, but you could even do it in any programming language that allows you to plot points, including Scratch.

Random numbers can be used for all sorts of things, including using the Monte Carlo Method for calculating pi.

Examples

If you would like to see how these techniques work in practice, watch the video on programming efficiency on the AdvancedICT YouTube channel.

If you would like to demonstrate this techniques to your students yourself using a BBC Model B emulator (or even a real one - there are still plenty around) then have a look at my blog, In Praise of Slowness.