Original Post
🔒 Locked
Drawing Circle
Advertisement
Here is psuedo-code for drawing a circle using trigonometry:
for(theta=0; theta<2PI; theta++)
{
run = radius * cos(theta) + xCenter;
rise = radius * sin(theta) + yCenter;
PlotPixel(run, rise);
}
theta is the angle (in radians) of the current position on the circumference of the circle, run is the x coordinate of the current position and rise is the y coordinate. xCenter and yCenter are the coordinates of the center of the circle. The rest should be pretty self-explainatory.
Keep in mind however, that since this is using integers (theta++) unless you are drawing a really small circle the circumference is not going to be connected - it will be a dotted outline for the circle. A bad way of fixing this (as in slow, although I''ve tested it and lost around 1-3fps with it) is to using floating point numbers instead of integers, and have something like theta+=.01 (you''d have to play around with it a little to get a good addition factor), and you can also loop more (theta<4PI) although that will only work if you are using floating points as well (otherwise you''ll just draw pixels over previous pixels). I''m sure someone can come up with some good optimizing techniques (maybe fixed-point math and look-up tables or some version of brensenhams algorithm?) but that''s the basics of it.
for(theta=0; theta<2PI; theta++)
{
run = radius * cos(theta) + xCenter;
rise = radius * sin(theta) + yCenter;
PlotPixel(run, rise);
}
theta is the angle (in radians) of the current position on the circumference of the circle, run is the x coordinate of the current position and rise is the y coordinate. xCenter and yCenter are the coordinates of the center of the circle. The rest should be pretty self-explainatory.
Keep in mind however, that since this is using integers (theta++) unless you are drawing a really small circle the circumference is not going to be connected - it will be a dotted outline for the circle. A bad way of fixing this (as in slow, although I''ve tested it and lost around 1-3fps with it) is to using floating point numbers instead of integers, and have something like theta+=.01 (you''d have to play around with it a little to get a good addition factor), and you can also loop more (theta<4PI) although that will only work if you are using floating points as well (otherwise you''ll just draw pixels over previous pixels). I''m sure someone can come up with some good optimizing techniques (maybe fixed-point math and look-up tables or some version of brensenhams algorithm?) but that''s the basics of it.
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement