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.
Pt.norm2
fn norm2(self) -> IntThe 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 == 25Pt(0, 0).norm2 == 0
Pt.norm2_128
fn norm2_128(self) -> I128The exact squared length x*x + y*y, computed in I128.
- cost: O(1)
- traps: when the result overflows
I128(only for coordinates nearInt.MIN)
Examples:
Pt(3, 4).norm2_128 == 25Pt(3_000_000_000, 4_000_000_000).norm2_128 == 25_000_000_000_000_000_000
Pt.len
fn len(self) -> F64The 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.0Pt(0, 0).len == 0.0
Pt.perp
fn perp(self) -> Ptself rotated 90 degrees counter-clockwise: Pt(-y, x).
- cost: O(1)
- traps: when
yisInt.MIN
Examples:
Pt(1, 2).perp == Pt(-2, 1)
Pt.perp_cw
fn perp_cw(self) -> Ptself rotated 90 degrees clockwise: Pt(y, -x).
- cost: O(1)
- traps: when
xisInt.MIN
Examples:
Pt(1, 2).perp_cw == Pt(2, -1)
Pt.angle
fn angle(self) -> F64The 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.0Pt(0, -1).angle < 0.0Pt(-1, 0).angle > 3.14Pt(0, 0).angle == 0.0
Pt.unit_dir
fn unit_dir(self) -> PtThe 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)
Pt.to_vec
fn to_vec(self) -> Vecself 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)
Pt.dot
fn dot(self, o: Pt) -> IntThe 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)) == 11Pt(1, 0).dot(Pt(0, 1)) == 0
Pt.dot128
fn dot128(self, o: Pt) -> I128The 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 nearInt.MIN)
Examples:
Pt(1, 2).dot128(Pt(3, 4)) == 11
Pt.cross
fn cross(self, o: Pt) -> IntThe 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)) == 1Pt(1, 2).cross(Pt(3, 4)) == -2Pt(1, 2).cross(Pt(2, 4)) == 0
Pt.cross128
fn cross128(self, o: Pt) -> I128The 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 nearInt.MIN)
Examples:
Pt(1, 2).cross128(Pt(3, 4)) == -2
Pt.dist2
fn dist2(self, o: Pt) -> IntThe 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)) == 25Pt(1, 1).dist2(Pt(1, 1)) == 0
Pt.dist2_128
fn dist2_128(self, o: Pt) -> I128The exact squared Euclidean distance from self to o, computed in I128.
- cost: O(1)
- traps: when the result overflows
I128(coordinates beyond about4e18apart)
Examples:
Pt(0, 0).dist2_128(Pt(3, 4)) == 25
Pt.dist
fn dist(self, o: Pt) -> F64The Euclidean distance from self to o, as F64.
- cost: O(1)
- traps: never
Examples:
Pt(0, 0).dist(Pt(3, 4)) == 5.0
Pt.manhattan
fn manhattan(self, o: Pt) -> IntThe 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
Pt.chebyshev
fn chebyshev(self, o: Pt) -> IntThe 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
Pt
impl Add for PtPt.add
fn add(a, b)Pt
impl Sub for PtPt.sub
fn sub(a, b)Pt
impl Neg for PtPt.neg
fn neg(a)Pt
impl Mul<Int> for PtPt.mul
fn mul(a, k)Pt
impl Show for PtPt.show
fn show(a)ByAngle
pub struct ByAngle(pO(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.
ByAngle
impl Ord for ByAngleByAngle.lt
fn lt(a, b)ByAngleFrom
pub struct ByAngleFrom(base: Pt, pO(1) comparison key. Counter-clockwise by direction starting from ray base.
Exact in I128; vectors in the same direction compare equal, zero comes first.
ByAngleFrom
impl Ord for ByAngleFromByAngleFrom.lt
fn lt(a, b)OnlineHull
pub struct OnlineHullO(n) memory. Incremental 2D convex hull of integer points backed by upper and lower chains.
OnlineHull.new
fn new() -> OnlineHullAn 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
OnlineHull.add
fn add(mut self, p: Pt) -> BoolInsert 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)) == truevar h = OnlineHull(); h.add(Pt(0, 0)); h.add(Pt(4, 0)); h.add(Pt(0, 4)); h.add(Pt(1, 1)) == falsevar h = OnlineHull(); h.add(Pt(0, 0)); h.add(Pt(0, 0)) == false
OnlineHull.locate
fn locate(self, p: Pt) -> LocationWhere 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.Insidevar h = OnlineHull(); h.add(Pt(0, 0)); h.add(Pt(4, 0)); h.add(Pt(0, 4)); h.locate(Pt(2, 0)) == Location.BoundaryOnlineHull().locate(Pt(0, 0)) == Location.Outside
OnlineHull.extreme
fn extreme(self, dir: Pt) -> ?PtThe 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
OnlineHull.area2
fn area2(self) -> IntTwice 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 == 16OnlineHull().area2 == 0
OnlineHull.len
fn len(self) -> IntThe 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
OnlineHull.is_empty
fn is_empty(self) -> BoolWhether no point has been added since creation or the last clear.
- cost: O(1)
- traps: never
Examples:
OnlineHull().is_empty == truevar h = OnlineHull(); h.add(Pt(1, 1)); h.is_empty == false
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 newList, 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)]
OnlineHull.iter
fn iter(self) -> yields PtThe 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)]
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
Location
pub enum Location = Inside | Boundary | OutsideO(1) enum. Position of a point relative to a polygon or disk.
Location.Inside
InsideLocation.Boundary
BoundaryLocation.Outside
OutsidePI
pub const PI: F64 = 3.14159265358979323846O(1). Circle constant pi (canonical home: std.math.PI; re-exported here).
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.
Vec.norm2
fn norm2(self) -> F64The squared length x*x + y*y.
- cost: O(1)
- traps: never
Examples:
Vec(3.0, 4.0).norm2 == 25.0
Vec.len
fn len(self) -> F64The Euclidean length (hypot(x, y), so no intermediate overflow).
- cost: O(1)
- traps: never
Examples:
Vec(3.0, 4.0).len == 5.0
Vec.perp
fn perp(self) -> Vecself 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)
Vec.perp_cw
fn perp_cw(self) -> Vecself 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)
Vec.angle
fn angle(self) -> F64The 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.0Vec(-1.0, 0.0).angle == PIVec(0.0, 1.0).angle == PI / 2.0
Vec.from_angle
fn from_angle(rad: F64) -> VecThe 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)
Vec.unit
fn unit(self) -> VecThe 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)
Vec.rot
fn rot(self, by: Vec) -> Vecself 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)
Vec.dot
fn dot(self, o: Vec) -> F64The 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
Vec.cross
fn cross(self, o: Vec) -> F64The 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
Vec.dist2
fn dist2(self, o: Vec) -> F64The 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
Vec.dist
fn dist(self, o: Vec) -> F64The 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
Vec
impl Add for VecVec.add
fn add(a, b)Vec
impl Sub for VecVec.sub
fn sub(a, b)Vec
impl Neg for VecVec.neg
fn neg(a)Vec
impl Mul<F64> for VecVec.mul
fn mul(a, k)Vec
impl Div<F64> for VecVec.div
fn div(a, k)Circle
pub struct Circle(c: Vec, r: F64)O(1) value type. A 2D circle with center c and non-negative radius r.
Circle.through
fn through(a: Vec, b: Vec, c: Vec, eps: F64 = 1e-9) -> ?CircleThe 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
Circle.area
fn area(self) -> F64The disk area, computed as PI * r * r.
- cost: O(1)
- traps: never
Examples:
Circle(Vec(0.0, 0.0), 1.0).area == PI
Circle.circumference
fn circumference(self) -> F64The circumference, computed as 2.0 * PI * r.
- cost: O(1)
- traps: never
Examples:
Circle(Vec(0.0, 0.0), 0.5).circumference == PI
Circle.locate
fn locate(self, p: Vec, eps: F64 = 1e-9) -> LocationWhere 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.BoundaryCircle(Vec(0.0, 0.0), 5.0).locate(Vec(1.0, 1.0)) == Location.InsideCircle(Vec(0.0, 0.0), 5.0).locate(Vec(6.0, 0.0)) == Location.Outside
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 }.
HalfPlane.through
fn through(a: Vec, b: Vec) -> HalfPlaneThe 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))
HalfPlane.from_line
fn from_line(a: F64, b: F64, c: F64) -> HalfPlaneThe 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.0andb == 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
HalfPlane.side
fn side(self, q: Vec) -> F64The 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.0HalfPlane.through(Vec(0.0, 0.0), Vec(1.0, 0.0)).side(Vec(3.0, -2.0)) == -2.0
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).
Rect.area
fn area(self) -> IntThe 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 == 6Rect(3..1, 0..2).area == 0
Rect.is_empty
fn is_empty(self) -> BoolWhether either axis range is empty.
- cost: O(1)
- traps: never
Examples:
Rect(0..0, 0..5).is_empty == trueRect(0..1, 0..1).is_empty == false
Rect.contains
fn contains(self, p: Pt) -> BoolWhether 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)) == trueRect(0..3, 0..2).contains(Pt(3, 1)) == false
Rect.intersect
fn intersect(self, o: Rect) -> ?RectThe 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
Pt3
pub struct Pt3(x: Int, y: Int, z: Int)O(1) value type. A 3D point or vector with exact integer coordinates.
Pt3.norm2
fn norm2(self) -> IntThe 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
Pt3.norm2_128
fn norm2_128(self) -> I128The exact squared length, computed in I128.
- cost: O(1)
- traps: when the result overflows
I128
Examples:
Pt3(1, 2, 2).norm2_128 == 9
Pt3.len
fn len(self) -> F64The Euclidean length as F64.
- cost: O(1)
- traps: never
Examples:
Pt3(1, 2, 2).len == 3.0
Pt3.to_vec3
fn to_vec3(self) -> Vec3self 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)
Pt3.dot
fn dot(self, o: Pt3) -> IntThe 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
Pt3.dot128
fn dot128(self, o: Pt3) -> I128The 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
Pt3.cross
fn cross(self, o: Pt3) -> Pt3The 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)
Pt3.dist2
fn dist2(self, o: Pt3) -> IntThe 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
Pt3.dist
fn dist(self, o: Pt3) -> F64The 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
Pt3.manhattan
fn manhattan(self, o: Pt3) -> IntThe 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
Pt3.chebyshev
fn chebyshev(self, o: Pt3) -> IntThe 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
Pt3
impl Add for Pt3Pt3.add
fn add(a, b)Pt3
impl Sub for Pt3Pt3.sub
fn sub(a, b)Pt3
impl Neg for Pt3Pt3.neg
fn neg(a)Pt3
impl Mul<Int> for Pt3Pt3.mul
fn mul(a, k)Pt3
impl Show for Pt3Pt3.show
fn show(a)Vec3
pub struct Vec3(x: F64, y: F64, z: F64)O(1) value type. A 3D floating-point point or vector.
Vec3.norm2
fn norm2(self) -> F64The squared length x*x + y*y + z*z.
- cost: O(1)
- traps: never
Examples:
Vec3(1.0, 2.0, 2.0).norm2 == 9.0
Vec3.len
fn len(self) -> F64The Euclidean length.
- cost: O(1)
- traps: never
Examples:
Vec3(1.0, 2.0, 2.0).len == 3.0
Vec3.unit
fn unit(self) -> Vec3The 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)
Vec3.dot
fn dot(self, o: Vec3) -> F64The 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
Vec3.cross
fn cross(self, o: Vec3) -> Vec3The 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)
Vec3.dist2
fn dist2(self, o: Vec3) -> F64The 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
Vec3.dist
fn dist(self, o: Vec3) -> F64The 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
Vec3.rot
fn rot(self, axis: Vec3, rad: F64) -> Vec3self 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)
Vec3
impl Add for Vec3Vec3.add
fn add(a, b)Vec3
impl Sub for Vec3Vec3.sub
fn sub(a, b)Vec3
impl Neg for Vec3Vec3.neg
fn neg(a)Vec3
impl Mul<F64> for Vec3Vec3.mul
fn mul(a, k)Vec3
impl Div<F64> for Vec3Vec3.div
fn div(a, k)Plane3
pub struct Plane3(n: Vec3, d: F64)O(1) value type. A 3D plane { p | n.dot(p) == d } with unit normal n.
Plane3.through
fn through(a: Vec3, b: Vec3, c: Vec3, eps: F64 = 1e-9) -> ?Plane3The 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
Plane3.dist
fn dist(self, p: Vec3) -> F64The 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.0Plane3(Vec3(0.0, 0.0, 1.0), 1.0).dist(Vec3(5.0, 5.0, 0.0)) == -1.0
Plane3.project
fn project(self, p: Vec3) -> Vec3The 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)