Draw smoother lines in the inner region

This commit is contained in:
numzero 2024-06-10 20:16:17 +03:00
parent 4b511af742
commit 5947a6e324

View File

@ -275,6 +275,20 @@ impl Space {
None None
}).collect() }).collect()
} }
fn line(&self, a: Vec2, b: Vec2, step: f32) -> Vec<Vec2> {
match self.which_subspace(a) {
Outer => vec![b],
Inner => {
let cell = RectInside { rect: self.rect };
let n = ((b - a).length() / step) as usize + 1;
let a = cell.pos_to_local(a);
let b = cell.pos_to_local(b);
(1..=n).map(|k| cell.pos_to_global(a.lerp(b, k as f32 / n as f32))).collect()
}
Boundary => panic!("Can't draw a line here!"),
}
}
} }
fn draw_ray_2(gc: &mut Vec<Draw>, space: &Space, base: Vec2, dir: Vec2) { fn draw_ray_2(gc: &mut Vec<Draw>, space: &Space, base: Vec2, dir: Vec2) {
@ -306,6 +320,7 @@ fn draw_ray_2(gc: &mut Vec<Draw>, space: &Space, base: Vec2, dir: Vec2) {
for hit in ret.objects { for hit in ret.objects {
hits.circle(hit.pos.x, hit.pos.y, 1.5); hits.circle(hit.pos.x, hit.pos.y, 1.5);
} }
let a = ray.pos;
ray = match ret.end { ray = match ret.end {
Some(r) => r, Some(r) => r,
None => { None => {
@ -314,7 +329,9 @@ fn draw_ray_2(gc: &mut Vec<Draw>, space: &Space, base: Vec2, dir: Vec2) {
break; break;
} }
}; };
gc.line_to(ray.pos.x, ray.pos.y); for p in space.line(a, ray.pos, 10.0) {
gc.line_to(p.x, p.y);
}
gc.stroke(); gc.stroke();
gc.new_dash_pattern(); gc.new_dash_pattern();
gc.new_path(); gc.new_path();