diff --git a/src/ui.c b/src/ui.c index 8e43b7d..91d18c8 100644 --- a/src/ui.c +++ b/src/ui.c @@ -3,10 +3,11 @@ #include "aliases/aliases.h" #include "math_utils.h" #include "window.h" +#include #include #include -#define NOODLE_WIDTH 2 +#define NOODLE_HALF_WIDTH 2 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) { 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.h = NOODLE_WIDTH; + bounding_box.h = NOODLE_HALF_WIDTH * 2; fill_rect(wnd, bounding_box, colours.fill); } else { - quad qd = (quad){ - .p0 = ln.p0, - .p1 = (point){ln.p0.x + NOODLE_WIDTH, ln.p0.y}, - .p2 = ln.p1, - .p3 = (point){ln.p1.x + NOODLE_WIDTH, ln.p1.y}, - }; + vec2 direction = line_direction(&ln); + + quad qd = (quad){0}; + + 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);