refactor: adjust naming

This commit is contained in:
beriff
2026-03-04 18:32:19 +07:00
parent 1c3fc0c4bd
commit 28d4d4b6ce
5 changed files with 53 additions and 53 deletions

View File

@@ -1,14 +1,14 @@
#include "primitives.h" #include "primitives.h"
Polyline *mk_polyline(void) { VektorPolyline *vektor_polyline_new(void) {
Polyline *pl = malloc(sizeof(Polyline)); VektorPolyline *pl = malloc(sizeof(VektorPolyline));
pl->count = 0; pl->count = 0;
pl->capacity = 4; pl->capacity = 4;
pl->points = malloc(sizeof(V2) * pl->capacity); pl->points = malloc(sizeof(V2) * pl->capacity);
return pl; return pl;
} }
void add_point_polyline(Polyline *pl, V2 point) { void vektor_polyline_add_point(VektorPolyline *pl, V2 point) {
if (pl->count >= pl->capacity) { if (pl->count >= pl->capacity) {
pl->capacity *= 2; pl->capacity *= 2;
pl->points = realloc(pl->points, sizeof(V2) * pl->capacity); pl->points = realloc(pl->points, sizeof(V2) * pl->capacity);
@@ -16,22 +16,22 @@ void add_point_polyline(Polyline *pl, V2 point) {
pl->points[pl->count++] = point; pl->points[pl->count++] = point;
} }
void free_polyline(Polyline *pl) { void vektor_polyline_free(VektorPolyline *pl) {
if (!pl) if (!pl)
return; return;
free(pl->points); free(pl->points);
free(pl); free(pl);
} }
Polygon *mk_polygon(void) { VektorPolygon *vektor_polygon_new(void) {
Polygon *pg = malloc(sizeof(Polygon)); VektorPolygon *pg = malloc(sizeof(VektorPolygon));
pg->count = 0; pg->count = 0;
pg->capacity = 4; pg->capacity = 4;
pg->points = malloc(sizeof(V2) * pg->capacity); pg->points = malloc(sizeof(V2) * pg->capacity);
return pg; return pg;
} }
void add_point_polygon(Polygon *pg, V2 point) { void vektor_polygon_add_point(VektorPolygon *pg, V2 point) {
if (pg->count >= pg->capacity) { if (pg->count >= pg->capacity) {
pg->capacity *= 2; pg->capacity *= 2;
pg->points = realloc(pg->points, sizeof(V2) * pg->capacity); pg->points = realloc(pg->points, sizeof(V2) * pg->capacity);
@@ -39,7 +39,7 @@ void add_point_polygon(Polygon *pg, V2 point) {
pg->points[pg->count++] = point; pg->points[pg->count++] = point;
} }
void free_polygon(Polygon *pg) { void vektor_polygon_free(VektorPolygon *pg) {
if (!pg) if (!pg)
return; return;
free(pg->points); free(pg->points);

View File

@@ -8,43 +8,43 @@
typedef struct { typedef struct {
V2 p1; V2 p1;
V2 p2; V2 p2;
} Line; } VektorLine;
typedef struct { typedef struct {
V2 *points; V2 *points;
size_t count; size_t count;
size_t capacity; size_t capacity;
} Polyline; } VektorPolyline;
typedef struct { typedef struct {
V2 *points; V2 *points;
size_t count; size_t count;
size_t capacity; size_t capacity;
} Polygon; } VektorPolygon;
typedef struct { typedef struct {
V2 center; V2 center;
double radius; double radius;
} Circle; } VektorCircle;
typedef enum { LINE, POLYLINE, POLYGON, CIRCLE } PrimitiveKind; typedef enum { LINE, POLYLINE, POLYGON, CIRCLE } VektorPrimitiveKind;
typedef struct { typedef struct {
PrimitiveKind kind; VektorPrimitiveKind kind;
union { union {
Line line; VektorLine line;
Polyline *polyline; VektorPolyline *polyline;
Polygon *polygon; VektorPolygon *polygon;
Circle circle; VektorCircle circle;
}; };
} Primitive; } VektorPrimitive;
Polyline *mk_polyline(void); VektorPolyline* vektor_polyline_new(void);
void add_point_polyline(Polyline *pl, V2 point); void vektor_polyline_add_point(VektorPolyline *pl, V2 point);
void free_polyline(Polyline *pl); void vektor_polyline_free(VektorPolyline *pl);
Polygon *mk_polygon(void); VektorPolygon *vektor_polygon_new(void);
void add_point_polygon(Polygon *pl, V2 point); void vektor_polygon_add_point(VektorPolygon *pl, V2 point);
void free_polygon(Polygon *pl); void vektor_polygon_free(VektorPolygon *pl);
#endif // PRIMITIVES_H_ #endif // PRIMITIVES_H_

View File

@@ -2,7 +2,7 @@
#include "primitives.h" #include "primitives.h"
#include "stddef.h" #include "stddef.h"
void add_edge(EdgeBuffer *buffer, Edge edge) { void vektor_edgebuffer_add_edge(EdgeBuffer *buffer, Edge edge) {
if (buffer->count >= buffer->capacity) { if (buffer->count >= buffer->capacity) {
buffer->capacity = buffer->capacity ? buffer->capacity * 2 : 4; buffer->capacity = buffer->capacity ? buffer->capacity * 2 : 4;
buffer->edges = realloc(buffer->edges, sizeof(Edge) * buffer->capacity); buffer->edges = realloc(buffer->edges, sizeof(Edge) * buffer->capacity);
@@ -10,17 +10,17 @@ void add_edge(EdgeBuffer *buffer, Edge edge) {
buffer->edges[buffer->count++] = edge; buffer->edges[buffer->count++] = edge;
} }
void flatten_line(EdgeBuffer *buffer, Line line) { void vektor_edgebuffer_flatten_line(EdgeBuffer *buffer, VektorLine line) {
add_edge(buffer, (Edge){line.p1, line.p2, 0}); vektor_edgebuffer_add_edge(buffer, (Edge){line.p1, line.p2, 0});
} }
void flatten_polyline(EdgeBuffer *buffer, Polyline *line) { void vektor_edgebuffer_flatten_polyline(EdgeBuffer *buffer, VektorPolyline *line) {
for (size_t i = 0; i + 1 < line->count; i++) { for (size_t i = 0; i + 1 < line->count; i++) {
add_edge(buffer, (Edge){line->points[i], line->points[i + 1], 0}); vektor_edgebuffer_add_edge(buffer, (Edge){line->points[i], line->points[i + 1], 0});
} }
} }
void flatten_polygon(EdgeBuffer *buffer, Polygon *pg) { void vektor_edgebuffer_flatten_polygon(EdgeBuffer *buffer, VektorPolygon *pg) {
size_t n = pg->count; size_t n = pg->count;
if (n < 3) if (n < 3)
return; return;
@@ -29,16 +29,16 @@ void flatten_polygon(EdgeBuffer *buffer, Polygon *pg) {
V2 p1 = pg->points[i]; V2 p1 = pg->points[i];
V2 p2 = pg->points[(i + 1) % n]; V2 p2 = pg->points[(i + 1) % n];
int winding = (p1.y < p2.y) ? +1 : -1; int winding = (p1.y < p2.y) ? +1 : -1;
add_edge(buffer, (Edge){p1, p2, winding}); vektor_edgebuffer_add_edge(buffer, (Edge){p1, p2, winding});
} }
} }
inline Framebuffer mk_framebuffer(unsigned int W, unsigned int H) { inline VektorFramebuffer vektor_framebuffer_new(unsigned int W, unsigned int H) {
Framebuffer fb = {.width = W, .height = H, .pixels = calloc(W * H * 3, 1)}; VektorFramebuffer fb = {.width = W, .height = H, .pixels = calloc(W * H * 3, 1)};
return fb; return fb;
} }
inline void put_pixel(Framebuffer *fb, int x, int y, unsigned char r, inline void vektor_framebuffer_putpixel(VektorFramebuffer *fb, int x, int y, unsigned char r,
unsigned char g, unsigned char b) { unsigned char g, unsigned char b) {
if ((unsigned)x >= fb->width || (unsigned)y >= fb->height) if ((unsigned)x >= fb->width || (unsigned)y >= fb->height)
return; return;
@@ -49,7 +49,7 @@ inline void put_pixel(Framebuffer *fb, int x, int y, unsigned char r,
fb->pixels[i + 2] = b; fb->pixels[i + 2] = b;
} }
void draw_line(Framebuffer *fb, V2 a, V2 b, unsigned char r, unsigned char g, void vektor_framebuffer_drawline(VektorFramebuffer *fb, V2 a, V2 b, unsigned char r, unsigned char g,
unsigned char bl) { unsigned char bl) {
int x0 = (int)a.x; int x0 = (int)a.x;
int y0 = (int)a.y; int y0 = (int)a.y;
@@ -63,7 +63,7 @@ void draw_line(Framebuffer *fb, V2 a, V2 b, unsigned char r, unsigned char g,
int err = dx + dy; int err = dx + dy;
for (;;) { for (;;) {
put_pixel(fb, x0, y0, r, g, bl); vektor_framebuffer_putpixel(fb, x0, y0, r, g, bl);
if (x0 == x1 && y0 == y1) if (x0 == x1 && y0 == y1)
break; break;

View File

@@ -17,24 +17,24 @@ typedef struct {
size_t capacity; size_t capacity;
} EdgeBuffer; } EdgeBuffer;
void add_edge(EdgeBuffer *edges, Edge edge); void vektor_edgebuffer_add_edge(EdgeBuffer *edges, Edge edge);
void flatten_line(EdgeBuffer *edges, Line line); void vektor_edgebuffer_flatten_line(EdgeBuffer *edges, VektorLine line);
void flatten_polyline(EdgeBuffer *edges, Polyline *line); void vektor_edgebuffer_flatten_polyline(EdgeBuffer *edges, VektorPolyline *line);
void flatten_polygon(EdgeBuffer *buffer, Polygon *line); void vektor_edgebuffer_flatten_polygon(EdgeBuffer *buffer, VektorPolygon *line);
typedef struct { typedef struct {
unsigned int width; unsigned int width;
unsigned int height; unsigned int height;
unsigned char *pixels; // Flat RGB8 array unsigned char *pixels; // Flat RGB8 array
} Framebuffer; } VektorFramebuffer;
Framebuffer mk_framebuffer(unsigned int width, unsigned int height); VektorFramebuffer vektor_framebuffer_new(unsigned int width, unsigned int height);
void put_pixel(Framebuffer *fb, int x, int y, unsigned char r, unsigned char g, void vektor_framebuffer_putpixel(VektorFramebuffer *fb, int x, int y, unsigned char r, unsigned char g,
unsigned char b); unsigned char b);
void draw_line(Framebuffer *fb, V2 a, V2 b, unsigned char r, unsigned char g, void vektor_framebuffer_drawline(VektorFramebuffer *fb, V2 a, V2 b, unsigned char r, unsigned char g,
unsigned char bl); unsigned char bl);
#endif // RASTER_H_ #endif // RASTER_H_

View File

@@ -28,7 +28,7 @@ static void activate(GtkApplication *app, gpointer user_data) {
gtk_window_present(widget_state->window); gtk_window_present(widget_state->window);
} }
void write_ppm(const char *path, const Framebuffer *fb) { void write_ppm(const char *path, const VektorFramebuffer *fb) {
FILE *f = fopen(path, "wb"); FILE *f = fopen(path, "wb");
if (!f) if (!f)
abort(); abort();
@@ -39,18 +39,18 @@ void write_ppm(const char *path, const Framebuffer *fb) {
} }
int main(int argc, char **argv) { int main(int argc, char **argv) {
Framebuffer fb = mk_framebuffer(256, 256); VektorFramebuffer fb = vektor_framebuffer_new(256, 256);
EdgeBuffer edges = {0}; EdgeBuffer edges = {0};
Polygon pg = *mk_polygon(); VektorPolygon pg = *vektor_polygon_new();
add_point_polygon(&pg, (V2){50, 50}); vektor_polygon_add_point(&pg, (V2){50, 50});
add_point_polygon(&pg, (V2){200, 80}); vektor_polygon_add_point(&pg, (V2){200, 80});
add_point_polygon(&pg, (V2){120, 200}); vektor_polygon_add_point(&pg, (V2){120, 200});
flatten_polygon(&edges, &pg); vektor_edgebuffer_flatten_polygon(&edges, &pg);
for (size_t i = 0; i < edges.count; i++) { for (size_t i = 0; i < edges.count; i++) {
draw_line(&fb, edges.edges[i].p1, edges.edges[i].p2, 255, 255, 255); vektor_framebuffer_drawline(&fb, edges.edges[i].p1, edges.edges[i].p2, 255, 255, 255);
} }
write_ppm("out.ppm", &fb); write_ppm("out.ppm", &fb);