Pt

Pt is a subclass of standard Float32Array with additional properties and functions to support vector and geometric calculations. See Pt guide for details.

Class extends Float32Array implements IPt, Iterable in src/Pt.ts

Constructor

new Pt ( args )

Create a Pt. If no parameter is provided, this will instantiate a Pt with 2 dimensions [0, 0]. Note that new Pt(3) will only instantiate Pt with length of 3 (ie, same as new Float32Array(3) ). If you need a Pt with 1 dimension of value 3, use new Pt([3]).

parameters
args: any[]

a list of numeric parameters, an array of numbers, or an object with {x,y,z,w} properties

returns
An instance of Pt
example

new Pt(), new Pt(1,2,3,4,5), new Pt([1,2]), new Pt({x:0, y:1}), new Pt(pt)

source

Accessors

GET SET id : string

ID string of this Pt

GET SET w : number

Value in the forth dimension of this Pt

GET SET x : number

Value in the first dimensional of this Pt

GET SET y : number

Value in the second dimension of this Pt

GET SET z : number

Value in the third dimension of this Pt

Methods

$abs ( )

Get a new Pt with absolute values of this Pt.

returns
Pt

source

$add ( args )

Like Pt.add, but returns result as a new Pt.

parameters
args: any[]

can be either a list of numbers, an array, a Pt, or an object with {x,y,z,w} properties

returns
Pt

source

$ceil ( )

Get a new Pt with ceiling values of this Pt.

returns
Pt

source

$concat ( args )

Concatenate this Pt with addition dimensional values and return as a new Pt.

parameters
args: any[]

can be either a list of numbers, an array, a Pt, or an object with {x,y,z,w} properties

returns
Pt

source

$cross ( args )

3D Cross product of this Pt and another Pt. Return results as a new Pt.

parameters
args: any[]

can be either a list of numbers, an array, a Pt, or an object with {x,y,z,w} properties

returns
Pt

source

$cross2D ( args )

2D Cross product of this Pt and another Pt. Return results as a new Pt.

parameters
args: any[]

can be either a list of numbers, an array, a Pt, or an object with {x,y,z,w} properties

returns
number

source

$divide ( args )

Like Pt.divide, but returns result as a new Pt.

parameters
args: any[]

can be either a list of numbers, an array, a Pt, or an object with {x,y,z,w} properties

returns
Pt

source

$floor ( )

Get a new Pt with floor values of this Pt.

returns
Pt

source

$max ( args )

Get a new Pt that has the maximum dimensional values of this Pt and another Pt.

parameters
args: any[]

can be either a list of numbers, an array, a Pt, or an object with {x,y,z,w} properties

returns
Pt

source

$min ( args )

Get a new Pt that has the minimum dimensional values of this Pt and another Pt.

parameters
args: any[]

can be either a list of numbers, an array, a Pt, or an object with {x,y,z,w} properties

returns
Pt

source

$multiply ( args )

Like Pt.multiply, but returns result as a new Pt.

parameters
args: any[]

can be either a list of numbers, an array, a Pt, or an object with {x,y,z,w} properties

returns
Pt

source

$project ( args )

Calculate vector projection of this Pt on another Pt.

parameters
args: any[]

can be either a list of numbers, an array, a Pt, or an object with {x,y,z,w} properties

returns
Pt

the projection vector as a Pt

source

$round ( )

Get a new Pt with rounded values of this Pt.

returns
Pt

source

$subtract ( args )

Like Pt.subtract, but returns result as a new Pt.

parameters
args: any[]

can be either a list of numbers, an array, a Pt, or an object with {x,y,z,w} properties

returns
Pt

source

$take ( axis )

Take specific dimensional values from this Pt and create a new Pt.

parameters
axis: string | number[]

a string such as "xy" (use Const.xy) or an array to specify indices

returns
Pt

source

$to ( args )

Like Pt.to but returns a new Pt.

parameters
args: any[]

can be either a list of numbers, an array, a Pt, or an object with {x,y,z,w} properties

returns
Pt

source

$unit ( magnitude )

Get a new unit vector from this Pt.

parameters
magnitude: number (default value: undefined)

returns
Pt

source

abs ( )

Absolute values for all values in this pt.

returns
Pt

source

add ( args )

Add scalar or vector values to this Pt.

parameters
args: any[]

can be either a list of numbers, an array, a Pt, or an object with {x,y,z,w} properties

returns
this

source

angle ( axis )

Get angle of this Pt from origin.

parameters
axis: string | number[] (default value: Const.xy)

