mirror of https://codeberg.org/topola/topola.git
refactor: DRY/factor out BendShape::render_discretization
This commit is contained in:
parent
418d72487a
commit
4859887a5d
|
|
@ -36,26 +36,15 @@ impl<'a> Painter<'a> {
|
||||||
],
|
],
|
||||||
egui::Stroke::new(seg.width as f32 * self.transform.scaling, color),
|
egui::Stroke::new(seg.width as f32 * self.transform.scaling, color),
|
||||||
),
|
),
|
||||||
PrimitiveShape::Bend(bend) => {
|
PrimitiveShape::Bend(bend) => egui::Shape::line(
|
||||||
let circle = bend.circle();
|
bend.render_discretization(101)
|
||||||
|
|
||||||
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))
|
|
||||||
.map(|point| {
|
.map(|point| {
|
||||||
self.transform
|
self.transform
|
||||||
.mul_pos([point.0.x as f32, -point.0.y as f32].into())
|
.mul_pos([point.0.x as f32, -point.0.y as f32].into())
|
||||||
})
|
})
|
||||||
.collect();
|
.collect(),
|
||||||
|
|
||||||
egui::Shape::line(
|
|
||||||
points,
|
|
||||||
egui::Stroke::new(bend.width as f32 * self.transform.scaling, color),
|
egui::Stroke::new(bend.width as f32 * self.transform.scaling, color),
|
||||||
)
|
),
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
self.ui.painter().add(epaint_shape);
|
self.ui.painter().add(epaint_shape);
|
||||||
|
|
|
||||||
|
|
@ -296,6 +296,16 @@ impl BendShape {
|
||||||
angle
|
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 {
|
impl MeasureLength for BendShape {
|
||||||
|
|
|
||||||
|
|
@ -94,19 +94,9 @@ impl SpecctraDesign {
|
||||||
// line segments.
|
// line segments.
|
||||||
// TODO: make this configurable? pick a smarter value?
|
// TODO: make this configurable? pick a smarter value?
|
||||||
let segment_count: usize = 100;
|
let segment_count: usize = 100;
|
||||||
|
bend.render_discretization(segment_count + 1)
|
||||||
let circle = bend.circle();
|
.map(Into::into)
|
||||||
let angle_from = bend.start_angle();
|
.collect()
|
||||||
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>>()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Intentionally skipped for now.
|
// Intentionally skipped for now.
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue