Skip to content

rounded_rect_path

The path of a rectangle with a radius per corner, as SVG path data. Each radius is clamped to half the smaller side, and a radius of 0 gives a square corner (no arc is written).

use helpers4::geometry::rounded_rect_path;

Cargo feature geometry (enabled by default). To compile only this module:

cargo add helpers4 --no-default-features --features geometry

or in Cargo.toml:

[dependencies]
helpers4 = { version = "0.0.11", default-features = false, features = ["geometry"] }
pub fn rounded_rect_path(rect: Rect, radii: [f64; 4]) -> String
ParameterTypeDescription
rectRectThe rectangle.
radii[f64; 4][top_left, top_right, bottom_right, bottom_left], as in CSS border-radius; pass [r; 4] for one radius. Negative radii count as 0.

String — The path data, closed.

use helpers4::geometry::{rounded_rect_path, Rect};

let square = Rect::new(0.0, 0.0, 10.0, 10.0);
assert_eq!(rounded_rect_path(square, [0.0; 4]), "M0 0 L10 0 L10 10 L0 10 L0 0 Z");
assert_eq!(
    rounded_rect_path(square, [5.0, 0.0, 0.0, 0.0]),
    "M5 0 L10 0 L10 10 L0 10 L0 5 A5 5 0 0 1 5 0 Z"
);

src/geometry/rounded_rect_path.rs