egui: fix painting bends: circles of these are actually inner circles

I've changed field names to avoid repeating this mistake in the future.
This commit is contained in:
Mikolaj Wielgus 2024-06-25 10:47:49 +02:00
parent bd26f5fef8
commit 514eab683a
5 changed files with 55 additions and 43 deletions

View File

@ -16,7 +16,7 @@ impl<'a> Painter<'a> {
pub fn paint_primitive(&mut self, shape: &PrimitiveShape, color: egui::epaint::Color32) {
let epaint_shape = match shape {
PrimitiveShape::Dot(dot) => self.dot_shape(dot.c, color),
PrimitiveShape::Dot(dot) => self.dot_shape(dot.circle, color),
PrimitiveShape::Seg(seg) => egui::Shape::line_segment(
[
self.transform
@ -27,8 +27,9 @@ impl<'a> Painter<'a> {
egui::Stroke::new(seg.width as f32 * self.transform.scale().x, color),
),
PrimitiveShape::Bend(bend) => {
let delta_from = bend.from - bend.c.pos;
let delta_to = bend.to - bend.c.pos;
let circle = bend.circle();
let delta_from = bend.from - circle.pos;
let delta_to = bend.to - circle.pos;
let angle_from = delta_from.y().atan2(delta_from.x());
@ -39,8 +40,8 @@ impl<'a> Painter<'a> {
let mut points: Vec<egui::Pos2> = vec![];
for i in 0..=100 {
let x = bend.c.pos.x() + bend.c.r * (angle_from + i as f64 * angle_step).cos();
let y = bend.c.pos.y() + bend.c.r * (angle_from + i as f64 * angle_step).sin();
let x = circle.pos.x() + circle.r * (angle_from + i as f64 * angle_step).cos();
let y = circle.pos.y() + circle.r * (angle_from + i as f64 * angle_step).sin();
points.push(self.transform.transform_pos([x as f32, -y as f32].into()));
}

View File

@ -21,8 +21,8 @@ impl<'a> Painter<'a> {
PrimitiveShape::Dot(dot) => {
let mut path = Path2D::new();
path.ellipse(
vec2f(dot.c.pos.x() as f32, -dot.c.pos.y() as f32),
dot.c.r as f32,
vec2f(dot.circle.pos.x() as f32, -dot.circle.pos.y() as f32),
dot.circle.r as f32,
0.0,
0.0,
std::f32::consts::TAU,

View File

@ -239,7 +239,7 @@ impl<
pub fn dot_shape(&self, dot: DI) -> PrimitiveShape {
let weight = self.dot_weight(dot);
PrimitiveShape::Dot(DotShape {
c: Circle {
circle: Circle {
pos: weight.pos(),
r: weight.width() / 2.0,
},
@ -261,7 +261,7 @@ impl<
PrimitiveShape::Bend(BendShape {
from: self.dot_weight(from).pos(),
to: self.dot_weight(to).pos(),
c: Circle {
inner_circle: Circle {
pos: core_weight.pos(),
r: self.inner_radius(bend),
},

View File

@ -48,16 +48,16 @@ pub enum PrimitiveShape {
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct DotShape {
pub c: Circle,
pub circle: Circle,
}
impl ShapeTrait for DotShape {
fn center(&self) -> Point {
self.c.pos
self.circle.pos
}
fn contains_point(&self, p: Point) -> bool {
p.euclidean_distance(&self.c.pos) <= self.c.r
p.euclidean_distance(&self.circle.pos) <= self.circle.r
}
}
@ -68,9 +68,9 @@ impl PrimitiveShapeTrait for DotShape {
fn inflate(&self, margin: f64) -> PrimitiveShape {
PrimitiveShape::Dot(DotShape {
c: Circle {
pos: self.c.pos,
r: self.c.r + margin,
circle: Circle {
pos: self.circle.pos,
r: self.circle.r + margin,
},
})
}
@ -82,19 +82,20 @@ impl PrimitiveShapeTrait for DotShape {
match other {
PrimitiveShape::Dot(other) => {
self.c.pos.euclidean_distance(&other.c.pos) < self.c.r + other.c.r
self.circle.pos.euclidean_distance(&other.circle.pos)
< self.circle.r + other.circle.r
}
PrimitiveShape::Seg(other) => {
self.c.pos.euclidean_distance(&other.polygon()) < self.c.r
self.circle.pos.euclidean_distance(&other.polygon()) < self.circle.r
}
PrimitiveShape::Bend(other) => {
for point in math::intersect_circles(&self.c, &other.inner_circle()) {
for point in math::intersect_circles(&self.circle, &other.inner_circle()) {
if other.between_ends(point) {
return true;
}
}
for point in math::intersect_circles(&self.c, &other.outer_circle()) {
for point in math::intersect_circles(&self.circle, &other.outer_circle()) {
if other.between_ends(point) {
return true;
}
@ -108,18 +109,18 @@ impl PrimitiveShapeTrait for DotShape {
fn envelope(&self, margin: f64) -> AABB<[f64; 2]> {
AABB::from_corners(
[
self.c.pos.x() - self.c.r - margin,
self.c.pos.y() - self.c.r - margin,
self.circle.pos.x() - self.circle.r - margin,
self.circle.pos.y() - self.circle.r - margin,
],
[
self.c.pos.x() + self.c.r + margin,
self.c.pos.y() + self.c.r + margin,
self.circle.pos.x() + self.circle.r + margin,
self.circle.pos.y() + self.circle.r + margin,
],
)
}
fn width(&self) -> f64 {
self.c.r * 2.0
self.circle.r * 2.0
}
fn length(&self) -> f64 {
@ -234,46 +235,47 @@ impl PrimitiveShapeTrait for SegShape {
pub struct BendShape {
pub from: Point,
pub to: Point,
pub c: Circle,
pub inner_circle: Circle,
pub width: f64,
}
impl BendShape {
pub fn inner_circle(&self) -> Circle {
self.c
self.inner_circle
}
pub fn circle(&self) -> Circle {
Circle {
pos: self.c.pos,
r: self.c.r + self.width / 2.0,
pos: self.inner_circle.pos,
r: self.inner_circle.r + self.width / 2.0,
}
}
pub fn outer_circle(&self) -> Circle {
Circle {
pos: self.c.pos,
r: self.c.r + self.width,
pos: self.inner_circle.pos,
r: self.inner_circle.r + self.width,
}
}
pub fn between_ends(&self, point: Point) -> bool {
math::between_vectors(
point - self.c.pos,
self.from - self.c.pos,
self.to - self.c.pos,
point - self.inner_circle.pos,
self.from - self.inner_circle.pos,
self.to - self.inner_circle.pos,
)
}
}
impl ShapeTrait for BendShape {
fn center(&self) -> Point {
let sum = (self.from - self.c.pos) + (self.to - self.c.pos);
self.c.pos + (sum / sum.euclidean_distance(&point! {x: 0.0, y: 0.0})) * self.c.r
let sum = (self.from - self.inner_circle.pos) + (self.to - self.inner_circle.pos);
self.inner_circle.pos
+ (sum / sum.euclidean_distance(&point! {x: 0.0, y: 0.0})) * self.inner_circle.r
}
fn contains_point(&self, p: Point) -> bool {
let d = p.euclidean_distance(&self.c.pos);
let d = p.euclidean_distance(&self.inner_circle.pos);
self.between_ends(p) && d >= self.inner_circle().r && d <= self.outer_circle().r
}
}
@ -287,9 +289,9 @@ impl PrimitiveShapeTrait for BendShape {
PrimitiveShape::Bend(BendShape {
from: self.from, // TODO: Is not inflated for now.
to: self.to, // TODO: Is not inflated for now.
c: Circle {
pos: self.c.pos,
r: self.c.r - margin,
inner_circle: Circle {
pos: self.inner_circle.pos,
r: self.inner_circle.r - margin,
},
width: self.width + 2.0 * margin,
})
@ -333,10 +335,16 @@ impl PrimitiveShapeTrait for BendShape {
}
fn envelope(&self, _margin: f64) -> AABB<[f64; 2]> {
let halfwidth = self.c.r + self.width;
let halfwidth = self.inner_circle.r + self.width;
AABB::from_corners(
[self.c.pos.x() - halfwidth, self.c.pos.y() - halfwidth],
[self.c.pos.x() + halfwidth, self.c.pos.y() + halfwidth],
[
self.inner_circle.pos.x() - halfwidth,
self.inner_circle.pos.y() - halfwidth,
],
[
self.inner_circle.pos.x() + halfwidth,
self.inner_circle.pos.y() + halfwidth,
],
)
}

View File

@ -66,6 +66,9 @@ impl GetMaybeNet for ViaWeight {
impl MakePrimitiveShape for ViaWeight {
fn shape(&self) -> PrimitiveShape {
DotShape { c: self.circle }.into()
DotShape {
circle: self.circle,
}
.into()
}
}