Draw a centered circle I've written this program that, renders a circle in the middle of the screen. It's been written in a very elementary way.

Any suggestions on principles or techniques? Or any more optimal or efficient ways of drawing circles in C?

I'm using SDL2 (Simple Direct Media Library) and GCC to compile.

#include "SDL.h"
#include &lt;math.h&gt;
#include &lt;stdio.h&gt;

#define PI  3.14159
#define SCR_WDT  1280
#define SCR_HGT  960

const int SCR_CEN_X = SCR_WDT / 2;
const int SCR_CEN_Y = SCR_HGT / 2;

struct Circle
{
    int radius;
    int h; // The X access of the center of the circle.
    int k; // The Y access of the center of the circle.
    int new_x;
    int new_y;
    int old_x;
    int old_y;
    float step; 
};

int main ( int argc, char *argv[] )
{
    SDL_Init ( SDL_INIT_VIDEO ); 
    SDL_Window *window = SDL_CreateWindow ( "Drawing a Circle",       SDL_WINDOWPOS_UNDEFINED, 
                                             SDL_WINDOWPOS_UNDEFINED, SCR_WDT, SCR_HGT, 0 );
    SDL_Renderer *renderer = SDL_CreateRenderer ( window, -1, SDL_RENDERER_SOFTWARE );

    Circle circle;
    circle.radius = 200;
    circle.h = SCR_CEN_X;
    circle.k = SCR_CEN_Y;
    circle.new_x = 0;
    circle.new_y = 0;
    circle.old_x = 0;
    circle.old_y = 0;
    circle.step = (PI * 2) / 50;

    bool is_running = true;
    while ( is_running )
    {
        SDL_Event event;
        if ( SDL_PollEvent( &amp;event ))
        {
            if ( event.type == SDL_QUIT )
            {
                is_running = false;
            }   
        }

        SDL_RenderClear ( renderer );

        SDL_SetRenderDrawColor ( renderer, 255, 255, 255, 255 );

        for ( float theta = 0; theta &lt; (PI * 2); theta += circle.step )
        {
            circle.new_x = circle.h + (circle.radius * cos ( theta ));
            circle.new_y = circle.k - (circle.radius * sin ( theta ));  

            SDL_RenderDrawLine ( renderer, circle.old_x, circle.old_y,
                                 circle.new_x, circle.new_y );

            circle.old_x = circle.new_x;
            circle.old_y = circle.new_y;

        }

        SDL_SetRenderDrawColor ( renderer, 0, 0, 0, 255 );
        SDL_RenderPresent ( renderer ); 
    }

    SDL_Quit ();

return 0; 
}
Doesn't SDL have a function for drawing a circle, or are you just doing that as practise?

#define PI  3.14159


math.h should have M_PI.

struct Circle {
    int radius;
    int h; // The X access of the center of the circle.
    int k; // The Y access of the center of the circle.


Just call them x and y, or center_x and center_y.

int new_x; int new_y; int old_x; int old_y; float step; 


These should be in the function that draws the circle, as they're part of the logic of drawing, not the circle as an object.

int main (int argc, char *argv[])
    Circle circle;
    circle.radius = 200;
    circle.h = SCR_CEN_X;


Make a function to initialize the Circle struct, esp. as you have fields that are initialized to as uninteresting a value as zero.

        if ( event.type == SDL_QUIT ) {
            is_running = false;


I would break out of the loop here, since you're going to exit soon anyway.

    for ( float theta = 0; theta &lt; (PI * 2); theta += circle.step ) {
        circle.new_x = circle.h + (circle.radius * cos ( theta ));
        circle.new_y = circle.k - (circle.radius * sin ( theta )); 
        SDL_RenderDrawLine ( renderer, circle.old_x, circle.old_y,
                             circle.new_x, circle.new_y );


Put the circle drawing in a function, and make new_x, new_y etc. locals to it. As for step, you might want to scale it based on the circle's radius. Or make it a parameter to the function.

I think you have a sort of an off-by-one error here, as (1) on the first iteration of the loop, you draw starting from (old_x, old_y), but old_x and old_y aren't set yet. And (2) when the loop ends, you probably want to draw a line from the last point (at angle 49/50*2*pi) to the very first point (at angle 0) to complete the circle.

Also, decide if you're writing C or C++. You can't define a struct Circle as just Circle foo in C, and in C++, you should make the class have a proper constructor.