Skip to Content
ReferenceLibrary

geometry

The geometry module.

Generated from spec/std/geometry.cp by cpl doc --format json.

Pt

pub struct Pt(x: Int, y: Int)

O(1) value type. A 2D point or vector with exact integer coordinates.

Source

Pt.norm2

fn norm2(self) -> Int

The squared length x*x + y*y, in Int.

Fits for |x|, |y| <= 2e9; use norm2_128 beyond.

  • cost: O(1)
  • traps: when the result overflows Int

Examples:

  • Pt(3, 4).norm2 == 25
  • Pt(0, 0).norm2 == 0

Source

Pt.norm2_128

fn norm2_128(self) -> I128

The exact squared length x*x + y*y, computed in I128.

  • cost: O(1)
  • traps: when the result overflows I128 (only for coordinates near Int.MIN)

Examples:

  • Pt(3, 4).norm2_128 == 25
  • Pt(3_000_000_000, 4_000_000_000).norm2_128 == 25_000_000_000_000_000_000

Source

Pt.len

fn len(self) -> F64

The Euclidean length sqrt(x*x + y*y) as F64 (hypot, so no intermediate overflow).

  • cost: O(1)
  • traps: never

Examples:

  • Pt(3, 4).len == 5.0
  • Pt(0, 0).len == 0.0

Source

Pt.perp

fn perp(self) -> Pt

self rotated 90 degrees counter-clockwise: Pt(-y, x).

  • cost: O(1)
  • traps: when y is Int.MIN

Examples:

  • Pt(1, 2).perp == Pt(-2, 1)

Source

Pt.perp_cw

fn perp_cw(self) -> Pt

self rotated 90 degrees clockwise: Pt(y, -x).

  • cost: O(1)
  • traps: when x is Int.MIN

Examples:

  • Pt(1, 2).perp_cw == Pt(2, -1)

Source

Pt.angle

fn angle(self) -> F64

The polar angle atan2(y, x) in (-pi, pi]; 0 for the zero vector.

For exact angular comparisons use arg or angle_less instead.

  • cost: O(1)
  • traps: never

Examples:

  • Pt(1, 0).angle == 0.0
  • Pt(0, -1).angle < 0.0
  • Pt(-1, 0).angle > 3.14
  • Pt(0, 0).angle == 0.0

Source

Pt.unit_dir

fn unit_dir(self) -> Pt

The reduced integer direction (x / g, y / g) with g = gcd(|x|, |y|); Pt(0, 0) for the zero vector.

Two nonzero vectors point the same way exactly when their unit_dirs are equal, so it is a hashable direction key.

  • cost: O(log max(|x|, |y|))
  • traps: never

Examples:

  • Pt(4, -6).unit_dir() == Pt(2, -3)
  • Pt(0, 5).unit_dir() == Pt(0, 1)
  • Pt(0, 0).unit_dir() == Pt(0, 0)

Source

Pt.to_vec

fn to_vec(self) -> Vec

self as a floating-point Vec(F64(x), F64(y)): the explicit way across.

  • cost: O(1)
  • traps: never

Examples:

  • Pt(1, 2).to_vec == Vec(1.0, 2.0)

Source

Pt.dot

fn dot(self, o: Pt) -> Int

The dot product x * o.x + y * o.y, in Int.

  • cost: O(1)
  • traps: when the result or a product overflows Int

Examples:

  • Pt(1, 2).dot(Pt(3, 4)) == 11
  • Pt(1, 0).dot(Pt(0, 1)) == 0

Source

Pt.dot128

fn dot128(self, o: Pt) -> I128

The exact dot product x * o.x + y * o.y, computed in I128.

  • cost: O(1)
  • traps: when the sum overflows I128 (only when every coordinate is near Int.MIN)

Examples:

  • Pt(1, 2).dot128(Pt(3, 4)) == 11

Source

Pt.cross

fn cross(self, o: Pt) -> Int

