Skip to content

Commit 4e6317c

Browse files
committed
try1
1 parent 123764a commit 4e6317c

File tree

1 file changed

+25
-27
lines changed
  • plotters-backend/src/rasterizer

1 file changed

+25
-27
lines changed

plotters-backend/src/rasterizer/path.rs

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,7 @@ fn compute_polygon_vertex(triple: &[BackendCoord; 3], d: f64, buf: &mut Vec<Back
3434
f64::from(triple[1].1) + d * b_n.1,
3535
);
3636

37-
// Check if 3 points are colinear. If so, just emit the point.
38-
if a_t.1 * b_t.0 == a_t.0 * b_t.1 {
39-
buf.push((a_p.0 as i32, a_p.1 as i32));
40-
return;
41-
}
42-
43-
// So we are actually computing the intersection of two lines:
37+
// We want to compute the intersection of two lines:
4438
// a_p + u * a_t and b_p + v * b_t.
4539
// We can solve the following vector equation:
4640
// u * a_t + a_p = v * b_t + b_p
@@ -61,30 +55,34 @@ fn compute_polygon_vertex(triple: &[BackendCoord; 3], d: f64, buf: &mut Vec<Back
6155
let b1 = -b_t.1;
6256
let c1 = b_p.1 - a_p.1;
6357

64-
let mut x = f64::INFINITY;
65-
let mut y = f64::INFINITY;
66-
67-
// Well if the determinant is not 0, then we can actually get a intersection point.
68-
if (a0 * b1 - a1 * b0).abs() > f64::EPSILON {
69-
let u = (c0 * b1 - c1 * b0) / (a0 * b1 - a1 * b0);
70-
71-
x = a_p.0 + u * a_t.0;
72-
y = a_p.1 + u * a_t.1;
58+
// If the determinant is 0, then we cannot actuall get a intersection point.
59+
// n that case, the two lines are parallel and we just emit the point a_p \approx b_p
60+
if ( a0 * b1 - a1 * b0).abs() <= f64::EPSILON {
61+
buf.push((a_p.0 as i32, a_p.1 as i32));
62+
return;
7363
}
74-
75-
let cross_product = a_t.0 * b_t.1 - a_t.1 * b_t.0;
76-
if (cross_product < 0.0 && d < 0.0) || (cross_product > 0.0 && d > 0.0) {
77-
// Then we are at the outer side of the angle, so we need to consider a cap.
78-
let dist_square = (x - triple[1].0 as f64).powi(2) + (y - triple[1].1 as f64).powi(2);
79-
// If the point is too far away from the line, we need to cap it.
80-
if dist_square > d * d * 16.0 {
81-
buf.push((a_p.0.round() as i32, a_p.1.round() as i32));
82-
buf.push((b_p.0.round() as i32, b_p.1.round() as i32));
83-
return;
64+
else{
65+
let u = (c0 * b1 - c1 * b0) / (a0 * b1 - a1 * b0);
66+
let x = a_p.0 + u * a_t.0;
67+
let y = a_p.1 + u * a_t.1;
68+
69+
let cross_product = a_t.0 * b_t.1 - a_t.1 * b_t.0;
70+
if (cross_product < 0.0 && d < 0.0) || (cross_product > 0.0 && d > 0.0) {
71+
// Then we are at the outter side of the angle, so we need to consider a cap.
72+
let dist_square = (x - triple[1].0 as f64).powi(2) + (y - triple[1].1 as f64).powi(2);
73+
// If the point is too far away from the line, we need to cap it.
74+
if dist_square > d * d * 16.0 {
75+
buf.push((a_p.0.round() as i32, a_p.1.round() as i32));
76+
buf.push((b_p.0.round() as i32, b_p.1.round() as i32));
77+
return;
78+
}
79+
} else {
80+
// We are at the inner side of the angle, so we just emit the point.
81+
buf.push((x.round() as i32, y.round() as i32));
8482
}
8583
}
8684

87-
buf.push((x.round() as i32, y.round() as i32));
85+
8886
}
8987

9088
fn traverse_vertices<'a>(

0 commit comments

Comments
 (0)