abstract struct Geode::Point(T, N)

Overview

Base type for all point with a specific dimensionality.

T is the scalar type. N is a positive integer indicating the number of dimensions.

Included Modules

Direct Known Subclasses

Defined in:

geode/points/point.cr

Constructors

Instance Method Summary

Constructor Detail

def self.new(array : StaticArray(T, N)) #

Constructs the vector with pre-existing values.


[View source]
def self.new(&) #

Constructs the point by yielding for each coordinate.

The value of each coordinate should be returned from the block. The block will be given the index of each coordinate as an argument.

Point3(Int32).new { |i| i * 5 } # => (0, 5, 10)

[View source]
def self.origin : self #

Constructs a point referencing the origin.

Each coordinate will have a scalar value equal to the type's zero value. This is done by calling T.zero for each coordinate. The type T must have a .zero class method.

Point3(Float32).zero # => (0.0, 0.0, 0.0)

[View source]
def self.zero : self #

Constructs a point referencing the origin.

Each coordinate will have a scalar value equal to the type's zero value. This is done by calling T.zero for each coordinate. The type T must have a .zero class method.

Point3(Float32).zero # => (0.0, 0.0, 0.0)

[View source]

Instance Method Detail

def ==(other : Point) #

Checks if coordinates between two points are equal.

Compares this point coordinate-wise to another. Returns true if all coordinates are equal, false otherwise.

Point3[1, 2, 3] == Point3[1, 2, 3] # => true
Point3[1, 2, 3] == Point3[3, 2, 1] # => false

[View source]
def map(& : T -> U) : Point(U, N) forall U #

Returns a new point where coordinates are mapped by the given block.

point = Point3.new(1, 2, 3)
point.map { |v| v * 2 } # => (2, 4, 6)

[View source]
def map_with_index(offset = 0, & : T, Int32 -> U) : Point(U, N) forall U #

Like #map, but the block gets the coordinate and its index as arguments.

Accepts an optional offset parameter, which to start the index at.

point = Point3[1, 2, 3]
point.map_with_index { |v, i| v * i }    # => (0, 2, 6)
point.map_with_index(3) { |v, i| v + i } # => (4, 6, 8)

[View source]
def near_zero?(tolerance) #

Checks if this point is located near the origin.

Returns true if all coordinates of this point are close to zero.

Point3[0.0, 0.01, 0.001].near_zero?(0.01) # => true
Point3[0.1, 0.0, 0.01].near_zero?(0.01)   # => false

[View source]
def size #

Returns the dimensionality of this point.


[View source]
def to_s(io : IO) : Nil #

Produces a string representation of the point.

The format is: (x, y, z) but with the corresponding number of coordinates.


[View source]
def to_slice : Slice(T) #

Returns a slice that points to the coordinates in this point.

NOTE The returned slice is only valid for the caller's scope and sub-calls. The slice points to memory on the stack, it will be invalid after the caller returns.


[View source]
def to_unsafe : Pointer(T) #

Returns a pointer to the data for this point.

The coordinates are tightly packed and ordered consecutively in memory.

NOTE The returned pointer is only valid for the caller's scope and sub-calls. The pointer refers to memory on the stack, it will be invalid after the caller returns.


[View source]
abstract def to_vector #

Converts this point to a vector.

point = Point3[1, 2, 3]
point.to_vector # => (1, 2, 3)

[View source]
def unsafe_fetch(index : Int) #

Retrieves the scalar value of the coordinate at the given index, without checking size boundaries.

End-users should never invoke this method directly. Instead, methods like #[] and #[]? should be used.

This method should only be directly invoked if the index is certain to be in bounds.


[View source]
def zero? #

Checks if this point is located at the origin.

Returns true if all coordinates of this point are zero.

See: #near_zero?

Point3[0, 0, 0].zero? # => true
Point3[1, 0, 2].zero? # => false

[View source]