Geometry

Extreme point in any direction

Find the point of a set that lies farthest in any direction with a custom comparator; rotate the direction with a slider. TypeScript with VisQuill.

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

// ─── Layout ───────────────────────────────────────────────────────────────
const cloudPoints: [number, number][] = [
    [320, 200], [480, 160], [600, 240], [660, 360],
    [580, 480], [420, 520], [270, 440], [200, 300],
    [380, 300], [520, 380], [440, 240], [310, 390],
]

// Centre of the direction indicator arrow.
const arrowCx = 820
const arrowCy = 320
const arrowLength = 60

const trackHalfW    = 80
const sliderBottomY = 60

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

    // ─── Instructions ─────────────────────────────────────────────────
    const instructions = canvas.text.label(
        "Drag the angle slider · The highlighted point is most extreme in that direction",
        "instructions"
    )
    Reactive.do([rvg.frame], () => {
        Points.moveDown(Segments.midPoint(Boxes.topSide(rvg.frame)), 25, instructions)
    })

    // ─── Divider ──────────────────────────────────────────────────────
    canvas.visuals.segment("divider",
        canvas.values.point([720, 60]),
        canvas.values.point([720, 580])
    )

    // ─── Point cloud ──────────────────────────────────────────────────
    const handles = cloudPoints.map(pos =>
        canvas.handles.disk("cloud-handle", pos)
    )

    // ─── Extreme marker & projection line ─────────────────────────────
    const marker      = canvas.visuals.circle("marker")
    const projLine    = canvas.visuals.segment("projection-line")
    const markerLabel = canvas.text.label("extreme", "marker-label")

    // ─── Direction arrow ──────────────────────────────────────────────
    // Shows the current direction vector used by extreme().
    const arrowTip  = canvas.visuals.circle("arrow-tip")
    const arrowShaft = canvas.visuals.segment("arrow-shaft")
    canvas.text.labelAt("direction", { x: arrowCx, y: arrowCy + arrowLength + 24 }, "arrow-label")

    // ─── Angle slider ─────────────────────────────────────────────────
    // Maps 0→1 to 0→2π, giving a full rotation.
    const angle = buildSlider(canvas, rvg, "angle (0 – 2π)", 0, Math.PI * 2, 0, sliderBottomY)

    // ─── Reactive update ──────────────────────────────────────────────
    Reactive.do([angle, ...handles], () => {
        const dir = Vectors.fromAngle(angle)

        // extreme() selects the point that maximises dot(p, direction).
        const result = Extremes.extreme(handles, (p, q) =>
            // negative when p is more extreme
            Vectors.dotProduct(q, dir).value - Vectors.dotProduct(p, dir).value
        )

        // Highlight the winning point.
        Circles.circleAt(result, 12, marker)
        Points.moveUp(result, 20, markerLabel)

        // Project a line from the winning point along the direction.
        Points.copy(result, projLine.at(0))
        Vectors.add(result, Vectors.scale(dir, 200), projLine.at(1))

        // Update the direction arrow.
        Points.copy({ x: arrowCx, y: arrowCy }, arrowShaft.at(0))
        Vectors.add({ x: arrowCx, y: arrowCy }, Vectors.scale(dir, arrowLength), arrowShaft.at(1))
        Circles.circleAt({ x: arrowCx, y: arrowCy}, 4, arrowTip)
    })

    canvas.text.labelAt("extreme(points, comparison)", { x: 370, y: 560 }, "panel-label")
}

// ─── Slider ───────────────────────────────────────────────────────────────
function buildSlider(
    canvas:  RvgGroup,
    rvg:     RvgGraphic,
    label:   string,
    min:     number,
    max:     number,
    initial: number,
    yOffset: 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")

    Reactive.do([rvg.frame], () => {
        const bottom = Points.midPoint(
            Boxes.bottomSide(rvg.frame).at(0),
            Boxes.bottomSide(rvg.frame).at(1)
        )
        const center = Points.moveUp(bottom, yOffset)
        Points.moveLeft( center, trackHalfW, track.at(0))
        Points.moveRight(center, trackHalfW, track.at(1))
        Points.moveUp(center, 14, lbl)
        Points.moveDown(center, 20, 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(2)} rad`
    })

    return value
}

The point that lies farthest in a direction set by a slider. Extremes.extreme takes a comparison function, here a dot product with the direction.

How it works

Extremes.extreme takes a comparison of two points. It returns a negative number when the first point counts as more extreme. Here the rule is the projection onto a direction, so the winner is the point furthest along it. The direction comes from the slider's angle:

Reactive.do([angle, ...handles], () => {
    const dir = Vectors.fromAngle(angle)

    const result = Extremes.extreme(handles, (p, q) =>
        // negative when p is more extreme
        Vectors.dotProduct(q, dir).value - Vectors.dotProduct(p, dir).value
    )
    // …
})

At angle 0 the direction points right, so the result is the same as Extremes.rightMost. The four fixed directions are special cases of this rule.

API

  • Extremes.extreme: the most extreme point under a comparison
  • Vectors.fromAngle: a unit direction from an angle in radians
  • Vectors.dotProduct: the dot product of two vectors
  • Vectors.scale, Vectors.add: draw the projection line and the direction arrow
  • Values.lerp: maps the slider position to an angle

See also

  • geometry/extremes-01-basic

API used

Also used (28)