diff --git a/src/main.rs b/src/main.rs index 492c69b..48885b5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -87,12 +87,11 @@ async fn main() { ); for (w1, w2) in dd.get_walls() { - shadows::draw_wall( - w1.as_f32(), - w2.as_f32(), - mouse_position, - LIGHT_RADIUS, - ); + let w1 = w1.as_f32(); + let w2 = w2.as_f32(); + if shadows::wall_needs_to_be_drawn(w1, w2, mouse_position, LIGHT_RADIUS) { + shadows::draw_wall(w1, w2, mouse_position, LIGHT_RADIUS); + } } dd.update(); diff --git a/src/shadows.rs b/src/shadows.rs index 0ff009a..c53a98f 100644 --- a/src/shadows.rs +++ b/src/shadows.rs @@ -1,3 +1,4 @@ +use macroquad::prelude::Mat2; use macroquad::prelude::draw_triangle; use macroquad::prelude::DVec2; use macroquad::prelude::draw_mesh; @@ -52,7 +53,22 @@ pub fn draw_wall(a: Vec2, b: Vec2, light_source: Vec2, radius: f32) { draw_triangle(b_prime, a_prime, opposite_src, BLACK); } -pub fn draw_polygon(points: &[Vec2], color: Color) { +pub fn wall_needs_to_be_drawn(a: Vec2, b: Vec2, light_source: Vec2, radius: f32) -> bool { + let a = a - light_source; + let b = b - light_source; + let delta = a - b; + let unit = delta / delta.length(); + + let a_dot = unit.dot(a); + let b_dot = unit.dot(b); + + let d = Mat2::from_cols(unit, a).determinant(); + + (d < radius && d > -radius) && ( + a.length() < radius || + b.length() < radius || + (a_dot < 0. && b_dot > 0.) || (a_dot > 0. && b_dot < 0.) + ) } /*/// Code adapted from https://github.com/not-fl3/macroquad/issues/174