a string such as "xy" (use Const.xy) or an array to specify index for two dimensions

returns
number

source

angleBetween ( p, axis )

Get the angle between this and another Pt.

parameters
p: Pt

the other Pt

axis: string | number[] (default value: Const.xy)

a string such as "xy" (use Const.xy) or an array to specify index for two dimensions

returns
number

source

ceil ( )

Ceiling values for all values in this Pt.

returns
Pt

source

clone ( )

Clone this Pt and return it as a new Pt.

returns
Pt

source

divide ( args )

Divide this Pt over scalar or vector values (as element-wise).

parameters
args: any[]

can be either a list of numbers, an array, a Pt, or an object with {x,y,z,w} properties

returns
this

source

dot ( args )

Dot product of this Pt and another Pt.

parameters
args: any[]

can be either a list of numbers, an array, a Pt, or an object with {x,y,z,w} properties

returns
number

source

equals ( p, threshold )

Check if another Pt is equal to this Pt, within a threshold.

parameters
p: PtLike

another Pt to compare with

threshold: number (default value: 0.000001)

a threshold value within which the two Pts are considered equal. Default is 0.000001.

returns
boolean

source

floor ( )

Floor values for all values in this Pt.

returns
Pt

source

magnitude ( )

Get the distance (magnitude) of this Pt from origin.

returns
number

source

magnitudeSq ( )

Get the squared distance (magnitude) of this Pt from origin.

returns
number

source

STATIC make ( dimensions, defaultValue, randomize )

Create an n-dimensional Pt with either default value or random values.

parameters
dimensions: number

number of dimensions

defaultValue: number (default value: 0)

optional default value to fill the dimensions

randomize: boolean (default value: false)

if true, randomize the value between 0 to default value

returns
Pt

source

maxValue ( )

Find the maximum value across all dimensions in this Pt.

returns
{ index:`number`, value:`number` }

an object with value and index which returns the maximum value and its dimensional index

source

minValue ( )

Find the minimum value across all dimensions in this Pt.

returns
{ index:`number`, value:`number` }

an object with value and index which returns the minimum value and its dimensional index

source

multiply ( args )

Multiply scalar or vector values (as element-wise) with this Pt.

parameters
args: any[]

can be either a list of numbers, an array, a Pt, or an object with {x,y,z,w} properties

returns
this

source

op ( fn )

Create an operation using this Pt, passing this Pt into a custom function's first parameter. See the Op guide for details.

parameters
fn: Fn(p1:PtLike, rest:any[])

any function that takes a Pt as its first parameter

returns
Fn(rest:any[])

a resulting function that takes other parameters required in fn

example

let myOp = pt.op( fn ); let result = myOp( [1,2,3] );

source

ops ( fns )

This combines a series of operations into an array. See the Op guide for details.

parameters
fns: Fn(p1:PtLike, rest:any[])

an array of functions for op

returns
Fn(rest:any[])

an array of resulting functions

example

let myOps = pt.ops([fn1, fn2, fn3]); let results = myOps.map( (op) => op([1,2,3]) );

source

projectScalar ( args )

Calculate scalar projection.

parameters
args: any[]

can be either a list of numbers, an array, a Pt, or an object with {x,y,z,w} properties

returns
number

source

reflect2D ( line, axis )

Reflect this Pt along a 2D line.

parameters
line: GroupLike

a Group of 2 Pts that defines a line for reflection

axis: string

optional axis such as "yz" to define a 2D plane of reflection

returns
this

source

rotate2D ( angle, anchor, axis )

Rotate this Pt from origin or from an anchor point in 2D.

parameters
angle: number

rotate angle

anchor: PtLike

optional anchor point to scale from

axis: string

optional string such as "yz" to specify a 2D plane

returns
this

source

round ( )

Rounded values for all values in this Pt.

returns
Pt

source

scale ( scale, anchor )

Scale this Pt from origin or from an anchor point.

parameters
scale: number | number[] | PtLike

scale ratio

anchor: PtLike

optional anchor point to scale from

returns
this

source

shear2D ( scale, anchor, axis )

Shear this Pt from origin or from an anchor point in 2D.

parameters
scale: number | number[] | PtLike

anchor: PtLike

optional anchor point to scale from

axis: string

optional string such as "yz" to specify a 2D plane

returns
this

source

subtract ( args )

Subtract scalar or vector values from this Pt.

parameters
args: any[]

can be either a list of numbers, an array, a Pt, or an object with {x,y,z,w} properties

returns
this

source

to ( args )

Update the values of this Pt.

