Skip to content
Snippets Groups Projects

Gfx

Merged joey.martig requested to merge gfx into main
10 files
+ 164
57
Compare changes
  • Side-by-side
  • Inline
Files
10
src/draw.c 0 → 100644
+ 66
0
 
#include "../utils/gfx/gfx.h"
 
#include "field.h"
 
/*
 
(50, 50) → (75, 50)1 , (50, 50) → (72, 62), (50, 50) → (62, 72)
 
(50, 50) → (50, 75), (50, 50) → (38, 72), (50, 50) → (28, 62)
 
(50, 50) → (25, 50), (50, 50) → (28, 38), (50, 50) → (37, 28)
 
(50, 50) → (50, 25), (50, 50) → (62, 28), (50, 50) → (72, 37)
 
*/
 
 
void gfx_draw_line(struct gfx_context_t *ctxt, coordinates_t p0, coordinates_t p1, uint32_t color) {
 
int dx = abs(p1.column - p0.column);
 
int sx = p0.column < p1.column ? 1 : -1;
 
int dy = -abs(p1.row - p0.row);
 
int sy = p0.row < p1.row ? 1 : -1;
 
int error = dx + dy;
 
 
while (true) {
 
gfx_putpixel(ctxt, p0.column, p0.row, color);
 
 
if (p0.column == p1.column && p0.row == p1.row)
 
break;
 
 
int e2 = 2 * error;
 
 
if (e2 >= dy) {
 
if (p0.column == p1.column)
 
break;
 
error = error + dy;
 
p0.column = p0.column + sx;
 
}
 
 
if (e2 <= dx) {
 
if (p0.row == p1.row)
 
break;
 
error = error + dx;
 
p0.row = p0.row + sy;
 
}
 
}
 
}
 
 
void gfx_draw_circle(struct gfx_context_t *ctxt, coordinates c, uint32_t r, uint32_t color) {
 
int x = 0;
 
int y = r;
 
int d = r - 1;
 
while(y >= x){
 
gfx_putpixel(ctxt, c.row + x, c.column + y, color);
 
gfx_putpixel(ctxt, c.row + y, c.column + x, color);
 
gfx_putpixel(ctxt, c.row - x, c.column + y, color);
 
gfx_putpixel(ctxt, c.row - y, c.column + x, color);
 
gfx_putpixel(ctxt, c.row + x, c.column - y, color);
 
gfx_putpixel(ctxt, c.row + y, c.column - x, color);
 
gfx_putpixel(ctxt, c.row - x, c.column - y, color);
 
gfx_putpixel(ctxt, c.row - y, c.column - x, color);
 
if(d >= 2*x){
 
d = d-2*x-1;
 
x++;
 
}else if(d < 2 * (r-y)){
 
d = d + 2*y-1;
 
y--;
 
}else {
 
d = d + 2 * (y-x-1);
 
y--;
 
x++;
 
}
 
}
 
}
Loading