mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-21 09:54:09 +01:00
Shapes: Circle tool options pass the tool's own option check
The Circle tool now answers Tool::check_option with the values check_options takes, so tool.setOption and the option check that runs on every tool accept the options the tool reports. - With Third Point by Dragging off, a click in reach of both placed points is on the nearer one (or the one it snapped to). - A press on the second point previews the circle on the two points that its release draws, and a press on the first point previews none. - With Anchors at Standard Positions off, the preview is the path with an anchor at each point, which the release draws. - A move of less than 2 screen points keeps the second or third point where it was pressed. - circle_with_anchors_at gives the two ends of a diameter four anchors also where rounding of the midpoint puts the second point more than half a turn from the first. - The canvas-edge test checks for an outline whether dashed or solid. - docs/mcp.md says that only anchors: "standard" draws a live ellipse.
This commit is contained in:
1 parent
4dbbf4ac44
commit
b66e43f048
4 files changed
+150
-17
No files matched your search
@@ -371,8 +371,9 @@ pub fn circle_through_points(points: &[Point]) -> Option<(Point, f64)> {
|
||||
/// circle, as [`circle_through_points`] gives it) plus as many more as split each arc between two
|
||||
/// of them into equal parts of at most a quarter turn. The path starts at the first point and runs
|
||||
/// as [`ellipse`] does (left, top, right on the page); each curve's handles are 4/3·tan(θ/4) of
|
||||
/// the radius along the tangents at its ends (θ, its turn). A point given twice is one anchor;
|
||||
/// with no points it starts at the left, as [`ellipse`] does.
|
||||
/// the radius along the tangents at its ends (θ, its turn). Two points with `center` at their
|
||||
/// midpoint give four anchors. A point given twice is one anchor; with no points it starts at the
|
||||
/// left, as [`ellipse`] does.
|
||||
pub fn circle_with_anchors_at(center: Point, radius: f64, points: &[Point]) -> PathData {
|
||||
let quarter = PI / 2.0;
|
||||
let angle = |p: Point| (p.y - center.y).atan2(p.x - center.x);
|
||||
@@ -381,6 +382,13 @@ pub fn circle_with_anchors_at(center: Point, radius: f64, points: &[Point]) -> P
|
||||
let mut placed: Vec<(f64, Point)> = points.iter().map(|p| ((angle(*p) - start).rem_euclid(TAU), *p)).collect();
|
||||
placed.sort_by(|a, b| a.0.total_cmp(&b.0));
|
||||
placed.dedup_by(|a, b| a.1 == b.1);
|
||||
// Two points with the center at their midpoint are the ends of a diameter: the second is half a
|
||||
// turn from the first, also where rounding moves the angle from the center off it.
|
||||
if let [(_, a), (turn, b)] = placed.as_mut_slice()
|
||||
&& a.midpoint(*b) == center
|
||||
{
|
||||
*turn = PI;
|
||||
}
|
||||
if placed.is_empty() {
|
||||
placed.push((0.0, Point::new(center.x - radius, center.y)));
|
||||
}
|
||||
@@ -861,6 +869,20 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
/// The two ends of a diameter give four anchors, also where the midpoint rounds off it: at
|
||||
/// x = 10⁶, steps of 2⁻³³ put the second point more than half a turn from the first, seen from
|
||||
/// the rounded center.
|
||||
#[test]
|
||||
fn a_diameter_gives_four_anchors_where_its_midpoint_rounds() {
|
||||
let u = 2f64.powi(-33);
|
||||
let (a, b) = (Point::new(1_000_000.0, 0.0), Point::new(1_000_000.0 + 9.0 * u, 8.0 * u));
|
||||
let (center, radius) = circle_through_points(&[a, b]).expect("a circle");
|
||||
let path = circle_with_anchors_at(center, radius, &[a, b]);
|
||||
let sub = &path.subpaths[0];
|
||||
assert_eq!(sub.anchors.len(), 4, "{sub:?}");
|
||||
assert_eq!((sub.anchors[0].p, sub.anchors[2].p), (a, b));
|
||||
}
|
||||
|
||||
/// Nearly on one line: a point 10⁻⁴ off a chord 2000 long still gives the circle (its sagitta
|
||||
/// formula `r = (h² + (L/2)²) / 2h`), and its center sits on the chord's perpendicular
|
||||
/// bisector; a point 10⁻¹² off it does not.
|
||||
|
||||
+42
-12
@@ -205,8 +205,16 @@ impl CircleTool {
|
||||
fn points(&self) -> Vec<Point> {
|
||||
let mut out: Vec<Point> = self.first.into_iter().collect();
|
||||
if let Some((b, _)) = self.second {
|
||||
out.push(b);
|
||||
out.extend(self.pointer);
|
||||
match self.press {
|
||||
// Let go where it was pressed, a press on the first point draws nothing and one on
|
||||
// the second point draws the circle on the two.
|
||||
Some(Press::Third { on_first: true, dragged: false, .. }) => {}
|
||||
Some(Press::Third { on_second: true, dragged: false, .. }) => out.push(b),
|
||||
_ => {
|
||||
out.push(b);
|
||||
out.extend(self.pointer);
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
match self.press {
|
||||
@@ -302,9 +310,16 @@ impl Tool for CircleTool {
|
||||
PointerKind::Down => {
|
||||
if let (Some(a), Some((b, clicked))) = (self.first, self.second) {
|
||||
let at = self.snap.press(cx, ev.pos, &[], None);
|
||||
let on = |q: Point| at == q || ev.pos.distance(q) <= cx.tol(REACH_PX);
|
||||
let on_second = on(b) || ev.pos.distance(clicked) <= cx.tol(REACH_PX);
|
||||
self.press = Some(Press::Third { at, from: ev.pos, on_first: on(a), on_second, dragged: false });
|
||||
let reach = cx.tol(REACH_PX);
|
||||
let to_second = ev.pos.distance(b).min(ev.pos.distance(clicked));
|
||||
let (mut on_first, mut on_second) = (at == a || ev.pos.distance(a) <= reach, at == b || to_second <= reach);
|
||||
// A press in reach of both points is on the one it snapped to, otherwise on the
|
||||
// nearer one.
|
||||
if on_first && on_second {
|
||||
let first = if (at == a) != (at == b) { at == a } else { ev.pos.distance(a) < to_second };
|
||||
(on_first, on_second) = (first, !first);
|
||||
}
|
||||
self.press = Some(Press::Third { at, from: ev.pos, on_first, on_second, dragged: false });
|
||||
self.pointer = Some(at);
|
||||
return vec![];
|
||||
}
|
||||
@@ -332,17 +347,22 @@ impl Tool for CircleTool {
|
||||
match &mut self.press {
|
||||
// With `dragThird` off, the second point follows the pointer as it does before
|
||||
// the press.
|
||||
// The point stays where it was pressed until the press is a drag.
|
||||
Some(Press::Second { at, snapped, from, on_first: false, dragged }) if !drag_third => {
|
||||
*dragged |= ev.pos.distance(*from) >= cx.tol(DRAG_PX);
|
||||
let leave = first.map(|a| Leave::segment(cx, a, ev.mods.shift));
|
||||
*snapped = self.snap.drag(cx, ev.pos, leave.as_ref());
|
||||
*at = first.map_or(*snapped, |a| round_diameter(cx, a, *snapped, ev.mods));
|
||||
self.pointer = Some(*at);
|
||||
if *dragged {
|
||||
let leave = first.map(|a| Leave::segment(cx, a, ev.mods.shift));
|
||||
*snapped = self.snap.drag(cx, ev.pos, leave.as_ref());
|
||||
*at = first.map_or(*snapped, |a| round_diameter(cx, a, *snapped, ev.mods));
|
||||
self.pointer = Some(*at);
|
||||
}
|
||||
}
|
||||
Some(Press::Third { at, from, dragged, .. }) => {
|
||||
*dragged |= ev.pos.distance(*from) >= cx.tol(DRAG_PX);
|
||||
*at = self.snap.drag(cx, ev.pos, None);
|
||||
self.pointer = Some(*at);
|
||||
if *dragged {
|
||||
*at = self.snap.drag(cx, ev.pos, None);
|
||||
self.pointer = Some(*at);
|
||||
}
|
||||
}
|
||||
Some(Press::First { at, from, dragged }) => {
|
||||
*dragged |= ev.pos.distance(*from) >= cx.tol(DRAG_PX);
|
||||
@@ -443,7 +463,12 @@ impl Tool for CircleTool {
|
||||
let points = self.points();
|
||||
let color = crate::draw2::FEEDBACK;
|
||||
if let Some((c, r)) = drawn_circle(&points, self.options.standard_anchors) {
|
||||
let path = shapes::ellipse(Rect::new(c.x - r, c.y - r, c.x + r, c.y + r)).to_bezpath();
|
||||
// The path the release draws: with `standardAnchors` off, the one with an anchor at each point.
|
||||
let path = if self.options.standard_anchors {
|
||||
shapes::ellipse(Rect::new(c.x - r, c.y - r, c.x + r, c.y + r)).to_bezpath()
|
||||
} else {
|
||||
shapes::circle_with_anchors_at(c, r, &points).to_bezpath()
|
||||
};
|
||||
// The canvas draws a dashed outline dash by dash along its whole length: a circle longer
|
||||
// than DASHED_PX on screen is drawn solid instead.
|
||||
let dashed = std::f64::consts::TAU * r <= cx.tol(DASHED_PX);
|
||||
@@ -489,6 +514,11 @@ impl Tool for CircleTool {
|
||||
})
|
||||
}
|
||||
|
||||
/// Takes the values [`check_options`] takes.
|
||||
fn check_option(&self, key: &str, value: &Value) -> Result<(), String> {
|
||||
check_options(&Map::from_iter([(key.to_string(), value.clone())]))
|
||||
}
|
||||
|
||||
/// Sets one option; a value the option does not take leaves it as it was. Switching
|
||||
/// `dragThird` drops the points placed so far.
|
||||
fn set_option(&mut self, key: &str, value: &Value) {
|
||||
|
||||
@@ -162,6 +162,25 @@ fn anchors_at_the_points_asks_for_them() {
|
||||
assert_eq!(acts, vec![Action::Exec("shape.circleThroughPoints".into(), json!({"points": points, "anchors": "points"}))]);
|
||||
}
|
||||
|
||||
/// With `standardAnchors` off, the tool shows the path with an anchor at each point, the one its
|
||||
/// release draws.
|
||||
#[test]
|
||||
fn anchors_at_the_points_show_the_path_they_draw() {
|
||||
let (d, _) = doc_with_rect();
|
||||
let (s, p) = (Selection::default(), paint());
|
||||
let cx = ToolContext { smart_guides: false, ..cx(&d, &s, &p) };
|
||||
let mut t = CircleTool::default();
|
||||
t.set_option("standardAnchors", &json!(false));
|
||||
click(&mut t, &cx, (300.0, 300.0));
|
||||
t.pointer(&cx, &ev(PointerKind::Down, (380.0, 300.0), Mods::default()));
|
||||
t.pointer(&cx, &ev(PointerKind::Drag, (310.0, 340.0), Mods::default()));
|
||||
let points = [Point::new(300.0, 300.0), Point::new(380.0, 300.0), Point::new(310.0, 340.0)];
|
||||
let (c, r) = circle_of(&points).expect("a circle");
|
||||
let want = shapes::circle_with_anchors_at(c, r, &points).to_bezpath();
|
||||
let o = t.overlays(&cx);
|
||||
assert!(o.iter().any(|o| matches!(o, Overlay::Path { path, .. } if *path == want)), "{o:?}");
|
||||
}
|
||||
|
||||
/// With `standardAnchors` off, a circle within 1 pt of the canvas edge whose path with anchors at
|
||||
/// the points curves past that edge is not shown and not drawn; with standard anchors it is.
|
||||
#[test]
|
||||
@@ -180,10 +199,67 @@ fn anchors_at_the_points_keep_the_path_on_the_canvas() {
|
||||
click(&mut t, &cx, points[0]);
|
||||
t.pointer(&cx, &ev(PointerKind::Down, points[1], Mods::default()));
|
||||
t.pointer(&cx, &ev(PointerKind::Drag, points[2], Mods::default()));
|
||||
assert_eq!(preview(&t.overlays(&cx)), None);
|
||||
// No outline at all: at this size the tool would draw it solid, not dashed.
|
||||
let o = t.overlays(&cx);
|
||||
assert!(!o.iter().any(|o| matches!(o, Overlay::Path { .. })), "{o:?}");
|
||||
assert_eq!(t.pointer(&cx, &ev(PointerKind::Up, points[2], Mods::default())), vec![]);
|
||||
}
|
||||
|
||||
/// With `dragThird` off, a click in reach of both placed points is on the nearer one: nearer the
|
||||
/// first point it places nothing, nearer the second it draws the circle on the two.
|
||||
#[test]
|
||||
fn with_third_point_by_dragging_off_a_click_near_both_points_takes_the_nearer() {
|
||||
let (d, _) = doc_with_rect();
|
||||
let (s, p) = (Selection::default(), paint());
|
||||
let cx = ToolContext { smart_guides: false, ..cx(&d, &s, &p) };
|
||||
let mut t = CircleTool::default();
|
||||
t.set_option("dragThird", &json!(false));
|
||||
click(&mut t, &cx, (100.0, 100.0));
|
||||
assert_eq!(click(&mut t, &cx, (104.0, 100.0)), vec![]);
|
||||
assert_eq!(click(&mut t, &cx, (101.0, 100.0)), vec![]);
|
||||
assert_eq!(click(&mut t, &cx, (103.0, 100.0)), circle(&[(100.0, 100.0), (104.0, 100.0)]));
|
||||
}
|
||||
|
||||
/// With `dragThird` off, a press on the second point shows the circle on the two points, which its
|
||||
/// release draws, and a press on the first point shows none.
|
||||
#[test]
|
||||
fn with_third_point_by_dragging_off_a_press_shows_the_circle_its_release_draws() {
|
||||
let (d, _) = doc_with_rect();
|
||||
let (s, p) = (Selection::default(), paint());
|
||||
let cx = ToolContext { smart_guides: false, ..cx(&d, &s, &p) };
|
||||
let mut t = CircleTool::default();
|
||||
t.set_option("dragThird", &json!(false));
|
||||
click(&mut t, &cx, (100.0, 100.0));
|
||||
click(&mut t, &cx, (200.0, 100.0));
|
||||
t.pointer(&cx, &ev(PointerKind::Down, (101.0, 101.0), Mods::default()));
|
||||
assert_eq!(preview(&t.overlays(&cx)), None);
|
||||
assert_eq!(t.pointer(&cx, &ev(PointerKind::Up, (101.0, 101.0), Mods::default())), vec![]);
|
||||
t.pointer(&cx, &ev(PointerKind::Down, (202.0, 101.0), Mods::default()));
|
||||
let o = t.overlays(&cx);
|
||||
assert!(near(preview(&o).expect("the circle is shown"), Rect::new(100.0, 50.0, 200.0, 150.0)), "{o:?}");
|
||||
assert_eq!(t.pointer(&cx, &ev(PointerKind::Up, (202.0, 101.0), Mods::default())), circle(&[(100.0, 100.0), (200.0, 100.0)]));
|
||||
}
|
||||
|
||||
/// With `dragThird` off, a press that moves less than 2 screen points keeps the point where it was
|
||||
/// pressed, for the second point and for the third.
|
||||
#[test]
|
||||
fn with_third_point_by_dragging_off_a_short_move_keeps_the_pressed_point() {
|
||||
let (d, _) = doc_with_rect();
|
||||
let (s, p) = (Selection::default(), paint());
|
||||
let cx = ToolContext { smart_guides: false, ..cx(&d, &s, &p) };
|
||||
let (down, drag, up) = (PointerKind::Down, PointerKind::Drag, PointerKind::Up);
|
||||
let mut t = CircleTool::default();
|
||||
t.set_option("dragThird", &json!(false));
|
||||
click(&mut t, &cx, (100.0, 100.0));
|
||||
t.pointer(&cx, &ev(down, (200.0, 100.0), Mods::default()));
|
||||
t.pointer(&cx, &ev(drag, (201.0, 100.0), Mods::default()));
|
||||
assert_eq!(t.pointer(&cx, &ev(up, (200.0, 100.0), Mods::default())), vec![]);
|
||||
t.pointer(&cx, &ev(down, (150.0, 200.0), Mods::default()));
|
||||
t.pointer(&cx, &ev(drag, (151.0, 200.0), Mods::default()));
|
||||
let three = circle(&[(100.0, 100.0), (200.0, 100.0), (150.0, 200.0)]);
|
||||
assert_eq!(t.pointer(&cx, &ev(up, (150.0, 200.0), Mods::default())), three);
|
||||
}
|
||||
|
||||
/// With `dragThird` off, a second click places the second point and a third click draws the circle
|
||||
/// through the three; a click on the second point again (as a double-click there does) draws the
|
||||
/// circle on the two; a second point let go on the first places nothing; a drag from the first
|
||||
@@ -300,6 +376,9 @@ fn an_alt_click_places_the_second_point_where_it_is_shown() {
|
||||
fn check_options_takes_only_the_values_the_options_take() {
|
||||
let ok = json!({"center": false, "radii": true, "diameter": false, "color": "gray", "standardAnchors": false, "dragThird": false});
|
||||
assert_eq!(check_options(ok.as_object().unwrap()), Ok(()));
|
||||
// The tool's own check, which `tool.setOption` also runs, takes the same values.
|
||||
let tool = CircleTool::default();
|
||||
assert_eq!(tool.check_options(ok.as_object().unwrap()), Ok(()));
|
||||
for (bad, error) in [
|
||||
(json!({"center": 1}), "`center` must be true or false"),
|
||||
(json!({"dragThird": null}), "`dragThird` must be true or false"),
|
||||
@@ -307,6 +386,7 @@ fn check_options_takes_only_the_values_the_options_take() {
|
||||
(json!({"colour": "red"}), "unknown Circle tool option `colour`"),
|
||||
] {
|
||||
assert_eq!(check_options(bad.as_object().unwrap()), Err(error.to_string()), "{bad}");
|
||||
assert_eq!(tool.check_options(bad.as_object().unwrap()), Err(error.to_string()), "{bad}");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+3
-2
@@ -6582,8 +6582,9 @@ slider back to the Auto-Smooth level.
|
||||
|
||||
## Circle through two or three points
|
||||
|
||||
`shape.circleThroughPoints {points?: [[x, y], [x, y]] or [[x, y], [x, y], [x, y]]}` draws the circle whose edge passes
|
||||
through the points, as a live ellipse with the current paint, and selects it. Two points are the ends of a diameter.
|
||||
`shape.circleThroughPoints {points?: [[x, y], [x, y]] or [[x, y], [x, y], [x, y]], anchors?}` draws the circle whose
|
||||
edge passes through the points, with the current paint, and selects it: a live ellipse with the default
|
||||
`anchors: "standard"`, and a plain path with `anchors: "points"`. Two points are the ends of a diameter.
|
||||
Without `points`, it uses the two or three selected anchor points, from one path or several: anchor points selected with
|
||||
Direct Selection, or all the anchor points of a path selected whole, such as a triangle. The ends of selected segments
|
||||
do not count. The result is `{id, cx, cy, radius}`, and one undo removes the circle. The circle is not pixel-aligned,
|
||||
|
||||
Reference in new issue
Block a user