parameters
args: any[]

can be either a list of numbers, an array, a Pt, or an object with {x,y,z,w} properties

returns
this

source

toAngle ( radian, magnitude, anchorFromPt )

Update the values of this Pt to point at a specific angle.

parameters
radian: number

target angle in radian

magnitude: number

Optional magnitude if known. If not provided, it'll calculate and use this Pt's magnitude.

anchorFromPt: boolean (default value: false)

If true, add it from this Pt's current position. Default is false which update the position from origin (0,0). See also Geom.rotate2D for rotating a point from another anchor point.

returns
this

source

toArray ( )

Convert this Pt to a javascript Array.

returns
number[]

source

toBound ( )

Convert this Pt to a Bound as new Group([0,...], pt)

returns
Bound

source

toGroup ( )

Convert this Pt to a Group as new Group([0,...], pt)

returns
Group

source

toString ( )

A string representation of this Pt. Eg, "Pt(1, 2, 3)".

returns
string

source
Overrides Float32Array.toString in src/Pt.ts

unit ( magnitude )

Convert to a unit vector, which is a normalized vector whose magnitude equals to 1.

parameters
magnitude: number (default value: undefined)

Optional: if the magnitude is known, pass it as a parameter to avoid duplicate calculation.

returns
Pt

source

[Symbol.iterator] ( )

returns
IterableIterator

source
Inherited from Float32Array.[Symbol.iterator]

copyWithin ( target, start, end )

Returns the this object after copying a section of the array identified by start and end to the same array starting at position target

parameters
target: number

If target is negative, it is treated as length+target where length is the length of the array.

start: number

If start is negative, it is treated as length+start. If end is negative, it is treated as length+end.

end: number

If not specified, length of the this object is used as its default value.

returns
this

source
Inherited from Float32Array.copyWithin

entries ( )

Returns an array of key, value pairs for every entry in the array

returns
IterableIterator

source
Inherited from Float32Array.entries

every ( predicate, thisArg )

Determines whether all the members of an array satisfy the specified test.

parameters
predicate: Fn(value:number, index:number, array:Float32Array)

A function that accepts up to three arguments. The every method calls the predicate function for each element in the array until the predicate returns a value which is coercible to the Boolean value false, or until the end of the array.

thisArg: any

An object to which the this keyword can refer in the predicate function. If thisArg is omitted, undefined is used as the this value.

returns
boolean

source
Inherited from Float32Array.every

fill ( value, start, end )

Changes all array elements from start to end index to a static value and returns the modified array

parameters
value: number

value to fill array section with

start: number

index to start filling the array at. If start is negative, it is treated as length+start where length is the length of the array.

end: number

index to stop filling the array at. If end is negative, it is treated as length+end.

returns
this

source
Inherited from Float32Array.fill

filter ( predicate, thisArg )

Returns the elements of an array that meet the condition specified in a callback function.

parameters
predicate: Fn(value:number, index:number, array:Float32Array)

A function that accepts up to three arguments. The filter method calls the predicate function one time for each element in the array.

thisArg: any

An object to which the this keyword can refer in the predicate function. If thisArg is omitted, undefined is used as the this value.

returns
Float32Array

source
Inherited from Float32Array.filter

find ( predicate, thisArg )

Returns the value of the first element in the array where predicate is true, and undefined otherwise.

parameters
predicate: Fn(value:number, index:number, obj:Float32Array)

find calls predicate once for each element of the array, in ascending order, until it finds one where predicate returns true. If such an element is found, find immediately returns that element value. Otherwise, find returns undefined.

thisArg: any

If provided, it will be used as the this value for each invocation of predicate. If it is not provided, undefined is used instead.

returns
number | undefined

source
Inherited from Float32Array.find

findIndex ( predicate, thisArg )

Returns the index of the first element in the array where predicate is true, and -1 otherwise.

parameters
predicate: Fn(value:number, index:number, obj:Float32Array)

find calls predicate once for each element of the array, in ascending order, until it finds one where predicate returns true. If such an element is found, findIndex immediately returns that element index. Otherwise, findIndex returns -1.

thisArg: any

If provided, it will be used as the this value for each invocation of predicate. If it is not provided, undefined is used instead.

returns
number

source
Inherited from Float32Array.findIndex

forEach ( callbackfn, thisArg )

Performs the specified action for each element in an array.

parameters
callbackfn: Fn(value:number, index:number, array:Float32Array)

A function that accepts up to three arguments. forEach calls the callbackfn function one time for each element in the array.

thisArg: any

An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.

