* claim: #7395 * Sketch: a circumscribed polygon stays regular when dragged (#7395) Circumscribed Polygon held its sides equal and tangent to the support circle only. Equal sides round one circle leave every other corner free (a rhombus round a circle), so a hexagon had 9 degrees of freedom instead of 4, and dragging its centre or a corner pulled it out of shape (radius 10 became 8.02, angles 174/66 degrees). Its corners now also lie on a construction circle concentric with the support circle, which makes it regular as an inscribed polygon is, and one side is tangent to the support circle, which sets its size: dof 4 (centre, size, turn). One tangent rather than all of them, so no constraint is redundant. Test in sketch_ux_tests: 4, 5 and 6 sides, dragged by the centre and by a corner, stay regular with their sides on the support circle. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
b1cf66b628
commit
1a9c4cd99d
2 files changed
+78
-2
No files matched your search
@@ -1419,8 +1419,20 @@ fn polygon(s: &mut Session, p: &Value, cmd: &str, center: Vec2, r_vertex: f64, s
|
||||
cons.push(add_c(sk, ConstraintKind::PointOnCurve { p: *pt, c: circ })?);
|
||||
}
|
||||
} else {
|
||||
for li in &l {
|
||||
cons.push(add_c(sk, ConstraintKind::Tangent { a: *li, b: circ })?);
|
||||
// Equal sides tangent to one circle don't make a polygon regular: every other corner
|
||||
// stays free (a rhombus round a circle), and a drag pulled it out of shape (#7395).
|
||||
// The corners on a second circle, concentric, make it regular as the inscribed one
|
||||
// is; one side tangent to the support circle then sets its size (the others follow).
|
||||
let corner = sk.add_circle(center, r_vertex, None, None)?;
|
||||
if let Some(cu) = sk.curves.get_mut(corner) {
|
||||
cu.construction = true;
|
||||
}
|
||||
cons.push(add_c(sk, ConstraintKind::Concentric { a: circ, b: corner })?);
|
||||
for pt in &pts {
|
||||
cons.push(add_c(sk, ConstraintKind::PointOnCurve { p: *pt, c: corner })?);
|
||||
}
|
||||
if let Some(first) = l.first() {
|
||||
cons.push(add_c(sk, ConstraintKind::Tangent { a: *first, b: circ })?);
|
||||
}
|
||||
}
|
||||
Ok((ids_of(sk, &l), cons))
|
||||
|
||||
@@ -574,6 +574,70 @@ fn v(x: f64, y: f64) -> solidcraft_geom::Vec2 {
|
||||
solidcraft_geom::Vec2::new(x, y)
|
||||
}
|
||||
|
||||
/// The corners of the polygon drawn by `out`, in order, and its support circle's centre and radius.
|
||||
fn polygon_shape(s: &Session, out: &Value) -> (Vec<solidcraft_geom::Vec2>, solidcraft_geom::Vec2, f64) {
|
||||
use solidcraft_sketch::CurveKind;
|
||||
let sk = sketch(s);
|
||||
let corners = out["curves"]
|
||||
.as_array()
|
||||
.unwrap()
|
||||
.iter()
|
||||
.map(|id| match &sk.curves[sk.curve_index(id.as_str().unwrap()).unwrap()].kind {
|
||||
CurveKind::Line { a, .. } => sk.point(*a).unwrap(),
|
||||
other => panic!("a side is a line: {other:?}"),
|
||||
})
|
||||
.collect();
|
||||
let support = &sk.curves[sk.curve_index(out["support_circle"].as_str().unwrap()).unwrap()].kind;
|
||||
let CurveKind::Circle { c, r } = support else { panic!("support circle: {support:?}") };
|
||||
(corners, sk.point(*c).unwrap(), *r)
|
||||
}
|
||||
|
||||
/// A regular polygon: corners equally far from `c`, sides equally long.
|
||||
fn assert_regular(corners: &[solidcraft_geom::Vec2], c: solidcraft_geom::Vec2, what: &str) {
|
||||
let n = corners.len();
|
||||
let reach: Vec<f64> = corners.iter().map(|q| q.dist(c)).collect();
|
||||
let sides: Vec<f64> = (0..n).map(|i| corners[i].dist(corners[(i + 1) % n])).collect();
|
||||
let even = |xs: &[f64]| xs.iter().all(|x| (x - xs[0]).abs() < 1e-6);
|
||||
assert!(even(&reach) && even(&sides), "{what}: corners {reach:?}, sides {sides:?}");
|
||||
}
|
||||
|
||||
/// #7395: a circumscribed polygon had equal sides tangent to its circle only, which leaves every
|
||||
/// other corner free (dof 9 for a hexagon); dragging its centre or a corner pulled it out of
|
||||
/// shape. It is held regular now, as an inscribed one: dof 4 (centre, size, turn), and a drag
|
||||
/// moves or turns it without deforming it.
|
||||
#[test]
|
||||
fn dragging_a_circumscribed_polygon_keeps_it_regular() {
|
||||
for sides in [4, 5, 6] {
|
||||
for drag_corner in [false, true] {
|
||||
let mut s = new_sketch();
|
||||
let out = run(&mut s, "sketch.polygon.circumscribed", json!({"center": [50, 50], "radius": 10, "sides": sides}));
|
||||
assert_eq!(out["sketch"]["dof"], 4, "{sides} sides: centre, size and turn only: {}", out["sketch"]);
|
||||
let (corners, c, r) = polygon_shape(&s, &out);
|
||||
assert_regular(&corners, c, "as drawn");
|
||||
assert!((r - 10.0).abs() < 1e-9);
|
||||
let support = out["support_circle"].as_str().unwrap();
|
||||
if drag_corner {
|
||||
let from = corners[0];
|
||||
let to = from + v(3.0, -4.0);
|
||||
drag(&mut s, &format!("{}.start", out["curves"][0].as_str().unwrap()), [from.x, from.y], [to.x, to.y]);
|
||||
} else {
|
||||
drag(&mut s, &format!("{support}.center"), [50.0, 50.0], [70.0, 60.0]);
|
||||
}
|
||||
run(&mut s, "sketch.drag_end", json!({}));
|
||||
let (corners, c, r) = polygon_shape(&s, &out);
|
||||
let what = format!("{sides} sides, dragged by its {}", if drag_corner { "corner" } else { "centre" });
|
||||
assert_regular(&corners, c, &what);
|
||||
let apothem = corners[0].dist(corners[1]) / (2.0 * (std::f64::consts::PI / sides as f64).tan());
|
||||
assert!((apothem - r).abs() < 1e-6, "{what}: the sides touch the support circle (apothem {apothem}, radius {r})");
|
||||
// A drag may change a free size by a hair (an inscribed polygon's too); the polygon
|
||||
// went from radius 10 to 8.02.
|
||||
if !drag_corner {
|
||||
assert!((r - 10.0).abs() < 1e-2 && c.dist(v(70.0, 60.0)) < 1e-6, "{what}: moved whole, radius {r}, centre {c:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dragging_a_line_slides_it_and_an_end_keeps_the_other_end() {
|
||||
let mut s = new_sketch();
|
||||
|
||||
Reference in new issue
Block a user