The 2D cross product x * o.y - y * o.x, in Int: positive when o is counter-clockwise from self, negative when clockwise, 0 when parallel.

  • cost: O(1)
  • traps: when the result or a product overflows Int

Examples:

  • Pt(1, 0).cross(Pt(0, 1)) == 1
  • Pt(1, 2).cross(Pt(3, 4)) == -2
  • Pt(1, 2).cross(Pt(2, 4)) == 0

Source

Pt.cross128

fn cross128(self, o: Pt) -> I128

The exact 2D cross product x * o.y - y * o.x, computed in I128.

  • cost: O(1)
  • traps: when the difference overflows I128 (only for coordinates near Int.MIN)

Examples:

  • Pt(1, 2).cross128(Pt(3, 4)) == -2

Source

Pt.dist2

fn dist2(self, o: Pt) -> Int

The squared Euclidean distance from self to o, in Int.

  • cost: O(1)
  • traps: when the result overflows Int

Examples:

  • Pt(0, 0).dist2(Pt(3, 4)) == 25
  • Pt(1, 1).dist2(Pt(1, 1)) == 0

Source

Pt.dist2_128

fn dist2_128(self, o: Pt) -> I128

The exact squared Euclidean distance from self to o, computed in I128.

  • cost: O(1)
  • traps: when the result overflows I128 (coordinates beyond about 4e18 apart)

Examples:

  • Pt(0, 0).dist2_128(Pt(3, 4)) == 25

Source

Pt.dist

fn dist(self, o: Pt) -> F64

The Euclidean distance from self to o, as F64.

  • cost: O(1)
  • traps: never

Examples:

  • Pt(0, 0).dist(Pt(3, 4)) == 5.0

Source

Pt.manhattan

fn manhattan(self, o: Pt) -> Int

The Manhattan (L1) distance |x - o.x| + |y - o.y|.

  • cost: O(1)
  • traps: when the result overflows Int

Examples:

  • Pt(1, 1).manhattan(Pt(4, -3)) == 7

Source

Pt.chebyshev

fn chebyshev(self, o: Pt) -> Int

The Chebyshev (L-infinity) distance max(|x - o.x|, |y - o.y|).

  • cost: O(1)
  • traps: when the result overflows Int

Examples:

  • Pt(1, 1).chebyshev(Pt(4, -3)) == 4

Source

Pt

impl Add for Pt

Source

Pt.add

fn add(a, b)

Source

Pt

impl Sub for Pt

Source

Pt.sub

fn sub(a, b)

Source

Pt

impl Neg for Pt

Source

Pt.neg

fn neg(a)

Source

Pt

impl Mul<Int> for Pt

Source

Pt.mul

fn mul(a, k)

Source

Pt

impl Show for Pt

Source

Pt.show

fn show(a)

Source

ByAngle

