Skip to content

Commit 91dfbc8

Browse files
committed
Code formatting and comments
1 parent 83199ed commit 91dfbc8

File tree

1 file changed

+24
-15
lines changed
  • plotters-backend/src/rasterizer

1 file changed

+24
-15
lines changed

plotters-backend/src/rasterizer/path.rs

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,23 @@ fn get_dir_vector(from: BackendCoord, to: BackendCoord, flag: bool) -> ((f64, f6
1414
}
1515
}
1616

17-
// Compute the polygonized vertex of the given angle
18-
// d is the distance between the polygon edge and the actual line.
19-
// d can be negative, this will emit a vertex on the other side of the line.
17+
/// Consider two line segments between three points: t1-t2-t3
18+
/// Imagine the line bends to the right, and we run along the line on the right hand side.
19+
/// In that case, we make an "inside corner" at t2.
20+
/// This function would compute the point close to t2 that makes the line have thickness `d`
21+
///
22+
/// For "outside corners" (the line bends to the left), we sometimes get too pointy corners
23+
/// if there is just one new point. In that case, we add a cap to make the corner look better.
24+
/// In that case, the function emits two points.
25+
///
26+
/// The function will return values via the `buf` parameter, after clearing it.
27+
///
28+
/// d can be negative, this will emit a vertex on the other side of the line.
29+
///
2030
fn compute_polygon_vertex(triple: &[BackendCoord; 3], d: f64, buf: &mut Vec<BackendCoord>) {
2131
buf.clear();
2232

23-
// Compute the tanginal and normal vectors of the given straight line.
33+
// Compute the tangential and normal vectors of the given straight line.
2434
let (a_t, a_n) = get_dir_vector(triple[0], triple[1], false);
2535
let (b_t, b_n) = get_dir_vector(triple[2], triple[1], true);
2636

@@ -55,24 +65,25 @@ fn compute_polygon_vertex(triple: &[BackendCoord; 3], d: f64, buf: &mut Vec<Back
5565
let b1 = -b_t.1;
5666
let c1 = b_p.1 - a_p.1;
5767

58-
// If the determinant is 0, then we cannot actually 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 {
68+
// If the determinant is 0, then we cannot actually get an intersection point.
69+
// In that case, the two lines are parallel and we just emit the point a_p, which is
70+
// approximately equal to b_p
71+
if (a0 * b1 - a1 * b0).abs() <= f64::EPSILON {
6172
buf.push((a_p.0 as i32, a_p.1 as i32));
6273
return;
63-
}
64-
else{
74+
} else {
6575
let u = (c0 * b1 - c1 * b0) / (a0 * b1 - a1 * b0);
6676
let x = a_p.0 + u * a_t.0;
6777
let y = a_p.1 + u * a_t.1;
68-
78+
6979
let cross_product = a_t.0 * b_t.1 - a_t.1 * b_t.0;
70-
let is_outside_the_angle = (cross_product < 0.0 && d < 0.0) || (cross_product > 0.0 && d > 0.0);
80+
let is_outside_the_angle =
81+
(cross_product < 0.0 && d < 0.0) || (cross_product > 0.0 && d > 0.0);
7182
if is_outside_the_angle {
72-
// Then we are at the outter side of the angle, so we need to consider a cap.
83+
// We are at the outer side of the angle, so we need to consider a cap.
7384
let dist_square = (x - triple[1].0 as f64).powi(2) + (y - triple[1].1 as f64).powi(2);
7485
let needs_capping = dist_square > d * d * 16.0;
75-
if needs_capping {
86+
if needs_capping {
7687
// If the point is too far away from the line, we need to cap it to make it look okay
7788
buf.push((a_p.0.round() as i32, a_p.1.round() as i32));
7889
buf.push((b_p.0.round() as i32, b_p.1.round() as i32));
@@ -86,8 +97,6 @@ fn compute_polygon_vertex(triple: &[BackendCoord; 3], d: f64, buf: &mut Vec<Back
8697
buf.push((x.round() as i32, y.round() as i32));
8798
}
8899
}
89-
90-
91100
}
92101

93102
fn traverse_vertices<'a>(

0 commit comments

Comments
 (0)