/** @file primitives.c
 *
 *  A versatile software tool to analyse and configure CANopen devices.
 *
 *  Copyright (c) 2026, Michael Fitzmayer. All rights reserved.
 *  SPDX-License-Identifier: MIT
 *
 **/

#include "os.h"

void draw_circle(os_renderer* renderer, int cx, int cy, int radius, bool fill)
{
    int x = 0;
    int y = radius;
    int d = 3 - 2 * radius;

    while (x <= y)
    {
        if (fill)
        {
            for (int i = (cx - x); i <= (cx + x); i++)
            {
                os_draw_pixel(renderer, i, cy + y);
                os_draw_pixel(renderer, i, cy - y);
            }
            for (int i = (cx - y); i <= (cx + y); i++)
            {
                os_draw_pixel(renderer, i, cy + x);
                os_draw_pixel(renderer, i, cy - x);
            }
        }
        else
        {
            os_draw_pixel(renderer, cx + x, cy + y);
            os_draw_pixel(renderer, cx - x, cy + y);
            os_draw_pixel(renderer, cx + x, cy - y);
            os_draw_pixel(renderer, cx - x, cy - y);
            os_draw_pixel(renderer, cx + y, cy + x);
            os_draw_pixel(renderer, cx - y, cy + x);
            os_draw_pixel(renderer, cx + y, cy - x);
            os_draw_pixel(renderer, cx - y, cy - x);
        }

        if (d < 0)
        {
            d += 4 * x + 6;
        }
        else
        {
            d += 4 * (x - y) + 10;
            y--;
        }
        x++;
    }
}
