Geometry

Rotated rectangles from a point or a segment

Build rotated rectangles centred on a pin, anchored at any point of the rectangle, or along a segment. TypeScript with VisQuill.

import {
    VisQuill, Reactive, Values, Rectangles, Points,
    type RvgGroup, type Real, Pins, Circles, Vectors
} from "@visquill/visquill-gdk"
import './styles.css'

// ─── Layout ───────────────────────────────────────────────────────────────
const leftX   = 165
const midX    = 500
const rightX  = 835
const panelY  = 260

const trackHalfW = 60
const rowH       = 52
const sliderBaseY = 460  // y position of the top slider row

export function run(div: HTMLDivElement) {
    const rvg    = VisQuill.create(div, "demos-rect-construction-")
    const canvas = rvg.canvas

    canvas.text.labelAt(
        "Drag handles and sliders — rectangles follow the pin orientation",
        { x: 500, y: 25 },
        "instructions"
    )

    canvas.visuals.segment("divider", { x: 333, y: 50 }, { x: 333, y: 580 })
    canvas.visuals.segment("divider", { x: 666, y: 50 }, { x: 666, y: 580 })

    // ─── centeredRectangle ────────────────────────────────────────────
    {
        const pin    = canvas.handles.disk("pin-handle", [leftX, panelY])
        Pins.rotateBy(pin,Math.PI / 8,pin)
        const result = canvas.visuals.rectangle("rect")

        const width  = buildSlider(canvas,  "width",  40, 200, 120, leftX, sliderBaseY)
        const height = buildSlider(canvas,  "height", 20, 120,  60, leftX, sliderBaseY + rowH)

        Reactive.do([pin, width, height], () => {
            Rectangles.centeredRectangle(pin, width, height,  result)
        })

        canvas.text.labelAt("centeredRectangle", { x: leftX, y: 555 }, "panel-label")
    }

    // ─── anchoredRectangle ────────────────────────────────────────────
    // xRatio=0 anchors the left edge at the pin; xRatio=1 the right.
    // yRatio=0 anchors the bottom edge; yRatio=1 the top (for an
    // unrotated pin; both follow the pin's orientation).
    {
        const pin    = canvas.handles.disk("pin-handle", [midX, panelY])
        Pins.rotateBy(pin,-Math.PI / 8,pin)
        const result = canvas.visuals.rectangle("rect")
        const dot    = canvas.visuals.circle("anchor-dot")

        const width  = buildSlider(canvas,  "width",  40, 200, 120, midX, sliderBaseY)
        const height = buildSlider(canvas,  "height", 20, 120,  60, midX, sliderBaseY + rowH)
        const xRatio = buildSlider(canvas,  "xRatio",  0,   1, 0.5, midX, sliderBaseY + rowH * 2)
        const yRatio = buildSlider(canvas,  "yRatio",  0,   1, 0.5, midX, sliderBaseY + rowH * 3)

        Reactive.do([pin, width, height, xRatio, yRatio], () => {
            Rectangles.anchoredRectangle(pin, xRatio, yRatio, width, height,  result)
            Circles.circleAt(pin,5,dot)
        })

        canvas.text.labelAt("anchoredRectangle", { x: midX, y: 755 }, "panel-label")
    }

    // ─── alongSegment ─────────────────────────────────────────────────
    // Two-handle baseline; height slider; side toggle handle.
    {
        const p1     = canvas.handles.disk("endpoint", [rightX - 100, panelY + 60])
        const p2     = canvas.handles.disk("endpoint", [rightX + 100, panelY - 60])
        const result = canvas.visuals.rectangle("rect")
        canvas.visuals.segment("base-seg", p1, p2)

        const height = buildSlider(canvas,  "height", 10, 120, 50, rightX, sliderBaseY)

        // Side toggle: drag it to either side of the base segment.
        const toggle  = canvas.handles.disk("side-toggle", [rightX, panelY - 80])
        const sideLbl = canvas.text.label("side: left", "toggle-label")
        Reactive.do([toggle], () => {
            Points.moveUp(toggle, 16, sideLbl)
        })

        Reactive.do([p1, p2, height, toggle], () => {
            // Negative cross product: toggle lies left of p1 → p2 (on screen).
            const left  = Vectors.crossProduct(Vectors.direction(p1, p2), Vectors.direction(p1, toggle)).value < 0
            sideLbl.value = `side: ${left ? "left" : "right"}`
            Rectangles.alongSegment(p1, p2, height, left, result)
        })

        canvas.text.labelAt("alongSegment (baseline)", { x: rightX, y: 555 }, "panel-label")
    }
}

// ─── Slider ───────────────────────────────────────────────────────────────
function buildSlider(
    canvas:  RvgGroup,
    label:   string,
    min:     number,
    max:     number,
    initial: number,
    cx:      number,
    cy:      number
): Real {
    const t      = canvas.values.real((initial - min) / (max - min))
    const value  = canvas.values.real(initial)
    const track  = canvas.visuals.segment("slider-track")
    const handle = canvas.handles.disk("slider-handle")
    const lbl    = canvas.text.label(label, "slider-label")
    const valLbl = canvas.text.label("", "slider-value")

    Points.moveLeft( { x: cx, y: cy }, trackHalfW, track.at(0))
    Points.moveRight({ x: cx, y: cy }, trackHalfW, track.at(1))
    Points.moveUp(   { x: cx, y: cy }, 14, lbl)
    Points.moveDown( { x: cx, y: cy }, 18, valLbl)
    Points.placeOnSegment(track.at(0), track.at(1), t, handle)

    Reactive.do([handle], () => {
        Points.closestPointOnSegment(handle, track, handle)
        Points.relativePositionOnSegment(handle, track, t)
    })

    Reactive.do([t], () => {
        Values.lerp(min, max, t, value)
        valLbl.value = value.value.toFixed(1)
    })

    return value
}

Three ways to build a rotated rectangle: centred on a pin (Rectangles.centeredRectangle), with any point on the pin (Rectangles.anchoredRectangle), or standing on a segment (Rectangles.alongSegment).

How it works

A rectangle in the GDK can be rotated, unlike a box. The pin's orientation sets the rotation. The handles here are pins, so Pins.rotateBy can tilt them at the start:

const pin    = canvas.handles.disk("pin-handle", [leftX, panelY])
Pins.rotateBy(pin,Math.PI / 8,pin)
// …
Reactive.do([pin, width, height], () => {
    Rectangles.centeredRectangle(pin, width, height,  result)
})

Rectangles.anchoredRectangle puts the pin at a relative position inside the rectangle instead of the center: xRatio along the width, yRatio along the height. 0.5 and 0.5 give the same rectangle as centeredRectangle.

Rectangles.alongSegment takes its rotation from a segment and stands on it, to its left or right. The demo decides the side from the toggle handle with a cross product:

const left  = Vectors.crossProduct(Vectors.direction(p1, p2), Vectors.direction(p1, toggle)).value < 0
Rectangles.alongSegment(p1, p2, height, left, result)

API

  • Rectangles.centeredRectangle: a rectangle centered on a pin
  • Rectangles.anchoredRectangle: a rectangle with the pin at relative position (x, y) inside it
  • Rectangles.alongSegment: a rectangle standing on a segment
  • Pins.rotateBy: turns a pin
  • Vectors.crossProduct, Vectors.direction: which side of the segment the toggle is on

Notes

For an unrotated pin, yRatio 0 puts the pin on the bottom edge and 1 on the top edge.

See also

  • geometry/rectangles-02-accessors
  • geometry/boxes-01-basics

API used

Also used (22)