refactor: DRY/factor out BendShape::render_discretization

This commit is contained in:
Alain Emilia Anna Zscheile 2024-12-11 13:01:47 +01:00
parent 418d72487a
commit 4859887a5d
3 changed files with 18 additions and 29 deletions

View File

@ -36,26 +36,15 @@ impl<'a> Painter<'a> {
],
egui::Stroke::new(seg.width as f32 * self.transform.scaling, color),
),
PrimitiveShape::Bend(bend) => {
let circle = bend.circle();
let angle_from = bend.start_angle();
let angle_step = bend.spanned_angle() / 100.0;
let points = (0..=100)
.into_iter()
.map(|i| circle.position_at_angle(angle_from + i as f64 * angle_step))
PrimitiveShape::Bend(bend) => egui::Shape::line(
bend.render_discretization(101)
.map(|point| {
self.transform
.mul_pos([point.0.x as f32, -point.0.y as f32].into())
})
.collect();
egui::Shape::line(
points,
egui::Stroke::new(bend.width as f32 * self.transform.scaling, color),
)
}
.collect(),
egui::Stroke::new(bend.width as f32 * self.transform.scaling, color),
),
};
self.ui.painter().add(epaint_shape);

View File

@ -296,6 +296,16 @@ impl BendShape {
angle
}
}
/// Render this bend as a list of points on its circle.
pub fn render_discretization(&self, point_count: usize) -> impl Iterator<Item = Point> + '_ {
let circle = self.circle();
let angle_from = self.start_angle();
// we need to use one less than the whole point count
// because we need to also emit the end-point
let angle_step = self.spanned_angle() / ((point_count - 1) as f64);
(0..point_count).map(move |i| circle.position_at_angle(angle_from + i as f64 * angle_step))
}
}
impl MeasureLength for BendShape {

View File

@ -94,19 +94,9 @@ impl SpecctraDesign {
// line segments.
// TODO: make this configurable? pick a smarter value?
let segment_count: usize = 100;
let circle = bend.circle();
let angle_from = bend.start_angle();
let angle_step = bend.spanned_angle() / segment_count as f64;
(0..=segment_count)
.into_iter()
.map(|i| {
circle
.position_at_angle(angle_from + i as f64 * angle_step)
.into()
})
.collect::<Vec<structure::Point>>()
bend.render_discretization(segment_count + 1)
.map(Into::into)
.collect()
}
// Intentionally skipped for now.