Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Hello I've been trying for ages to make a pixel circle using the putpixel function provided by SDL main website here is that function :

void putpixel(int x,int y , int color , SDL_Surface* surface)
{
    unsigned int *ptr = static_cast <unsigned int *> (surface->pixels);

    int offset = y * (surface->pitch/sizeof(unsigned int));

    ptr[offset + x] = color;
}

and my question is how do I curve a line or create an circle arc of pixels or any other curved shape then a rectangle or singular pixel or line.

for example here are some pictures

filled pixel circle below enter link description here

now my idea was too

change the x and y value of the pixel position using + and - to create the curves but in practice didn't provide the correct results

what my results are in this is to be able to create a circle that is made out of pixels only nothing else.

thank you for anyone who takes the time to read this question

thanks! :D

share|improve this question
1  
You might want to improve your accept rate if you want people to answer –  Ken Nov 17 '12 at 19:13

3 Answers 3

To draw a circle one pixel at at time, you can do something like this

int centrex=100,centrey=100;// centre of circle in pixel coords
int radius=50;

float two_pi=6.283f;

float angle_inc=1.0f/radius;
for(float angle=0.0f; angle<= two_pi;angle+=angle_inc){
    xpos=centrex+radius*cos(angle);
    ypos=centrey+radius*sin(angle);
    putpixel(x,pos,ypos,surface);

}

This method considers a circle like a bicycle wheel and draws a pixel at the end of each spoke. This is not the most efficient way to draw a circle using pixels (checkout the midpoint algorithm). But it will get you started.

share|improve this answer
    
I approve this message. Start with something you understand (like what Ken suggests for circles, or y = Ax+B for straight line segments), and then learn the "real" algorithms (e.g. Bresenham) –  ggambett Dec 13 '12 at 9:41

For curves you can use Bezier Curve. The parametric equation is what you are looking for ;)

Using Bezier Curves, you can draw lines too, all you have to do is use the Linear Bezier Curve.

For circles, you can also use parametric equations, and a quick Google returned me this awesome website: Link!.

But one can also use something like Midpoint Circle for filled circles. See this question: http://stackoverflow.com/questions/1201200/fast-algorithm-for-drawing-filled-circles

share|improve this answer

Read up on Bresenham's algorithms. First here for a line drawing algorithm, and once you understand how that works here for the actual circle algorithm.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.