I am wondering if anyone here can help me with some pseudocode or at least point me in the right direction on how to draw a circle segment without anti-aliasing.
3 Answers
The formula for points on a circle are:
x = xcenter + radius * sin(theta)
y = ycenter + radius * cos(theta)
where xcenter
and ycenter
are the center of the circle, radius
is the radius and theta
is the angle.
You simply have to iterate theta
from your starting angle to your ending angle in small enough steps, and extract the x
and y
values for plotting, and keep in mind that most trigonometric functions take their arguments as radians (0 through 2*PI) rather than degrees (0 through 360) - adjust your start and end angles and your theta
step to take this into account.
Pseudo-code would be something like the following:
def plotCirle (xc, yc, rad, start, end):
theta = start
while theta <= end:
x = xc + rad * sin (theta)
y = yc + rad * cos (theta)
plot (x, y)
theta = theta + 0.01
although you'd probably want to normalise the angles to be between 0 and 2*PI, and then swap the start and end angles if the former is greater than the latter.
If you want more efficient code, you can look into the midpoint circle algorithm. The math is heavier and it's slightly complicated by the requirement that you only want a segment (meaning you'll need to know the angle, something not usually necessary for this algorithm with a full circle) but that might be a good starting point if the simplistic algorithm above is not fast enough.

- 854,327
- 234
- 1,573
- 1,953
-
Thanks, got it working. But is there a integer math only version of this algorithm? – Mattivc Jan 17 '12 at 01:58
-
You can work with integers if you want but the standard trig functions will expect floating point values. I've built integer trig functions before using scaled up values (0-360 becomes 0-3600) and lookup tables for the values but that was in my old days in embedded space when these functions were far more expensive timewise. There are also more efficient circle drawing algorithms (look up bresenham or midpoint circle) but that requires more math than I can comfortably fit in an answer. – paxdiablo Jan 17 '12 at 02:06
Result.X := Round( fCenter.X + cos(Angle/180*pi)* Radius);
Result.Y := Round( fCenter.Y + sin(Angle/180*pi)* Radius);

- 1,127
- 8
- 14
For integer-only circle-drawing, see wikipedia's midpoint circle algorithm article. It presents, with code, a sort of circle-variant of Bresenham's line algorithm. See codecircle for comparisons (with codes) of midpoint circle algorithm, Bresenham's circle algorithm, and an optimized third method.

- 8,593
- 2
- 22
- 37