Browse the docs

silk.geometry.point.

Two- and three-dimensional positions over any number type.

When to use#

Use Point2 or Point3 for a location, such as a path vertex, a glyph outline point, or the corner of a box. Use silk.geometry.vector for a displacement between locations.

Details#

A point is a Copy struct with public fields, generic over its number type T. Point arithmetic keeps positions and displacements apart: point - point gives a vector, point + vector and point - vector give a point, and there is no point + point. &a == &b compares every field when T: Equal. Methods appear only when T has the operations they need: distance needs silk.num.numeric.Float.

Gotchas#

Comparison takes shared borrows, as in &a == &b. Float fields compare with IEEE rules, so a point with a NaN field is not equal to itself.

Examples#

Move a point and measure how far it went#

silk
import silk.geometry.point { Point2 }
import silk.geometry.vector { Vector2 }

pub fn main() -> i32 {
  let start = Point2<f64>.new(1.0, 1.0)
  let end = start + Vector2<f64>.new(3.0, 4.0)
  let offset = end - start
  if start.distance(end) != 5.0 || offset.x != 3.0 || start.midpoint(end).y != 3.0 {
    return 1
  }
  return 0
}

Import as Point2 with import silk.geometry.point { Point2 }.

Public declarations: 2.

Point2#

silk
pub struct Point2<T>

A position in a plane with an x and a y coordinate of number type T.

When to use#

Use Point2 for a location. Use silk.geometry.vector.Vector2 for a displacement.

Details#

The fields are public. point - point gives a Vector2<T>, and point + vector and point - vector give a Point2<T>, when T has the operator. There is no point + point. &a == &b compares both coordinates when T: Equal.

Field x#

silk
pub x: T

The horizontal coordinate.

Field y#

silk
pub y: T

The vertical coordinate. In a y-down space, such as SVG or a screen, y grows downward.

Implementation Copy for Point2<T>#

silk
impl<T: Copy> Copy for Point2<T>

Implementation Point2<T>#

silk
impl<T> Point2<T>

Associated function new#

silk
pub fn new(x: T, y: T) -> Point2<T>

Returns the point at x and y.

Method map#

silk
pub fn map<U>(self: Self, transform: mut fn(T) -> U) -> Point2<U>

Returns a point whose coordinates are transform applied to x, then to y.

When to use#

Use map to change the number type, such as from font units in i32 to f64.

Method toVector#

silk
pub fn toVector(self: Self) -> Vector2<T>

Returns the vector from the origin to this point, with the same x and y.

Associated function fromVector#

silk
pub fn fromVector(vector: Vector2<T>) -> Point2<T>

Returns the point at the end of vector when it starts at the origin.

Implementation Point2<T>#

silk
impl<T: Copy> Point2<T>

Method coordinates#

silk
pub fn coordinates(self: Self) -> [T; 2]

Returns the coordinates as the array [x, y].

Implementation Point2<T>#

silk
impl<T: Zero> Point2<T>

Associated function origin#

silk
pub fn origin() -> Point2<T>

Returns the origin, the point whose coordinates are zero.

Implementation Point2<T>#

silk
impl<T: Copy + Add<T, T> + Subtract<T, T> + Multiply<T, T>> Point2<T>

Method lerp#

silk
pub fn lerp(self: Self, other: Point2<T>, t: T) -> Point2<T>

Returns self + (other - self) * t: self at t = 0 and other at t = 1.

Method distanceSquared#

silk
pub fn distanceSquared(self: Self, other: Point2<T>) -> T

Returns the square of the distance from self to other, with no square root.

When to use#

Use distanceSquared to compare distances, or when T is an integer or fixed-point type.

Implementation Point2<T>#

silk
impl<T: Copy + One + Add<T, T> + Divide<T, T>> Point2<T>

Method midpoint#

silk
pub fn midpoint(self: Self, other: Point2<T>) -> Point2<T>

Returns the point halfway between self and other.

Implementation Point2<T>#

silk
impl<T: Copy + Float + Subtract<T, T>> Point2<T>

Method distance#

silk
pub fn distance(self: Self, other: Point2<T>) -> T

Returns the distance from self to other, computed without overflow in the squares.

Method isFinite#

silk
pub fn isFinite(self: Self) -> bool

Returns true when both coordinates are neither infinite nor NaN.

Implementation Add<Vector2<T>, Point2<T>> for Point2<T>#

silk
impl<T: Add<T, T>> Add<Vector2<T>, Point2<T>> for Point2<T>

Operation add#

silk
fn add(left: Self, right: Vector2<T>) -> Self

Implementation Subtract<Vector2<T>, Point2<T>> for Point2<T>#

