Skip to content

Commit c874dfd

Browse files
committed
Revert "Remove floating point operations for is_inside_primitive."
This reverts commit 86c07b2.
1 parent 86c07b2 commit c874dfd

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

src/geometrify.rs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,24 @@ impl Triangle {
6262

6363
impl Primitive for Triangle {
6464
fn is_inside_primitive(&self, p: Point) -> bool {
65-
return (self.a.x - self.b.x) * (p.y - self.a.y) - (self.a.y - self.b.y) * (p.x - self.a.x) > 0 &&
66-
(self.b.x - self.c.x) * (p.y - self.b.y) - (self.b.y - self.c.y) * (p.x - self.b.x) > 0 &&
67-
(self.c.x - self.a.x) * (p.y - self.c.y) - (self.c.y - self.a.y) * (p.x - self.c.x) > 0;
65+
let span_a = Point {
66+
x: self.b.x - self.a.x,
67+
y: self.b.y - self.a.y,
68+
};
69+
let span_b = Point {
70+
x: self.c.x - self.a.x,
71+
y: self.c.y - self.a.y,
72+
};
73+
74+
let q = Point {
75+
x: p.x - self.a.x,
76+
y: p.y - self.a.y,
77+
};
78+
79+
let s = q.cross_product(span_b) as f32 * self.span_div();
80+
let t = span_a.cross_product(q) as f32 * self.span_div();
81+
82+
(s >= 0.0) && (t >= 0.0) && ((s + t) <= 1.0)
6883
}
6984

7085
fn bounding_box(&self) -> BoundingBox {

0 commit comments

Comments
 (0)