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"
Polyline *mk_polyline(void) {
Polyline *pl = malloc(sizeof(Polyline));
VektorPolyline *vektor_polyline_new(void) {
VektorPolyline *pl = malloc(sizeof(VektorPolyline));
pl->count = 0;
pl->capacity = 4;
pl->points = malloc(sizeof(V2) * pl->capacity);
return pl;
}
void add_point_polyline(Polyline *pl, V2 point) {
void vektor_polyline_add_point(VektorPolyline *pl, V2 point) {
if (pl->count >= pl->capacity) {
pl->capacity *= 2;
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;
}
void free_polyline(Polyline *pl) {
void vektor_polyline_free(VektorPolyline *pl) {
if (!pl)
return;
free(pl->points);
free(pl);
}
Polygon *mk_polygon(void) {
Polygon *pg = malloc(sizeof(Polygon));
VektorPolygon *vektor_polygon_new(void) {
VektorPolygon *pg = malloc(sizeof(VektorPolygon));
pg->count = 0;
pg->capacity = 4;
pg->points = malloc(sizeof(V2) * pg->capacity);
return pg;
}
void add_point_polygon(Polygon *pg, V2 point) {
void vektor_polygon_add_point(VektorPolygon *pg, V2 point) {
if (pg->count >= pg->capacity) {
pg->capacity *= 2;
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;
}
void free_polygon(Polygon *pg) {
void vektor_polygon_free(VektorPolygon *pg) {
if (!pg)
return;
free(pg->points);

View File

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

View File

@@ -2,7 +2,7 @@
#include "primitives.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) {
buffer->capacity = buffer->capacity ? buffer->capacity * 2 : 4;
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;
}
void flatten_line(EdgeBuffer *buffer, Line line) {
add_edge(buffer, (Edge){line.p1, line.p2, 0});
void vektor_edgebuffer_flatten_line(EdgeBuffer *buffer, VektorLine line) {
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++) {
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;
if (n < 3)
return;
@@ -29,16 +29,16 @@ void flatten_polygon(EdgeBuffer *buffer, Polygon *pg) {
V2 p1 = pg->points[i];
V2 p2 = pg->points[(i + 1) % n];
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) {
Framebuffer fb = {.width = W, .height = H, .pixels = calloc(W * H * 3, 1)};
inline VektorFramebuffer vektor_framebuffer_new(unsigned int W, unsigned int H) {
VektorFramebuffer fb = {.width = W, .height = H, .pixels = calloc(W * H * 3, 1)};
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) {
if ((unsigned)x >= fb->width || (unsigned)y >= fb->height)
return;
@@ -49,7 +49,7 @@ inline void put_pixel(Framebuffer *fb, int x, int y, unsigned char r,
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) {
int x0 = (int)a.x;
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;
for (;;) {
put_pixel(fb, x0, y0, r, g, bl);
vektor_framebuffer_putpixel(fb, x0, y0, r, g, bl);
if (x0 == x1 && y0 == y1)
break;

View File

@@ -17,24 +17,24 @@ typedef struct {
size_t capacity;
} 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 flatten_polyline(EdgeBuffer *edges, Polyline *line);
void flatten_polygon(EdgeBuffer *buffer, Polygon *line);
void vektor_edgebuffer_flatten_line(EdgeBuffer *edges, VektorLine line);
void vektor_edgebuffer_flatten_polyline(EdgeBuffer *edges, VektorPolyline *line);
void vektor_edgebuffer_flatten_polygon(EdgeBuffer *buffer, VektorPolygon *line);
typedef struct {
unsigned int width;
unsigned int height;
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);
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);
#endif // RASTER_H_

View File

@@ -28,7 +28,7 @@ static void activate(GtkApplication *app, gpointer user_data) {
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");
if (!f)
abort();
@@ -39,18 +39,18 @@ void write_ppm(const char *path, const Framebuffer *fb) {
}
int main(int argc, char **argv) {
Framebuffer fb = mk_framebuffer(256, 256);
VektorFramebuffer fb = vektor_framebuffer_new(256, 256);
EdgeBuffer edges = {0};
Polygon pg = *mk_polygon();
add_point_polygon(&pg, (V2){50, 50});
add_point_polygon(&pg, (V2){200, 80});
add_point_polygon(&pg, (V2){120, 200});
VektorPolygon pg = *vektor_polygon_new();
vektor_polygon_add_point(&pg, (V2){50, 50});
vektor_polygon_add_point(&pg, (V2){200, 80});
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++) {
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);