Fix dot-bend collision detection

Fixes bend AABB calculation and the actual circle-arc intersection
algorithm.
This commit is contained in:
Mikolaj Wielgus 2023-08-16 21:39:24 +02:00
parent 4f473bacec
commit 6e87f9c124
2 changed files with 16 additions and 13 deletions

View File

@ -191,7 +191,7 @@ fn main() {
.add_dot(DotWeight {
net: 0,
circle: Circle {
pos: (190.5, 100.5).into(),
pos: (190.5, 200.5).into(),
r: 8.0,
},
})
@ -248,7 +248,7 @@ fn main() {
let _ = layout.add_seg(barrier2_dot1, barrier2_dot2, 16.0);
let head = layout.route_start(dot5);
let head = layout.route_around_dot(head, dot6, true, 5.0).unwrap();
let head = layout.route_around_dot(head, dot6, false, 5.0).unwrap();
let _ = layout.route_finish(head, dot7, 5.0);
/*render_times(&mut event_pump, &mut canvas, &mut layout, None, -1);

View File

@ -47,14 +47,14 @@ impl BendShape {
fn inner_circle(&self) -> Circle {
Circle {
pos: self.center,
r: self.center.euclidean_distance(&self.from) - self.width / 2.,
r: self.center.euclidean_distance(&self.from) - self.width / 2.0,
}
}
fn outer_circle(&self) -> Circle {
Circle {
pos: self.center,
r: self.center.euclidean_distance(&self.from) + self.width / 2.,
r: self.center.euclidean_distance(&self.from) + self.width / 2.0,
}
}
}
@ -97,26 +97,26 @@ impl Shape {
Shape::Seg(other) => dot.c.pos.euclidean_distance(&other.polygon()) < dot.c.r,
Shape::Bend(other) => {
for point in math::circles_intersection(&dot.c, &other.inner_circle()) {
if !math::between_vectors(
if math::between_vectors(
point - other.center,
other.from - other.center,
other.to - other.center,
) {
return false;
return true;
}
}
for point in math::circles_intersection(&dot.c, &other.outer_circle()) {
if !math::between_vectors(
if math::between_vectors(
point - other.center,
other.from - other.center,
other.to - other.center,
) {
return false;
return true;
}
}
true
false
}
},
Shape::Seg(seg) => {
@ -154,10 +154,13 @@ impl Shape {
.collect();
AABB::<[f64; 2]>::from_points(points.iter())
}
Shape::Bend(bend) => AABB::<[f64; 2]>::from_points(&[
[bend.from.x() - bend.width, bend.from.y() - bend.width],
[bend.to.x() + bend.width, bend.to.y() + bend.width],
]),
Shape::Bend(bend) => {
let halfwidth = bend.center.euclidean_distance(&bend.from).ceil() + bend.width;
AABB::from_corners(
[bend.center.x() - halfwidth, bend.center.y() - halfwidth],
[bend.center.x() + halfwidth, bend.center.y() + halfwidth],
)
}
}
}