silk
impl<T: Subtract<T, T>> Subtract<Vector2<T>, Point2<T>> for Point2<T>

Operation subtract#

silk
fn subtract(left: Self, right: Vector2<T>) -> Self

Implementation Subtract<Point2<T>, Vector2<T>> for Point2<T>#

silk
impl<T: Subtract<T, T>> Subtract<Point2<T>, Vector2<T>> for Point2<T>

Operation subtract#

silk
fn subtract(left: Self, right: Self) -> Vector2<T>

Implementation Equal for Point2<T>#

silk
impl<T: Equal> Equal for Point2<T>

Operation equals#

silk
fn equals(left: &Self, right: &Self) -> bool

Point3#

silk
pub struct Point3<T>

A position in space with x, y, and z coordinates of number type T.

When to use#

Use Point3 for a location in space, such as a point that a silk.geometry.matrix.Matrix4 projects. Use silk.geometry.vector.Vector3 for a displacement.

Details#

The fields are public. point - point gives a Vector3<T>, and point + vector and point - vector give a Point3<T>, when T has the operator. There is no point + point. &a == &b compares all three coordinates when T: Equal.

Field x#

silk
pub x: T

The first coordinate.

Field y#

silk
pub y: T

The second coordinate.

Field z#

silk
pub z: T

The third coordinate.

Implementation Copy for Point3<T>#

silk
impl<T: Copy> Copy for Point3<T>

Implementation Point3<T>#

silk
impl<T> Point3<T>

Associated function new#

silk
pub fn new(x: T, y: T, z: T) -> Point3<T>

Returns the point at x, y, and z.

Method map#

silk
pub fn map<U>(self: Self, transform: mut fn(T) -> U) -> Point3<U>

Returns a point whose coordinates are transform applied to x, y, then z.

Method toVector#

silk
pub fn toVector(self: Self) -> Vector3<T>

Returns the vector from the origin to this point, with the same coordinates.

Associated function fromVector#

silk
pub fn fromVector(vector: Vector3<T>) -> Point3<T>

Returns the point at the end of vector when it starts at the origin.

Implementation Point3<T>#

silk
impl<T: Copy> Point3<T>

Method coordinates#

silk
pub fn coordinates(self: Self) -> [T; 3]

Returns the coordinates as the array [x, y, z].

Implementation Point3<T>#

silk
impl<T: Zero> Point3<T>

Associated function origin#

silk
pub fn origin() -> Point3<T>

Returns the origin, the point whose coordinates are zero.

Implementation Point3<T>#

silk
impl<T: Copy + Add<T, T> + Subtract<T, T> + Multiply<T, T>> Point3<T>

Method lerp#

silk
pub fn lerp(self: Self, other: Point3<T>, t: T) -> Point3<T>

Returns self + (other - self) * t: self at t = 0 and other at t = 1.

Method distanceSquared#

silk
pub fn distanceSquared(self: Self, other: Point3<T>) -> T

Returns the square of the distance from self to other, with no square root.

Implementation Point3<T>#

silk
impl<T: Copy + Float + Subtract<T, T>> Point3<T>

Method distance#

silk
pub fn distance(self: Self, other: Point3<T>) -> T

Returns the distance from self to other, computed without overflow in the squares.

Method isFinite#

silk
pub fn isFinite(self: Self) -> bool

Returns true when all three coordinates are neither infinite nor NaN.

Implementation Add<Vector3<T>, Point3<T>> for Point3<T>#

silk
impl<T: Add<T, T>> Add<Vector3<T>, Point3<T>> for Point3<T>

Operation add#

silk
fn add(left: Self, right: Vector3<T>) -> Self

Implementation Subtract<Vector3<T>, Point3<T>> for Point3<T>#

silk
impl<T: Subtract<T, T>> Subtract<Vector3<T>, Point3<T>> for Point3<T>

Operation subtract#

silk
fn subtract(left: Self, right: Vector3<T>) -> Self

Implementation Subtract<Point3<T>, Vector3<T>> for Point3<T>#

silk
impl<T: Subtract<T, T>> Subtract<Point3<T>, Vector3<T>> for Point3<T>

Operation subtract#

silk
fn subtract(left: Self, right: Self) -> Vector3<T>

Implementation Equal for Point3<T>#

silk
impl<T: Equal> Equal for Point3<T>

Operation equals#

silk
fn equals(left: &Self, right: &Self) -> bool

Category: Geometry.

Profiles: aarch64-apple-darwin, aarch64-unknown-linux-gnu, aarch64-unknown-linux-gnu-no-libc, x86_64-unknown-linux-gnu, x86_64-unknown-linux-gnu-no-libc, wasm32-unknown-unknown, wasm32-wasip1, wasm32-wasip2.

View the source of this page