returns
void

source
Inherited from Float32Array.forEach

includes ( searchElement, fromIndex )

Determines whether an array includes a certain element, returning true or false as appropriate.

parameters
searchElement: number

The element to search for.

fromIndex: number

The position in this array at which to begin searching for searchElement.

returns
boolean

source
Inherited from Float32Array.includes

indexOf ( searchElement, fromIndex )

Returns the index of the first occurrence of a value in an array.

parameters
searchElement: number

The value to locate in the array.

fromIndex: number

The array index at which to begin the search. If fromIndex is omitted, the search starts at index 0.

returns
number

source
Inherited from Float32Array.indexOf

join ( separator )

Adds all the elements of an array separated by the specified separator string.

parameters
separator: string

A string used to separate one element of an array from the next in the resulting String. If omitted, the array elements are separated with a comma.

returns
string

source
Inherited from Float32Array.join

keys ( )

Returns an list of keys in the array

returns
IterableIterator

source
Inherited from Float32Array.keys

lastIndexOf ( searchElement, fromIndex )

Returns the index of the last occurrence of a value in an array.

parameters
searchElement: number

The value to locate in the array.

fromIndex: number

The array index at which to begin the search. If fromIndex is omitted, the search starts at index 0.

returns
number

source
Inherited from Float32Array.lastIndexOf

map ( callbackfn, thisArg )

Calls a defined callback function on each element of an array, and returns an array that contains the results.

parameters
callbackfn: Fn(value:number, index:number, array:Float32Array)

A function that accepts up to three arguments. The map method calls the callbackfn function one time for each element in the array.

thisArg: any

An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.

returns
Float32Array

source
Inherited from Float32Array.map

reduce ( callbackfn )

Calls the specified callback function for all the elements in an array. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function.

parameters
callbackfn: Fn(previousValue:number, currentValue:number, currentIndex:number, array:Float32Array)

A function that accepts up to four arguments. The reduce method calls the callbackfn function one time for each element in the array.

returns
number

source
Inherited from Float32Array.reduce

reduceRight ( callbackfn )

Calls the specified callback function for all the elements in an array, in descending order. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function.

parameters
callbackfn: Fn(previousValue:number, currentValue:number, currentIndex:number, array:Float32Array)

A function that accepts up to four arguments. The reduceRight method calls the callbackfn function one time for each element in the array.

returns
number

source
Inherited from Float32Array.reduceRight

reverse ( )

Reverses the elements in an Array.

returns
Float32Array

source
Inherited from Float32Array.reverse

set ( array, offset )

Sets a value or an array of values.

parameters
array: ArrayLike

A typed or untyped array of values to set.

offset: number

The index in the current array at which the values are to be written.

returns
void

source
Inherited from Float32Array.set

slice ( start, end )

Returns a section of an array.

parameters
start: number

The beginning of the specified portion of the array.

end: number

The end of the specified portion of the array. This is exclusive of the element at the index 'end'.

returns
Float32Array

source
Inherited from Float32Array.slice

some ( predicate, thisArg )

Determines whether the specified callback function returns true for any element of an array.

parameters
predicate: Fn(value:number, index:number, array:Float32Array)

A function that accepts up to three arguments. The some method calls the predicate function for each element in the array until the predicate returns a value which is coercible to the Boolean value true, or until the end of the array.

thisArg: any

An object to which the this keyword can refer in the predicate function. If thisArg is omitted, undefined is used as the this value.

returns
boolean

source
Inherited from Float32Array.some

sort ( compareFn )

Sorts an array.

parameters
compareFn: Fn(a:number, b:number)

Function used to determine the order of the elements. It is expected to return a negative value if first argument is less than second argument, zero if they're equal and a positive value otherwise. If omitted, the elements are sorted in ascending order.

[11,2,22,1].sort((a, b) => a - b)
returns
this

source
Inherited from Float32Array.sort

subarray ( begin, end )

Gets a new Float32Array view of the ArrayBuffer store for this array, referencing the elements at begin, inclusive, up to end, exclusive.

parameters
begin: number

The index of the beginning of the array.

end: number

The index of the end of the array.

returns
Float32Array

source
Inherited from Float32Array.subarray

toLocaleString ( )

Converts a number to a string by using the current locale.

returns
string

source
Inherited from Float32Array.toLocaleString

valueOf ( )

Returns the primitive value of the specified object.

returns
Float32Array

source
Inherited from Float32Array.valueOf

values ( )

Returns an list of values in the array

returns
IterableIterator

source
Inherited from Float32Array.values