So I was thinking about how I'd draw a circle, but without using the SIN and COS functions in the software. Instead, I'm using the Pythagorean theorem.

So what do we know about circles? A circle is the graph of all points that are equidistant from the center point. In other words, all of the points on a circle are the same distance from the center.

In graphics, any time we talk "distance", we might invoke Pythagoras. Anyone who has taken high school geometry probably remembers the formula:

a²+b²=c²

Re-written for a 2D graphics plane, we can say

x²+y²=d²

where d is the distance from the starting point, and x and y are the horizontal and vertical distance between the start and end points.

Normally, we solve this for d, getting the distance to a point, but what if we wanted to solve this for the x component? So I transformed the formula into...

d² = x² + y²

d² - y² = x² + y² - y²

d² - y² = x²

x² = d² - y²

x = √(d²-y²)

finally, we convert this to program code:

x = sqrt(d*d - y*y)

What this actually gives us is an arc...., like this:

So how to turn that into a circle? We just plot the same point by negating the x offset, then repeat with the y offset negated:

x = sqrt(d*d - y*y)

x = -sqrt(d*d - y*y)

x = sqrt(d*d - -y*-y)

x = -sqrt(d*d - -y*-y)

and some BASIC code:

This creates a filled circle, by drawing from the X axis out to the edge. If we want a filled circle, we should instead draw a line from the previous point to the next one. This prevents the broken arc we saw in the first image:

Here's the program listing:

And, for fun, here is an animated GIF showing the whole sequence:

So what do we know about circles? A circle is the graph of all points that are equidistant from the center point. In other words, all of the points on a circle are the same distance from the center.

In graphics, any time we talk "distance", we might invoke Pythagoras. Anyone who has taken high school geometry probably remembers the formula:

a²+b²=c²

Re-written for a 2D graphics plane, we can say

x²+y²=d²

where d is the distance from the starting point, and x and y are the horizontal and vertical distance between the start and end points.

Normally, we solve this for d, getting the distance to a point, but what if we wanted to solve this for the x component? So I transformed the formula into...

d² = x² + y²

d² - y² = x² + y² - y²

d² - y² = x²

x² = d² - y²

x = √(d²-y²)

finally, we convert this to program code:

x = sqrt(d*d - y*y)

What this actually gives us is an arc...., like this:

So how to turn that into a circle? We just plot the same point by negating the x offset, then repeat with the y offset negated:

x = sqrt(d*d - y*y)

x = -sqrt(d*d - y*y)

x = sqrt(d*d - -y*-y)

x = -sqrt(d*d - -y*-y)

and some BASIC code:

This creates a filled circle, by drawing from the X axis out to the edge. If we want a filled circle, we should instead draw a line from the previous point to the next one. This prevents the broken arc we saw in the first image:

Here's the program listing:

Code:

```
10 SCREEN $80
20 RECT 0,0,319,199,12
30 X=160:Y=100:R=75:C=2
100 FOR YO=0 TO R
110 XO = SQR(R*R - YO*YO)
130 LINE X-XO,Y+YO,X+XO,Y+YO,C
140 LINE X+XO,Y-YO,X-XO,Y-YO,C
170 LX=XO:LY=YO
190 NEXT
200 C=5
210 LX=R:LY=Y
220 FOR YO=0 TO R
230 XO = SQR(R*R - YO*YO)
240 LINE X+LX,Y+YO,X+XO,Y+YO,C
250 LINE X-LX,Y+YO,X-XO,Y+YO,C
260 LINE X+LX,Y-YO,X+XO,Y-YO,C
270 LINE X-LX,Y-YO,X-XO,Y-YO,C
280 LX=XO:LY=YO
290 NEXT
300 C=1
310 PSET X,Y,C
```

Last edited: