feat: add bounding box calculation
This commit is contained in:
@@ -81,7 +81,8 @@ begin_click_dispatch:
|
||||
.stroke_width = 0.01};
|
||||
VektorShape shape = (VektorShape){
|
||||
.primitive = linePrimitive, .z_index = 0, .style = style};
|
||||
vektor_shapebuffer_add_shape(state->shapeBuffer, shape);
|
||||
vektor_shapebuffer_add_shape(state->shapeBuffer, vektor_shape_new(linePrimitive, style, 0));
|
||||
|
||||
|
||||
state->selectedShape =
|
||||
&(state->shapeBuffer->shapes[state->shapeBuffer->count - 1]);
|
||||
@@ -96,6 +97,13 @@ begin_click_dispatch:
|
||||
|
||||
vektor_polyline_add_point(state->selectedShape->primitive.polyline,
|
||||
pos);
|
||||
|
||||
vektor_shapes_update_bbox(state->shapeBuffer);
|
||||
|
||||
|
||||
for (size_t i = 0; i < state->shapeBuffer->count; i++) {
|
||||
g_print("<%f,%f>-<%f,%f>\n", state->shapeBuffer->shapes[i].bbox.min.x, state->shapeBuffer->shapes[i].bbox.min.y, state->shapeBuffer->shapes[i].bbox.max.x, state->shapeBuffer->shapes[i].bbox.max.y);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -111,6 +119,7 @@ void vektor_appstate_new(VektorWidgetState* wstate, VektorAppState* stateOut) {
|
||||
stateOut->canvas = malloc(sizeof(VektorCanvas));
|
||||
stateOut->widgetState = wstate;
|
||||
stateOut->currentColor = vektor_color_blank;
|
||||
stateOut->selectedShape = NULL;
|
||||
vektor_canvas_init(wstate, stateOut->canvas, stateOut->shapeBuffer);
|
||||
|
||||
// link all the buttons
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
#include "primitives.h"
|
||||
#include <assert.h>
|
||||
#include <math.h>
|
||||
#include <stddef.h>
|
||||
|
||||
VektorPolyline* vektor_polyline_new(void) {
|
||||
VektorPolyline* pl = malloc(sizeof(VektorPolyline));
|
||||
@@ -55,3 +58,56 @@ void vektor_shapebuffer_add_shape(VektorShapeBuffer* buffer,
|
||||
}
|
||||
buffer->shapes[buffer->count++] = shape;
|
||||
}
|
||||
|
||||
VektorBBox polyline_mk_bbox(VektorPrimitive prim) {
|
||||
float min_x, max_x, min_y, max_y;
|
||||
for (size_t i = 0; i < prim.polyline->count; i++) {
|
||||
V2 p = prim.polyline->points[i];
|
||||
min_x = fminf(min_x, p.x);
|
||||
min_y = fminf(min_y, p.y);
|
||||
|
||||
max_x = fminf(max_x, p.x);
|
||||
max_y = fminf(max_y, p.y);
|
||||
}
|
||||
return (VektorBBox){(V2){min_x, min_y}, (V2){max_x, max_y}};
|
||||
}
|
||||
|
||||
VektorBBox polygon_mk_bbox(VektorPrimitive prim) {
|
||||
float min_x, max_x, min_y, max_y;
|
||||
for (size_t i = 0; i < prim.polygon->count; i++) {
|
||||
V2 p = prim.polygon->points[i];
|
||||
min_x = fminf(min_x, p.x);
|
||||
min_y = fminf(min_y, p.y);
|
||||
|
||||
max_x = fminf(max_x, p.x);
|
||||
max_y = fminf(max_y, p.y);
|
||||
}
|
||||
return (VektorBBox){(V2){min_x, min_y}, (V2){max_x, max_y}};
|
||||
}
|
||||
|
||||
VektorBBox vektor_mk_bbox(VektorPrimitive prim) {
|
||||
switch (prim.kind) {
|
||||
case VEKTOR_POLYLINE:
|
||||
return polyline_mk_bbox(prim);
|
||||
break;
|
||||
|
||||
case VEKTOR_POLYGON:
|
||||
return polygon_mk_bbox(prim);
|
||||
break;
|
||||
|
||||
default:
|
||||
// TODO: fill in all primitives
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
VektorShape vektor_shape_new(VektorPrimitive prim, VektorStyle style,
|
||||
int z_index) {
|
||||
return (VektorShape){.primitive = prim, .style = style, .z_index = z_index, .bbox=vektor_mk_bbox(prim)};
|
||||
}
|
||||
|
||||
void vektor_shapes_update_bbox(VektorShapeBuffer* buffer) {
|
||||
for (size_t i = 0; i < buffer->count; i++) {
|
||||
buffer->shapes[i].bbox = vektor_mk_bbox(buffer->shapes[i].primitive);
|
||||
}
|
||||
}
|
||||
@@ -51,12 +51,26 @@ typedef struct {
|
||||
float stroke_width;
|
||||
} VektorStyle;
|
||||
|
||||
typedef struct {
|
||||
V2 min;
|
||||
V2 max;
|
||||
} VektorBBox;
|
||||
|
||||
typedef struct {
|
||||
VektorStyle style;
|
||||
int z_index;
|
||||
VektorBBox bbox;
|
||||
VektorPrimitive primitive;
|
||||
} VektorShape;
|
||||
|
||||
VektorBBox polyline_mk_bbox(VektorPrimitive prim);
|
||||
VektorBBox polygon_mk_bbox(VektorPrimitive prim);
|
||||
|
||||
VektorBBox vektor_mk_bbox(VektorPrimitive prim);
|
||||
|
||||
VektorShape vektor_shape_new(VektorPrimitive prim, VektorStyle style,
|
||||
int z_index);
|
||||
|
||||
typedef struct {
|
||||
VektorShape* shapes;
|
||||
size_t count;
|
||||
@@ -64,5 +78,5 @@ typedef struct {
|
||||
} VektorShapeBuffer;
|
||||
|
||||
void vektor_shapebuffer_add_shape(VektorShapeBuffer* buffer, VektorShape shape);
|
||||
|
||||
void vektor_shapes_update_bbox(VektorShapeBuffer* buffer);
|
||||
#endif // PRIMITIVES_H_
|
||||
|
||||
Reference in New Issue
Block a user