Update noodle drawing code

This commit is contained in:
Abdelrahman Said 2024-01-24 22:44:36 +00:00
parent a58a315d48
commit 8275fce9c6

View File

@ -3,10 +3,11 @@
#include "aliases/aliases.h" #include "aliases/aliases.h"
#include "math_utils.h" #include "math_utils.h"
#include "window.h" #include "window.h"
#include <math.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdlib.h> #include <stdlib.h>
#define NOODLE_WIDTH 2 #define NOODLE_HALF_WIDTH 2
INTERNAL bool aabb(rect rec, i32 x, i32 y); INTERNAL bool aabb(rect rec, i32 x, i32 y);
@ -146,18 +147,37 @@ line ui_noodle(const window *wnd, ui_ctx *ctx, line ln,
if (horizontal) { if (horizontal) {
i32 x = min(ln.p0.x, ln.p1.x); i32 x = min(ln.p0.x, ln.p1.x);
bounding_box.topleft = (point){x, ln.p0.y}; bounding_box.topleft = (point){x, ln.p0.y - NOODLE_HALF_WIDTH};
bounding_box.w = abs(ln.p1.x - ln.p0.x); bounding_box.w = abs(ln.p1.x - ln.p0.x);
bounding_box.h = NOODLE_WIDTH; bounding_box.h = NOODLE_HALF_WIDTH * 2;
fill_rect(wnd, bounding_box, colours.fill); fill_rect(wnd, bounding_box, colours.fill);
} else { } else {
quad qd = (quad){ vec2 direction = line_direction(&ln);
.p0 = ln.p0,
.p1 = (point){ln.p0.x + NOODLE_WIDTH, ln.p0.y}, quad qd = (quad){0};
.p2 = ln.p1,
.p3 = (point){ln.p1.x + NOODLE_WIDTH, ln.p1.y}, if (direction.x == 0) {
}; qd = (quad){
.p0 = (point){ln.p0.x - NOODLE_HALF_WIDTH, ln.p0.y},
.p1 = (point){ln.p0.x + NOODLE_HALF_WIDTH, ln.p0.y},
.p2 = (point){ln.p1.x - NOODLE_HALF_WIDTH, ln.p1.y},
.p3 = (point){ln.p1.x + NOODLE_HALF_WIDTH, ln.p1.y},
};
} else {
f32 slope = (f32)(direction.y) / direction.x;
f32 perpendicular_dy =
sqrtf(square(NOODLE_HALF_WIDTH) / (square((f32)slope) + 1.0f));
f32 perpendicular_dx = -1.0f * slope * perpendicular_dy;
qd = (quad){
.p0 = (point){ln.p0.x - perpendicular_dx, ln.p0.y - perpendicular_dy},
.p1 = (point){ln.p0.x + perpendicular_dx, ln.p0.y + perpendicular_dy},
.p2 = (point){ln.p1.x - perpendicular_dx, ln.p1.y - perpendicular_dy},
.p3 = (point){ln.p1.x + perpendicular_dx, ln.p1.y + perpendicular_dy},
};
}
fill_quad(wnd, qd, colours.fill); fill_quad(wnd, qd, colours.fill);