Split render primitives into dedicated modules

This commit is contained in:
Stanislav N Mikhailov
2026-03-24 21:52:09 +03:00
parent 00b4bfd3d8
commit 0a9b9eb741
8 changed files with 181 additions and 0 deletions
+41
View File
@@ -0,0 +1,41 @@
#include "display/render/bezier.h"
#include <math.h>
static float bernstein(int i, int n, float t)
{
float binomial = 1.0f;
for (int j = 0; j < i; ++j)
{
binomial *= (float)(n - j) / (float)(j + 1);
}
return binomial * powf(t, (float)i) * powf(1.0f - t, (float)(n - i));
}
void render_bezier(
render_ctx_t* ctx,
const int* points_x,
const int* points_y,
size_t num_points,
uint16_t color
)
{
if (num_points < 2)
return;
const int steps = 1000;
for (int s = 0; s <= steps; ++s)
{
float t = (float)s / (float)steps;
float x = 0.0f;
float y = 0.0f;
for (size_t i = 0; i < num_points; ++i)
{
float b = bernstein((int)i, (int)num_points - 1, t);
x += b * (float)points_x[i];
y += b * (float)points_y[i];
}
render_pixel(ctx, (int)(x + 0.5f), (int)(y + 0.5f), color);
}
}
+17
View File
@@ -0,0 +1,17 @@
#include "display/render/grid.h"
void render_grid(render_ctx_t* ctx, uint16_t x, uint16_t y, uint16_t step, uint16_t color)
{
if (step == 0 || ctx->width == 0 || ctx->height == 0)
return;
for (uint16_t v = x; v < ctx->width; v = (uint16_t)(v + step))
{
render_line(ctx, v, 0, v, (int)ctx->height - 1, color);
}
for (uint16_t h = y; h < ctx->height; h = (uint16_t)(h + step))
{
render_line(ctx, 0, h, (int)ctx->width - 1, h, color);
}
}
+29
View File
@@ -0,0 +1,29 @@
#include "display/render/line.h"
void render_line(render_ctx_t* ctx, int x0, int y0, int x1, int y1, uint16_t color)
{
int dx = (x1 >= x0) ? (x1 - x0) : (x0 - x1);
int sx = (x0 < x1) ? 1 : -1;
int dy = (y1 >= y0) ? (y0 - y1) : (y1 - y0);
int sy = (y0 < y1) ? 1 : -1;
int err = dx + dy;
while (1)
{
render_pixel(ctx, x0, y0, color);
if (x0 == x1 && y0 == y1)
break;
int e2 = err * 2;
if (e2 >= dy)
{
err += dy;
x0 += sx;
}
if (e2 <= dx)
{
err += dx;
y0 += sy;
}
}
}
+27
View File
@@ -0,0 +1,27 @@
#include "display/render/sine_wave.h"
#include <math.h>
void render_sine_wave(
render_ctx_t* ctx,
uint16_t num_points,
int amplitude,
float frequency,
int offset_x,
int offset_y,
float phase_shift,
uint16_t color
)
{
if (num_points < 2 || ctx->width == 0 || ctx->height == 0)
return;
float step = (2.0f * (float)M_PI * frequency) / (float)(num_points - 1u);
float x_step = (float)ctx->width / (float)(num_points - 1u);
for (uint16_t i = 0; i < num_points; ++i)
{
int x = offset_x + (int)((float)i * x_step);
int y = offset_y + (int)((float)amplitude * sinf((float)i * step + phase_shift));
render_pixel(ctx, x, y, color);
}
}