Tin

Examples · packages/geometry · 333 of 339

vec

Vectors in the plane.

examples/packages/geometry/vec.tintin run examples/packages/geometry/vec.tin
// Vectors in the plane.

/// A 2D vector, in meters.
type Vec = {
  /// Across, positive to the right.
  x: float,
  /// Up, positive away from the floor.
  y: float,
}

/// How a point relates to an arm's reach.
type Reach = Inside | Edge | Outside(float)

/// The length of `v`: `Vec { x: 3.0, y: 4.0 }.len()` is 5.0.
fn Vec.len(v: Vec): float => sqrt(v.x * v.x + v.y * v.y)

/// `v` and `w` added component by component.
fn Vec.plus(v: Vec, w: Vec): Vec => Vec { x: v.x + w.x, y: v.y + w.y }

/// Where `p` is for an arm that reaches `limit` meters. **Edge** means within 1 cm.
fn reach(p: Vec, limit: float): Reach {
  let d = p.len() - limit
  if abs(d) < 0.01 { Edge } else if d < 0.0 { Inside } else { Outside(d) }
}