Add utility functions and fix bug when reallocating lines array
This commit is contained in:
parent
e4d3024364
commit
13aadb4281
@ -8,6 +8,22 @@
|
|||||||
|
|
||||||
#define square(x) (x * x)
|
#define square(x) (x * x)
|
||||||
|
|
||||||
|
typedef struct line line_t;
|
||||||
|
struct line {
|
||||||
|
point_t p0;
|
||||||
|
point_t p1;
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef struct triangle triangle_t;
|
||||||
|
struct triangle {
|
||||||
|
point_t p0;
|
||||||
|
point_t p1;
|
||||||
|
point_t p2;
|
||||||
|
};
|
||||||
|
|
||||||
|
INTERNAL void draw_line(SDL_Renderer *renderer, line_t ln);
|
||||||
|
INTERNAL void draw_triangle(SDL_Renderer *renderer, triangle_t triangle);
|
||||||
|
|
||||||
bool init_arrows_arr(arrows_t *lines, u64 capacity) {
|
bool init_arrows_arr(arrows_t *lines, u64 capacity) {
|
||||||
u64 size = capacity * sizeof(arrow_t);
|
u64 size = capacity * sizeof(arrow_t);
|
||||||
|
|
||||||
@ -36,6 +52,8 @@ void add_arrow(arrows_t *lines, arrow_t ln) {
|
|||||||
lines->lines = ptr;
|
lines->lines = ptr;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
lines->capacity = new_capacity;
|
||||||
}
|
}
|
||||||
|
|
||||||
lines->lines[(lines->count)++] = (arrow_t){
|
lines->lines[(lines->count)++] = (arrow_t){
|
||||||
@ -57,8 +75,9 @@ INTERNAL f32 vec2_magnitude(const vec2_t *vec) {
|
|||||||
return sqrtf((f32)vec->x * vec->x + (f32)vec->y * vec->y);
|
return sqrtf((f32)vec->x * vec->x + (f32)vec->y * vec->y);
|
||||||
}
|
}
|
||||||
|
|
||||||
INTERNAL void draw_arrow_head(SDL_Renderer *renderer, const arrow_t *arrow,
|
INTERNAL triangle_t calculate_arrow_head(SDL_Renderer *renderer,
|
||||||
const point_t *arrow_end) {
|
const arrow_t *arrow,
|
||||||
|
const point_t *arrow_end) {
|
||||||
// m = line_slope
|
// m = line_slope
|
||||||
// dx = change_in_x
|
// dx = change_in_x
|
||||||
// dy = change_in_y
|
// dy = change_in_y
|
||||||
@ -129,14 +148,7 @@ INTERNAL void draw_arrow_head(SDL_Renderer *renderer, const arrow_t *arrow,
|
|||||||
arrow_base_p1.y = arrow_end->y + perpendicular_dy;
|
arrow_base_p1.y = arrow_end->y + perpendicular_dy;
|
||||||
}
|
}
|
||||||
|
|
||||||
SDL_RenderDrawLine(renderer, arrow_end->x, arrow_end->y, arrow_base_p0.x,
|
return (triangle_t){arrow_tip, arrow_base_p0, arrow_base_p1};
|
||||||
arrow_base_p0.y);
|
|
||||||
SDL_RenderDrawLine(renderer, arrow_end->x, arrow_end->y, arrow_base_p1.x,
|
|
||||||
arrow_base_p1.y);
|
|
||||||
SDL_RenderDrawLine(renderer, arrow_tip.x, arrow_tip.y, arrow_base_p0.x,
|
|
||||||
arrow_base_p0.y);
|
|
||||||
SDL_RenderDrawLine(renderer, arrow_tip.x, arrow_tip.y, arrow_base_p1.x,
|
|
||||||
arrow_base_p1.y);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void draw_arrow(SDL_Renderer *renderer, const arrow_t *arrow, colour_t colour) {
|
void draw_arrow(SDL_Renderer *renderer, const arrow_t *arrow, colour_t colour) {
|
||||||
@ -147,10 +159,26 @@ void draw_arrow(SDL_Renderer *renderer, const arrow_t *arrow, colour_t colour) {
|
|||||||
arrow->origin.x + arrow->direction.x,
|
arrow->origin.x + arrow->direction.x,
|
||||||
arrow->origin.y + arrow->direction.y,
|
arrow->origin.y + arrow->direction.y,
|
||||||
};
|
};
|
||||||
|
line_t line = (line_t){arrow->origin, end};
|
||||||
|
|
||||||
if (vec2_magnitude(&(arrow->direction)) != 0) {
|
if (vec2_magnitude(&(arrow->direction)) != 0) {
|
||||||
draw_arrow_head(renderer, arrow, &end);
|
triangle_t triangle = calculate_arrow_head(renderer, arrow, &end);
|
||||||
|
draw_triangle(renderer, triangle);
|
||||||
}
|
}
|
||||||
|
|
||||||
SDL_RenderDrawLine(renderer, arrow->origin.x, arrow->origin.y, end.x, end.y);
|
draw_line(renderer, line);
|
||||||
|
}
|
||||||
|
|
||||||
|
INTERNAL void draw_line(SDL_Renderer *renderer, line_t ln) {
|
||||||
|
SDL_RenderDrawLine(renderer, ln.p0.x, ln.p0.y, ln.p1.x, ln.p1.y);
|
||||||
|
}
|
||||||
|
|
||||||
|
INTERNAL void draw_triangle(SDL_Renderer *renderer, triangle_t triangle) {
|
||||||
|
line_t ln0 = (line_t){triangle.p0, triangle.p1};
|
||||||
|
line_t ln1 = (line_t){triangle.p0, triangle.p2};
|
||||||
|
line_t ln2 = (line_t){triangle.p1, triangle.p2};
|
||||||
|
|
||||||
|
draw_line(renderer, ln0);
|
||||||
|
draw_line(renderer, ln1);
|
||||||
|
draw_line(renderer, ln2);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user