diff --git a/crates/draw/src/geometry/shape.rs b/crates/draw/src/geometry/shape.rs index 112fc4d2d..fff27a2bd 100644 --- a/crates/draw/src/geometry/shape.rs +++ b/crates/draw/src/geometry/shape.rs @@ -40,9 +40,14 @@ pub enum ShapeGeometry { /// The bounds of the ellipse. rect: Rect, }, - /// A regular polygon or star inscribed in the ellipse of `rect` (the Box frame: PhotoCraft's Triangle and - /// Polygon). `sides` in [`SIDES`]; `star_ratio` in [`STAR_RATIO`], 1 for a plain polygon. - /// The Triangle tool is 3 sides. + /// A triangle filling `rect`: the apex at the top centre, the base on the bottom edge (PhotoCraft's Triangle + /// tool and Create Triangle). + Triangle { + /// The triangle's bounds. + rect: Rect, + }, + /// A regular polygon or star inscribed in the ellipse of `rect` (the Box frame: PhotoCraft's Polygon). + /// `sides` in [`SIDES`]; `star_ratio` in [`STAR_RATIO`], 1 for a plain polygon. Polygon { /// The box whose inscribed ellipse holds the polygon. rect: Rect, @@ -93,9 +98,10 @@ impl ShapeGeometry { /// to half the shorter side, as kurbo's `RoundedRect` does); all radii 0 is the plain /// rectangle. /// - `Ellipse`: kurbo's `Ellipse::from_rect`. + /// - `Triangle`: the top centre, the bottom right and the bottom left of `rect`. /// - `Polygon`: the outline of [`RadialPolygon`](Self::RadialPolygon) with the first point /// straight up, its outer points on the ellipse inscribed in `rect` (PhotoCraft's engine: - /// a triangle's base lies a quarter of the height below the centre, not on the bottom + /// a 3-sided polygon's base lies a quarter of the height below the centre, not on the bottom /// edge); a star alternates outer and inner points, the inner ones on the ellipse /// scaled by `star_ratio`. /// - `RadialPolygon`: `sides` outer points on the circle of `radius`, the first at `rotation` @@ -105,7 +111,7 @@ impl ShapeGeometry { /// - `Custom`: the box itself (the app draws the preset). pub fn to_path(&self, tolerance: f64) -> BezPath { // pc: crates/ui-egui/src/tools/vector_ui/shapes.rs:73-79 (the kinds: `roundedRect` with four - // equal radii, ellipse, a polygon of 3 sides for the Triangle, `polygon_sides.max(3)`). + // equal radii, ellipse, `triangle` filling its rect, `polygon_sides.max(3)`). // new: PhotoCraft's engine draws the outline; here it is kurbo's `RoundedRect`, `Ellipse` // and a point list, so engines without one share it. let tolerance = tolerance.max(f64::EPSILON); @@ -119,6 +125,11 @@ impl ShapeGeometry { } } Self::Ellipse { rect } => Ellipse::from_rect(rect.abs()).to_path(tolerance), + Self::Triangle { rect } => { + // pc: #13350 (the triangle fills its box, apex on top). + let rect = rect.abs(); + closed(&[Point::new(rect.center().x, rect.y0), Point::new(rect.x1, rect.y1), Point::new(rect.x0, rect.y1)]) + } Self::Polygon { rect, sides, star_ratio } => { let rect = rect.abs(); let (c, rx, ry) = (rect.center(), rect.width() / 2.0, rect.height() / 2.0); @@ -145,7 +156,11 @@ impl ShapeGeometry { pub fn bounds(&self) -> Rect { // pc: crates/ui-egui/src/tools/vector_ui/shapes.rs:85-96 `shape_bounds` (a line's ends' box). match *self { - Self::Rect { rect, .. } | Self::Ellipse { rect } | Self::Polygon { rect, .. } | Self::Custom { rect, .. } => rect.abs(), + Self::Rect { rect, .. } + | Self::Ellipse { rect } + | Self::Triangle { rect } + | Self::Polygon { rect, .. } + | Self::Custom { rect, .. } => rect.abs(), Self::RadialPolygon { center, radius, .. } => Rect::new(center.x - radius, center.y - radius, center.x + radius, center.y + radius), Self::Line { from, to, .. } => Rect::from_points(from, to), } @@ -203,6 +218,16 @@ fn positive(pair: (f64, f64)) -> Option<(f64, f64)> { (pair.0.is_finite() && pair.1.is_finite() && pair.0 > 0.0 && pair.1 > 0.0).then_some(pair) } +/// The tight box of the equilateral triangle, apex up, inscribed in the circle of `square`: what ⇧ draws with the +/// Triangle tool (#13350). With the square centred on the press (⇧⌥), the triangle's centre is the press. +pub fn equilateral_in(square: Rect) -> Rect { + let square = square.abs(); + let r = square.width() / 2.0; + let half_base = r * 3f64.sqrt() / 2.0; + let x = square.center().x - half_base; + Rect::new(x, square.y0, x + 2.0 * half_base, square.y0 + 1.5 * r) +} + /// The end of a line dragged from `from` to `to`, or `None` when it is shorter than /// [`MIN_SIDE`]. pub fn line_drag(from: Point, to: Point) -> Option { diff --git a/crates/draw/src/ui/shape_drag/dialog.rs b/crates/draw/src/ui/shape_drag/dialog.rs index 554900769..2237176ed 100644 --- a/crates/draw/src/ui/shape_drag/dialog.rs +++ b/crates/draw/src/ui/shape_drag/dialog.rs @@ -209,7 +209,7 @@ fn answer(o: &CreateShapeOptions, words: &CreateShapeWords, units: &Units) -> Re ShapeGeometry::Rect { rect, radii } } ShapeKind::Ellipse => ShapeGeometry::Ellipse { rect }, - ShapeKind::Triangle => ShapeGeometry::Polygon { rect, sides: 3, star_ratio: 1.0 }, + ShapeKind::Triangle => ShapeGeometry::Triangle { rect }, ShapeKind::Polygon | ShapeKind::Star => { let ratio = if o.star_ratio.is_finite() { o.star_ratio.clamp(0.01, 1.0) } else { 1.0 }; ShapeGeometry::Polygon { rect, sides, star_ratio: ratio } diff --git a/crates/draw/src/ui/shape_drag/tool.rs b/crates/draw/src/ui/shape_drag/tool.rs index d676bb04d..13ae40ccf 100644 --- a/crates/draw/src/ui/shape_drag/tool.rs +++ b/crates/draw/src/ui/shape_drag/tool.rs @@ -13,7 +13,7 @@ use kurbo::{Point, Vec2}; use super::words::ShapeDragWords; use super::{BoxConstraint, ShapeDragEvent, ShapeDragOptions, ShapeDragRules, ShapeFrame, ShapeKind}; -use crate::geometry::shape::{BoxRule, MIN_SIDE, RadialFrame, SIDES, ShapeGeometry, box_drag, line_drag}; +use crate::geometry::shape::{BoxRule, MIN_SIDE, RadialFrame, SIDES, ShapeGeometry, box_drag, equilateral_in, line_drag}; /// The flattening tolerance of the outline being drawn, in screen points. const OUTLINE_TOLERANCE: f32 = 0.25; @@ -147,7 +147,8 @@ impl ShapeDragTool { /// The shape a drag from `start` to `at` with `mods` draws under the options and rules, or `None` /// when a side is under 1 px: [`box_drag`] for the box kinds under the options' constraints, ⇧ /// toggling Square and ⌥ From Center (Rectangle with four equal radii when - /// `corner_radius` > 0, Rounded Rectangle always with them, Triangle a 3-sided polygon, Polygon and + /// `corner_radius` > 0, Rounded Rectangle always with them, Triangle filling the box (⇧: the equilateral + /// triangle in the square's circle), Polygon and /// Star with the options' sides and ratio, Custom a `Custom` box keeping proportions with ⇧), /// [`line_drag`] for a Line (weight at least 1, ⇧ snapping the end by [`line_shift`]), and /// [`RadialFrame::drag`] in the radial frame. @@ -191,7 +192,7 @@ impl ShapeDragTool { ShapeKind::Rectangle => ShapeGeometry::Rect { rect, radii: [0.0; 4] }, ShapeKind::RoundedRectangle => ShapeGeometry::Rect { rect, radii }, ShapeKind::Ellipse => ShapeGeometry::Ellipse { rect }, - ShapeKind::Triangle => ShapeGeometry::Polygon { rect, sides: 3, star_ratio: 1.0 }, + ShapeKind::Triangle => ShapeGeometry::Triangle { rect: if rule.square { equilateral_in(rect) } else { rect } }, ShapeKind::Polygon => ShapeGeometry::Polygon { rect, sides: drag.sides, star_ratio: 1.0 }, ShapeKind::Star => ShapeGeometry::Polygon { rect, sides: drag.sides, star_ratio: o.star_ratio }, ShapeKind::Custom | ShapeKind::Line => ShapeGeometry::Custom { rect, keep_proportions: mods.shift }, diff --git a/crates/draw/tests/it/shape_drag.rs b/crates/draw/tests/it/shape_drag.rs index e90906931..8eedbf1b7 100644 --- a/crates/draw/tests/it/shape_drag.rs +++ b/crates/draw/tests/it/shape_drag.rs @@ -194,6 +194,37 @@ fn the_rectangle_gear_constrains_the_drag_and_shift_and_alt_turn_it_off() { assert_eq!(shapes.committed_box(), Some(Rect::new(100.0, 100.0, 160.0, 130.0)), "the largest 2 : 1 box inside the drag"); } +/// The corners of the triangle `shape` draws, from the apex clockwise. +fn corners(shape: Option) -> Vec { + let Some(shape @ ShapeGeometry::Triangle { .. }) = shape else { return Vec::new() }; + shape.to_path(0.1).elements().iter().filter_map(|el| el.end_point()).collect() +} + +/// A Triangle drag fills its box, apex on top; ⇧ draws the equilateral triangle in the circle of the shorter +/// side's square; ⇧⌥ centres it on the press. +#[trial] +fn a_triangle_fills_its_box_and_shift_makes_it_equilateral() { + let mut shapes = Shapes::new(ShapeDragOptions::new(ShapeKind::Triangle)); + let to = PRESS + Vec2::new(60.0, 40.0); + shapes.drag(PRESS, to, Modifiers::NONE); + let plain = corners(shapes.committed()); + assert_eq!(plain.first(), Some(&Point::new(130.0, 100.0)), "the apex is the box's top centre: {plain:?}"); + assert!(plain.len() >= 3 && plain[1..].iter().all(|p| p.y == 140.0), "the base lies on the bottom edge: {plain:?}"); + + shapes.drag(PRESS, to, Modifiers::SHIFT); + let equal = corners(shapes.committed()); + assert!(equal.len() >= 3, "a triangle: {equal:?}"); + let sides = [(equal[0] - equal[1]).hypot(), (equal[1] - equal[2]).hypot(), (equal[2] - equal[0]).hypot()]; + assert!(sides.iter().all(|side| (side - 20.0 * 3f64.sqrt()).abs() < 1e-9), "three equal sides in the 40 px square's circle: {sides:?}"); + assert_eq!(equal[0], Point::new(120.0, 100.0), "the apex is the top of the circle"); + + shapes.drag(PRESS, to, Modifiers::SHIFT | Modifiers::ALT); + let centred = corners(shapes.committed()); + assert!(centred.len() >= 3, "a triangle: {centred:?}"); + let centre = (centred[0].to_vec2() + centred[1].to_vec2() + centred[2].to_vec2()) / 3.0; + assert!((centre.to_point() - PRESS).hypot() < 1e-9, "⇧⌥ puts the centre on the press, got {centre:?}"); +} + #[trial] fn the_shape_tool_owes_the_canvas_what_every_tool_owes() { ToolFixture::with_view("Shape", tool(ShapeKind::Rectangle), room_view, |_, _| {}).check();