Skip to content

Commit 8108c9e

Browse files
committed
Logical fix.
1 parent d964314 commit 8108c9e

File tree

1 file changed

+28
-30
lines changed

1 file changed

+28
-30
lines changed

plotters/src/element/basic_shapes.rs

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -189,40 +189,38 @@ impl<I0: Iterator + Clone, Size: SizeDesc, DB: DrawingBackend> Drawable<DB>
189189
};
190190
let size = self.size.in_pixels(&ps).max(0) as f32;
191191
let spacing = self.spacing.in_pixels(&ps).max(0) as f32;
192-
let mut is_previous_solid = false;
192+
let mut dist = 0.;
193+
let mut is_solid = true;
194+
let mut queue = vec![to_i(start)];
193195
for curr in points {
194196
let curr_f = to_f(curr);
195197
let (dx, dy) = (curr_f.0 - start.0, curr_f.1 - start.1);
196-
let mut d = dx.hypot(dy).max(f32::EPSILON);
197-
let scale = size / d;
198-
let gap_scale = spacing / d;
199-
// Start drawing until last segment
200-
// 1) o-- -- o (need to patch last one)
201-
// 2) o-- -- o (ignore the last one)
202-
// 3) o o (points are too dense)
203-
if is_previous_solid {
204-
start.0 += dx * gap_scale;
205-
start.1 += dy * gap_scale;
206-
d -= spacing;
198+
let d = dx.hypot(dy).max(f32::EPSILON);
199+
dist += d;
200+
if is_solid {
201+
if dist < size {
202+
queue.push(curr);
203+
start = curr_f;
204+
} else {
205+
let t = (dist - size) / d;
206+
start = (start.0 + dx * t, start.1 + dy * t);
207+
queue.push(to_i(start));
208+
backend.draw_path(queue.drain(..), &self.style)?;
209+
dist = 0.;
210+
is_solid = false;
211+
}
212+
} else if dist < spacing {
213+
start = curr_f;
214+
} else {
215+
let t = (dist - spacing) / d;
216+
start = (start.0 + dx * t, start.1 + dy * t);
217+
queue.push(to_i(start));
218+
dist = 0.;
219+
is_solid = true;
207220
}
208-
while d >= size {
209-
// Solid line
210-
let end = (start.0 + dx * scale, start.1 + dy * scale);
211-
backend.draw_path([to_i(start), to_i(end)], &self.style)?;
212-
// Spacing
213-
start = (end.0 + dx * gap_scale, end.1 + dy * gap_scale);
214-
d -= size + spacing;
215-
}
216-
// Finish the last segment
217-
// 1) o-- -- -o (patched)
218-
// 2) o-o (become solid line)
219-
let line = [to_i(start), curr];
220-
is_previous_solid = d > 0. && line[0] != line[1];
221-
if is_previous_solid {
222-
backend.draw_path(line, &self.style)?;
223-
}
224-
// Move to the current point
225-
start = curr_f;
221+
}
222+
if queue.len() > 1 {
223+
backend.draw_path(queue, &self.style)?;
226224
}
227225
Ok(())
228226
}

0 commit comments

Comments
 (0)