pub struct ByAngle(p

O(1) comparison key. Counter-clockwise by direction, starting from the positive x axis. Exact in I128; vectors in the same direction compare equal, zero comes first.

Source

ByAngle

impl Ord for ByAngle

Source

ByAngle.lt

fn lt(a, b)

Source

ByAngleFrom

pub struct ByAngleFrom(base: Pt, p

O(1) comparison key. Counter-clockwise by direction starting from ray base. Exact in I128; vectors in the same direction compare equal, zero comes first.

Source

ByAngleFrom

impl Ord for ByAngleFrom

Source

ByAngleFrom.lt

fn lt(a, b)

Source

OnlineHull

pub struct OnlineHull

O(n) memory. Incremental 2D convex hull of integer points backed by upper and lower chains.

Source

OnlineHull.new

fn new() -> OnlineHull

An empty online convex hull.

The hull keeps strict corners only: points on an edge or inside are absorbed.

  • cost: O(1)
  • traps: never

Examples:

  • OnlineHull().is_empty == true

Source

OnlineHull.add

fn add(mut self, p: Pt) -> Bool

Insert p and report whether the hull changed (whether p is now a corner).

A point inside, on the boundary, or equal to a corner returns false and changes nothing.

  • cost: O(log n) amortized
  • traps: never

Examples:

  • var h = OnlineHull(); h.add(Pt(0, 0)) == true
  • var h = OnlineHull(); h.add(Pt(0, 0)); h.add(Pt(4, 0)); h.add(Pt(0, 4)); h.add(Pt(1, 1)) == false
  • var h = OnlineHull(); h.add(Pt(0, 0)); h.add(Pt(0, 0)) == false

Source

OnlineHull.locate

fn locate(self, p: Pt) -> Location

Where p is relative to the current hull: Inside, Boundary or Outside.

An empty hull has everything Outside; a one-point or segment hull has only its points on the Boundary.

  • cost: O(log n)
  • traps: never

Examples:

  • var h = OnlineHull(); h.add(Pt(0, 0)); h.add(Pt(4, 0)); h.add(Pt(0, 4)); h.locate(Pt(1, 1)) == Location.Inside
  • var h = OnlineHull(); h.add(Pt(0, 0)); h.add(Pt(4, 0)); h.add(Pt(0, 4)); h.locate(Pt(2, 0)) == Location.Boundary
  • OnlineHull().locate(Pt(0, 0)) == Location.Outside

Source

OnlineHull.extreme

fn extreme(self, dir: Pt) -> ?Pt

The hull corner maximizing v.dot128(dir), or None when the hull is empty.

Ties go to the corner that comes first in vertices() order.

  • cost: O(log n)
  • ties: the first in vertices() order
  • traps: never

Examples:

  • var h = OnlineHull(); h.add(Pt(0, 0)); h.add(Pt(4, 0)); h.add(Pt(0, 4)); h.extreme(Pt(1, 1)) == Some(Pt(4, 0))
  • var h = OnlineHull(); h.add(Pt(0, 0)); h.add(Pt(4, 0)); h.add(Pt(0, 4)); h.extreme(Pt(0, 1)) == Some(Pt(0, 4))
  • OnlineHull().extreme(Pt(1, 0)) == None

Source

OnlineHull.area2

fn area2(self) -> Int

Twice the area of the current hull; 0 with fewer than 3 non-collinear points.

  • cost: O(1)
  • traps: when the value overflows Int

Examples:

  • var h = OnlineHull(); h.add(Pt(0, 0)); h.add(Pt(4, 0)); h.add(Pt(0, 4)); h.area2 == 16
  • OnlineHull().area2 == 0

Source

OnlineHull.len

fn len(self) -> Int

The number of hull corners.

  • cost: O(1)
  • traps: never

Examples:

  • var h = OnlineHull(); h.add(Pt(0, 0)); h.add(Pt(4, 0)); h.add(Pt(0, 4)); h.add(Pt(1, 1)); h.len == 3

Source

OnlineHull.is_empty

fn is_empty(self) -> Bool

Whether no point has been added since creation or the last clear.

  • cost: O(1)
  • traps: never

Examples:

  • OnlineHull().is_empty == true
  • var h = OnlineHull(); h.add(Pt(1, 1)); h.is_empty == false

Source

OnlineHull.vertices

fn vertices(self) -> List<Pt>

The current hull corners, counter-clockwise from the least (x, y).

  • cost: O(n) time and memory
  • order: counter-clockwise, starting at the least (x, y); a new List, keyed from 0
  • traps: never

Examples:

  • var h = OnlineHull(); h.add(Pt(0, 4)); h.add(Pt(4, 0)); h.add(Pt(0, 0)); h.vertices() == [Pt(0, 0), Pt(4, 0), Pt(0, 4)]

Source

OnlineHull.iter

fn iter(self) -> yields Pt

The current hull corners, yielded in vertices() order without building a list.

  • cost: O(n)
  • order: counter-clockwise, starting at the least (x, y)
  • traps: never

Examples:

  • var h = OnlineHull(); h.add(Pt(0, 4)); h.add(Pt(4, 0)); h.add(Pt(0, 0)); h.iter().collect() == [Pt(0, 0), Pt(4, 0), Pt(0, 4)]

Source

OnlineHull.clear

fn clear(mut self)

Remove every point; the hull is then empty.

  • cost: O(n)
  • traps: never

Examples:

  • var h = OnlineHull(); h.add(Pt(1, 1)); h.clear(); h.is_empty

Source

Location

pub enum Location = Inside | Boundary | Outside

O(1) enum. Position of a point relative to a polygon or disk.

Source

Location.Inside

Inside

Source

Location.Boundary

Boundary

Source

Location.Outside

Outside

Source

PI

pub const PI: F64 = 3.14159265358979323846

O(1). Circle constant pi (canonical home: std.math.PI; re-exported here).

Source

Vec

pub struct Vec(x: F64, y: F64)

O(1) value type. A 2D floating-point point or vector. Kept distinct from Pt so exact integer predicates never silently mix with approximate arithmetic.

Source

Vec.norm2

fn norm2(self) -> F64

The squared length x*x + y*y.

  • cost: O(1)
  • traps: never

Examples:

  • Vec(3.0, 4.0).norm2 == 25.0

Source

Vec.len

fn len(self) -> F64

The Euclidean length (hypot(x, y), so no intermediate overflow).

  • cost: O(1)
  • traps: never

Examples:

  • Vec(3.0, 4.0).len == 5.0

Source

Vec.perp

fn perp(self) -> Vec

self rotated 90 degrees counter-clockwise: Vec(-y, x).

  • cost: O(1)
  • traps: never

Examples:

  • Vec(1.0, 2.0).perp == Vec(-2.0, 1.0)

Source

Vec.perp_cw

fn perp_cw(self) -> Vec

self rotated 90 degrees clockwise: Vec(y, -x).

  • cost: O(1)
  • traps: never

Examples:

  • Vec(1.0, 2.0).perp_cw == Vec(2.0, -1.0)

Source

Vec.angle

fn angle(self) -> F64

The polar angle atan2(y, x) in (-pi, pi]; a -0.0 y counts as 0.0, so the negative x axis gives PI, and the zero vector gives 0.0.

  • cost: O(1)
  • traps: never

Examples:

  • Vec(1.0, 0.0).angle == 0.0
  • Vec(-1.0, 0.0).angle == PI
  • Vec(0.0, 1.0).angle == PI / 2.0

Source

Vec.from_angle

fn from_angle(rad: F64) -> Vec

The unit vector Vec(cos(rad), sin(rad)).

  • cost: O(1)
  • traps: never

Examples:

  • Vec.from_angle(0.0) == Vec(1.0, 0.0)
  • close(Vec.from_angle(PI / 2.0).y, 1.0)

Source

Vec.unit

fn unit(self) -> Vec

The unit vector in the direction of self: each coordinate divided by self.len.

  • cost: O(1)
  • traps: when self.len == 0.0

Examples:

  • Vec(3.0, 4.0).unit == Vec(0.6, 0.8)

Source

Vec.rot

fn rot(self, by: Vec) -> Vec

self rotated by the direction by, as the complex product self * by: Vec(x * by.x - y * by.y, x * by.y + y * by.x). A unit by rotates without scaling; get one from Vec.from_angle.

  • cost: O(1)
  • traps: never

Examples:

  • Vec(1.0, 2.0).rot(Vec(0.0, 1.0)) == Vec(-2.0, 1.0)

Source

Vec.dot

fn dot(self, o: Vec) -> F64

The dot product x * o.x + y * o.y.

  • cost: O(1)
  • traps: never

Examples:

  • Vec(1.0, 2.0).dot(Vec(3.0, 4.0)) == 11.0

Source

Vec.cross

fn cross(self, o: Vec) -> F64

The 2D cross product x * o.y - y * o.x: positive when o is counter-clockwise from self.

  • cost: O(1)
  • traps: never

Examples:

  • Vec(1.0, 2.0).cross(Vec(3.0, 4.0)) == -2.0

Source

Vec.dist2

fn dist2(self, o: Vec) -> F64

The squared Euclidean distance from self to o.

  • cost: O(1)
  • traps: never

Examples:

  • Vec(0.0, 0.0).dist2(Vec(3.0, 4.0)) == 25.0

Source

Vec.dist

fn dist(self, o: Vec) -> F64

The Euclidean distance from self to o.

  • cost: O(1)
  • traps: never

Examples:

  • Vec(0.0, 0.0).dist(Vec(3.0, 4.0)) == 5.0

Source

Vec

impl Add for Vec

Source

Vec.add

fn add(a, b)

Source

Vec

impl Sub for Vec

Source

Vec.sub

fn sub(a, b)

Source

Vec

impl Neg for Vec

Source

Vec.neg

fn neg(a)

Source

Vec

impl Mul<F64> for Vec

Source

Vec.mul

fn mul(a, k)

Source

Vec

impl Div<F64> for Vec

Source

Vec.div

fn div(a, k)

Source

Circle

pub struct Circle(c: Vec, r: F64)

O(1) value type. A 2D circle with center c and non-negative radius r.

Source

Circle.through

fn through(a: Vec, b: Vec, c: Vec, eps: F64 = 1e-9) -> ?Circle

The circle through a, b and c: centre circumcenter(a, b, c, eps), radius its distance to a; None when collinear.

  • cost: O(1)
  • traps: never

Examples:

  • Circle.through(Vec(0.0, 0.0), Vec(6.0, 0.0), Vec(0.0, 8.0)) == Some(Circle(Vec(3.0, 4.0), 5.0))
  • Circle.through(Vec(0.0, 0.0), Vec(1.0, 1.0), Vec(2.0, 2.0)) == None

Source

Circle.area

fn area(self) -> F64

The disk area, computed as PI * r * r.

  • cost: O(1)
  • traps: never

Examples:

  • Circle(Vec(0.0, 0.0), 1.0).area == PI

Source

Circle.circumference

fn circumference(self) -> F64

The circumference, computed as 2.0 * PI * r.

  • cost: O(1)
  • traps: never

Examples:

  • Circle(Vec(0.0, 0.0), 0.5).circumference == PI

Source

Circle.locate

fn locate(self, p: Vec, eps: F64 = 1e-9) -> Location

Where p is relative to the disk, with d = c.dist(p): Boundary when |d - r| <= eps, else Inside when d < r, else Outside.

  • cost: O(1)
  • traps: never

Examples:

  • Circle(Vec(0.0, 0.0), 5.0).locate(Vec(3.0, 4.0)) == Location.Boundary
  • Circle(Vec(0.0, 0.0), 5.0).locate(Vec(1.0, 1.0)) == Location.Inside
  • Circle(Vec(0.0, 0.0), 5.0).locate(Vec(6.0, 0.0)) == Location.Outside

Source

HalfPlane

pub struct HalfPlane(p: Vec, dir: Vec)

O(1) value type. Directed line through p in direction dir, keeping the closed left half-plane { q | dir.cross(q - p) >= 0 }.

Source

HalfPlane.through

fn through(a: Vec, b: Vec) -> HalfPlane

The closed half-plane left of the directed line a -> b: HalfPlane(a, b - a).

  • cost: O(1)
  • traps: when a == b

Examples:

  • HalfPlane.through(Vec(0.0, 0.0), Vec(1.0, 0.0)) == HalfPlane(Vec(0.0, 0.0), Vec(1.0, 0.0))

Source

HalfPlane.from_line

fn from_line(a: F64, b: F64, c: F64) -> HalfPlane

The half-plane { (x, y) | a*x + b*y + c >= 0 }, as HalfPlane(p, Vec(b, -a)) with p the line's point nearest the origin, Vec(-a * c, -b * c) / (a*a + b*b).

  • cost: O(1)
  • traps: when a == 0.0 and b == 0.0

Examples:

  • HalfPlane.from_line(0.0, 1.0, -2.0) == HalfPlane(Vec(0.0, 2.0), Vec(1.0, 0.0))
  • HalfPlane.from_line(1.0, 0.0, 0.0).side(Vec(3.0, 7.0)) == 3.0

Source

HalfPlane.side

fn side(self, q: Vec) -> F64

The signed value dir.cross(q - p): positive left of the line (inside), 0 on it, negative outside. It is |dir| times the signed distance.

  • cost: O(1)
  • traps: never

Examples:

  • HalfPlane.through(Vec(0.0, 0.0), Vec(1.0, 0.0)).side(Vec(3.0, 2.0)) == 2.0
  • HalfPlane.through(Vec(0.0, 0.0), Vec(1.0, 0.0)).side(Vec(3.0, -2.0)) == -2.0

Source

Rect

pub struct Rect(x: Range<Int>, y: Range<Int>)

O(1) value type. Half-open integer rectangle x * y (accepts cut ranges per decision 124).

Source

Rect.area

fn area(self) -> Int

The number of unit cells, x.len * y.len; 0 when either range is empty.

  • cost: O(1)
  • traps: when the product overflows Int

Examples:

  • Rect(0..3, 0..2).area == 6
  • Rect(3..1, 0..2).area == 0

Source

Rect.is_empty

fn is_empty(self) -> Bool

Whether either axis range is empty.

  • cost: O(1)
  • traps: never

Examples:

  • Rect(0..0, 0..5).is_empty == true
  • Rect(0..1, 0..1).is_empty == false

Source

Rect.contains

fn contains(self, p: Pt) -> Bool

Whether p.x is in x and p.y is in y (half-open, so the top and right edges are out).

  • cost: O(1)
  • traps: never

Examples:

  • Rect(0..3, 0..2).contains(Pt(2, 1)) == true
  • Rect(0..3, 0..2).contains(Pt(3, 1)) == false

Source

Rect.intersect

fn intersect(self, o: Rect) -> ?Rect

The common part of two rectangles, or None when it is empty (including rectangles that only touch along an edge).

  • cost: O(1)
  • traps: never

Examples:

  • Rect(0..3, 0..3).intersect(Rect(1..5, 2..4)) == Some(Rect(1..3, 2..3))
  • Rect(0..1, 0..1).intersect(Rect(1..2, 0..1)) == None

Source

Pt3

pub struct Pt3(x: Int, y: Int, z: Int)

O(1) value type. A 3D point or vector with exact integer coordinates.

Source

Pt3.norm2

fn norm2(self) -> Int

The squared length x*x + y*y + z*z, in Int.

  • cost: O(1)
  • traps: when the result overflows Int

Examples:

  • Pt3(1, 2, 2).norm2 == 9

Source

Pt3.norm2_128

fn norm2_128(self) -> I128

The exact squared length, computed in I128.

  • cost: O(1)
  • traps: when the result overflows I128

Examples:

  • Pt3(1, 2, 2).norm2_128 == 9

Source

Pt3.len

fn len(self) -> F64

The Euclidean length as F64.

  • cost: O(1)
  • traps: never

Examples:

  • Pt3(1, 2, 2).len == 3.0

Source

Pt3.to_vec3

fn to_vec3(self) -> Vec3

self as a floating-point Vec3: the explicit way across.

  • cost: O(1)
  • traps: never

Examples:

  • Pt3(1, 2, 2).to_vec3 == Vec3(1.0, 2.0, 2.0)

Source

Pt3.dot

fn dot(self, o: Pt3) -> Int

The dot product x * o.x + y * o.y + z * o.z, in Int.

  • cost: O(1)
  • traps: when the result or a product overflows Int

Examples:

  • Pt3(1, 2, 3).dot(Pt3(4, 5, 6)) == 32

Source

Pt3.dot128

fn dot128(self, o: Pt3) -> I128

The exact dot product, computed in I128.

  • cost: O(1)
  • traps: when the sum overflows I128

Examples:

  • Pt3(1, 2, 3).dot128(Pt3(4, 5, 6)) == 32

Source

Pt3.cross

fn cross(self, o: Pt3) -> Pt3

The cross product Pt3(y*o.z - z*o.y, z*o.x - x*o.z, x*o.y - y*o.x), perpendicular to both by the right-hand rule.

  • cost: O(1)
  • traps: when a component overflows Int

Examples:

  • Pt3(1, 0, 0).cross(Pt3(0, 1, 0)) == Pt3(0, 0, 1)
  • Pt3(1, 2, 3).cross(Pt3(4, 5, 6)) == Pt3(-3, 6, -3)

Source

Pt3.dist2

fn dist2(self, o: Pt3) -> Int

The squared Euclidean distance from self to o, in Int.

  • cost: O(1)
  • traps: when the result overflows Int

Examples:

  • Pt3(0, 0, 0).dist2(Pt3(1, 2, 2)) == 9

Source

Pt3.dist

fn dist(self, o: Pt3) -> F64

The Euclidean distance from self to o, as F64.

  • cost: O(1)
  • traps: never

Examples:

  • Pt3(0, 0, 0).dist(Pt3(1, 2, 2)) == 3.0

Source

Pt3.manhattan

fn manhattan(self, o: Pt3) -> Int

The Manhattan (L1) distance |x - o.x| + |y - o.y| + |z - o.z|.

  • cost: O(1)
  • traps: when the result overflows Int

Examples:

  • Pt3(1, 1, 1).manhattan(Pt3(2, -1, 4)) == 6

Source

Pt3.chebyshev

fn chebyshev(self, o: Pt3) -> Int

The Chebyshev (L-infinity) distance max(|x - o.x|, |y - o.y|, |z - o.z|).

  • cost: O(1)
  • traps: when the result overflows Int

Examples:

  • Pt3(1, 1, 1).chebyshev(Pt3(2, -1, 4)) == 3

Source

Pt3

impl Add for Pt3

Source

Pt3.add

fn add(a, b)

Source

Pt3

impl Sub for Pt3

Source

Pt3.sub

fn sub(a, b)

Source

Pt3

impl Neg for Pt3

Source

Pt3.neg

fn neg(a)

Source

Pt3

impl Mul<Int> for Pt3

Source

Pt3.mul

fn mul(a, k)

Source

Pt3

impl Show for Pt3

Source

Pt3.show

fn show(a)

Source

Vec3

pub struct Vec3(x: F64, y: F64, z: F64)

O(1) value type. A 3D floating-point point or vector.

Source

Vec3.norm2

fn norm2(self) -> F64

The squared length x*x + y*y + z*z.

  • cost: O(1)
  • traps: never

Examples:

  • Vec3(1.0, 2.0, 2.0).norm2 == 9.0

Source

Vec3.len

fn len(self) -> F64

The Euclidean length.

  • cost: O(1)
  • traps: never

Examples:

  • Vec3(1.0, 2.0, 2.0).len == 3.0

Source

Vec3.unit

fn unit(self) -> Vec3

The unit vector in the direction of self: each coordinate divided by self.len.

  • cost: O(1)
  • traps: when self.len == 0.0

Examples:

  • Vec3(0.0, 0.0, 2.0).unit == Vec3(0.0, 0.0, 1.0)

Source

Vec3.dot

fn dot(self, o: Vec3) -> F64

The dot product x * o.x + y * o.y + z * o.z.

  • cost: O(1)
  • traps: never

Examples:

  • Vec3(1.0, 2.0, 3.0).dot(Vec3(4.0, 5.0, 6.0)) == 32.0

Source

Vec3.cross

fn cross(self, o: Vec3) -> Vec3

The cross product, by the right-hand rule, as for Pt3.cross.

  • cost: O(1)
  • traps: never

Examples:

  • Vec3(1.0, 0.0, 0.0).cross(Vec3(0.0, 1.0, 0.0)) == Vec3(0.0, 0.0, 1.0)

Source

Vec3.dist2

fn dist2(self, o: Vec3) -> F64

The squared Euclidean distance from self to o.

  • cost: O(1)
  • traps: never

Examples:

  • Vec3(0.0, 0.0, 0.0).dist2(Vec3(1.0, 2.0, 2.0)) == 9.0

Source

Vec3.dist

fn dist(self, o: Vec3) -> F64

The Euclidean distance from self to o.

  • cost: O(1)
  • traps: never

Examples:

  • Vec3(0.0, 0.0, 0.0).dist(Vec3(1.0, 2.0, 2.0)) == 3.0

Source

Vec3.rot

fn rot(self, axis: Vec3, rad: F64) -> Vec3

self rotated by rad radians counter-clockwise about the unit vector axis (seen with axis pointing at the viewer), by Rodrigues' formula v cos t + axis.cross(v) sin t + axis * axis.dot(v) * (1 - cos t).

axis must be a unit vector; that is not checked.

  • cost: O(1)
  • traps: never

Examples:

  • Vec3(1.0, 2.0, 3.0).rot(Vec3(0.0, 0.0, 1.0), 0.0) == Vec3(1.0, 2.0, 3.0)
  • let v = Vec3(1.0, 0.0, 0.0).rot(Vec3(0.0, 0.0, 1.0), PI / 2.0); close(v.x, 0.0) and close(v.y, 1.0) and close(v.z, 0.0)

Source

Vec3

impl Add for Vec3

Source

Vec3.add

fn add(a, b)

Source

Vec3

impl Sub for Vec3

Source

Vec3.sub

fn sub(a, b)

Source

Vec3

impl Neg for Vec3

Source

Vec3.neg

fn neg(a)

Source

Vec3

impl Mul<F64> for Vec3

Source

Vec3.mul

fn mul(a, k)

Source

Vec3

impl Div<F64> for Vec3

Source

Vec3.div

fn div(a, k)

Source

Plane3

pub struct Plane3(n: Vec3, d: F64)

O(1) value type. A 3D plane { p | n.dot(p) == d } with unit normal n.

Source

Plane3.through

fn through(a: Vec3, b: Vec3, c: Vec3, eps: F64 = 1e-9) -> ?Plane3

The plane through a, b and c: n = (b - a).cross(c - a).unit, d = n.dot(a); None when (b - a).cross(c - a).len <= eps (collinear).

The normal follows the right-hand rule, so a -> b -> c runs counter-clockwise seen from the side n points to.

  • cost: O(1)
  • traps: never

Examples:

  • Plane3.through(Vec3(0.0, 0.0, 1.0), Vec3(1.0, 0.0, 1.0), Vec3(0.0, 1.0, 1.0)) == Some(Plane3(Vec3(0.0, 0.0, 1.0), 1.0))
  • Plane3.through(Vec3(0.0, 0.0, 0.0), Vec3(1.0, 1.0, 1.0), Vec3(2.0, 2.0, 2.0)) == None

Source

Plane3.dist

fn dist(self, p: Vec3) -> F64

The signed distance n.dot(p) - d: positive on the side n points to.

  • cost: O(1)
  • traps: never

Examples:

  • Plane3(Vec3(0.0, 0.0, 1.0), 1.0).dist(Vec3(5.0, 5.0, 3.0)) == 2.0
  • Plane3(Vec3(0.0, 0.0, 1.0), 1.0).dist(Vec3(5.0, 5.0, 0.0)) == -1.0

Source

Plane3.project

fn project(self, p: Vec3) -> Vec3

The orthogonal projection of p onto the plane: p - n * self.dist(p).

  • cost: O(1)
  • traps: never

Examples:

  • Plane3(Vec3(0.0, 0.0, 1.0), 1.0).project(Vec3(5.0, 5.0, 3.0)) == Vec3(5.0, 5.0, 1.0)

Source

On this page