Browse the docs

silk.geometry.rounded_rect.

Axis-aligned rectangles with a circular radius at each corner, over any number type.

When to use#

Use RoundedRect for the outline of a card, a button, a shadow, or a clip with rounded corners. Use silk.geometry.rect.Rect when every corner is square.

Details#

A rounded rectangle is a Copy struct with the public fields rect, a silk.geometry.rect.Rect, and radii, a silk.geometry.corners.Corners, both of number type T. Each corner is a quarter circle whose radius is the matching field of radii. Radii are stored as given; clampRadii fits them to the rectangle. &a == &b compares the rectangle and every radius when T: Equal.

Gotchas#

contains assumes the radii already fit: none is negative and none exceeds half the smaller side. Call clampRadii first when the radii come from user input or a style.

Examples#

Hit-test a rounded button#

silk
import silk.geometry.point { Point2 }
import silk.geometry.rect { Rect }
import silk.geometry.rounded_rect { RoundedRect }

pub fn main() -> i32 {
  let button = RoundedRect<f64>.uniform(Rect<f64>.new(0.0, 0.0, 120.0, 40.0), 10.0)
  if !button.contains(Point2<f64>.new(60.0, 20.0)) || button.contains(Point2<f64>.new(1.0, 1.0)) {
    return 1
  }
  return 0
}

Import as RoundedRect with import silk.geometry.rounded_rect { RoundedRect }.

Public declarations: 1.

RoundedRect#

silk
pub struct RoundedRect<T>

An axis-aligned rectangle of number type T with a circular radius at each corner.

When to use#

Use RoundedRect for rounded boxes such as cards, buttons, shadows, and clips. Use silk.geometry.rect.Rect when every corner is square.

Details#

The fields are public. Each corner is a quarter circle whose radius is the matching field of radii; a zero radius gives a square corner. Radii are stored as given, so they can be negative or larger than the rectangle until clampRadii fits them. &a == &b compares the rectangle and every radius when T: Equal.

Field rect#

silk
pub rect: Rect<T>

The bounds of the shape, with inclusive edges.

Field radii#

silk
pub radii: Corners<T>

The radius at each corner, with top at the minimum y edge and left at the minimum x edge.

Implementation Copy for RoundedRect<T>#

silk
impl<T: Copy> Copy for RoundedRect<T>

Implementation RoundedRect<T>#

silk
impl<T> RoundedRect<T>

Associated function new#

silk
pub fn new(rect: Rect<T>, radii: Corners<T>) -> RoundedRect<T>

Returns the rounded rectangle with bounds rect and corner radii radii, as given.

Implementation RoundedRect<T>#

silk
impl<T: Copy> RoundedRect<T>

Associated function uniform#

silk
pub fn uniform(rect: Rect<T>, radius: T) -> RoundedRect<T>

Returns the rounded rectangle with bounds rect and radius at all four corners.

Implementation RoundedRect<T>#

silk
impl<T: Copy + Add<T, T>> RoundedRect<T>

Method translate#

silk
pub fn translate(self: Self, offset: Vector2<T>) -> RoundedRect<T>

Returns the rounded rectangle moved by offset, with the same size and radii.

Implementation RoundedRect<T>#

silk
impl<T: Copy + Order + Zero + One + Add<T, T> + Subtract<T, T> + Divide<T, T>> RoundedRect<T>

Method clampRadii#

silk
pub fn clampRadii(self: Self) -> RoundedRect<T>

Returns the rounded rectangle with its radii fitted to its size: each radius is clamped between zero and half of the smaller of the width and the height.

Details#

Each corner is clamped on its own, as GPU quad shaders do, so a fitted corner keeps its radius even when another corner is reduced. A reversed or zero-area rectangle gets zero radii. The rectangle itself is unchanged.

Implementation RoundedRect<T>#

silk
impl<T: Copy + Order + Add<T, T> + Subtract<T, T> + Multiply<T, T>> RoundedRect<T>

Method contains#

silk
pub fn contains(self: &Self, point: Point2<T>) -> bool

Returns true when point is inside the shape or on its outline.

Details#

A point is inside when it is inside rect, edges included, and not outside the quarter circle of the corner it falls in. A point exactly on a corner arc is inside.

Gotchas#

The radii must already fit: none negative and none above half the smaller side. Call clampRadii first when they may not, or the corner regions overlap and the answer is not meaningful. For an integer type, the squared distances can overflow for coordinates near the type's limit.

Implementation Equal for RoundedRect<T>#

silk
impl<T: Equal> Equal for RoundedRect<T>

Operation equals#

silk
fn equals(left: &Self, right: &Self) -> bool

Category: Geometry.

Profiles: aarch64-apple-darwin, aarch64-unknown-linux-gnu, aarch64-unknown-linux-gnu-no-libc, x86_64-unknown-linux-gnu, x86_64-unknown-linux-gnu-no-libc, wasm32-unknown-unknown, wasm32-wasip1, wasm32-wasip2, x86_64-pc-windows-msvc, wasm64-unknown-unknown.

View the source of this page