Group

A Group is a subclass of standard javascript Array. It should only contain Pt instances. You can think of it as an array of Float32Arrays. See Group guide for details.

Class extends Array in src/Pt.ts

Constructor

new Group ( args )

Create a Group by passing an array of Pt. You may also create a Group using Group.fromArray or Group.fromPtArray.

parameters
args: Pt[]

an array of Pts

returns
An instance of Group
source

Accessors

GET SET id : string

ID string of this Group

GET p1 : Pt

The first Pt in this Group

GET p2 : Pt

The second Pt in this Group

GET p3 : Pt

The third Pt in this Group

GET p4 : Pt

The forth Pt in this Group

GET q1 : Pt

The last Pt in this Group

GET q2 : Pt

The second-last Pt in this Group

GET q3 : Pt

The third-last Pt in this Group

GET q4 : Pt

The forth-last Pt in this Group

Methods

$matrixAdd ( g )

Apply this group as a matrix and calculate matrix addition.

parameters
g: GroupLike | number[][] | number

a scalar number, an array of numeric arrays, or a group of Pt

returns
Group

a new Group

source

$matrixMultiply ( g, transposed, elementwise )

Apply this group as a matrix and calculate matrix multiplication.

parameters
g: GroupLike | number

a scalar number, an array of numeric arrays, or a Group of K Pts, each with N dimensions (K-rows, N-columns) -- or if transposed is true, then N Pts with K dimensions

transposed: boolean (default value: false)

(Only applicable if it's not elementwise multiplication) If true, then a and b's columns should match (ie, each Pt should have the same dimensions). Default is false.

elementwise: boolean (default value: false)

if true, then the multiplication is done element-wise. Default is false.

returns
Group

If not elementwise, this will return a new Group with M Pt, each with N dimensions (M-rows, N-columns).

source

$zip ( defaultValue, useLongest )

Zip a group of Pt. eg, [[1,2],[3,4],[5,6]] => [[1,3,5],[2,4,6]].

parameters
defaultValue: number | boolean (default value: undefined)

a default value to fill if index out of bound. If not provided, it will throw an error instead.

useLongest: boolean (default value: false)

If true, find the longest list of values in a Pt and use its length for zipping. Default is false, which uses the first item's length for zipping.

returns
Group

source

add ( args )

Add scalar or vector values to this group's Pts.

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

anchorFrom ( ptOrIndex )

Anchor all the Pts in this Group by its absolute position from a target Pt. (ie, add all Pt with the target anchor to get an absolute position). All the Pts' values will be updated.

parameters
ptOrIndex: PtLike | number (default value: 0)

a Pt, or a numeric index to target a specific Pt in this Group

returns
void

source

anchorTo ( ptOrIndex )

Anchor all the Pts in this Group using a target Pt as origin. (ie, subtract all Pt with the target anchor to get a relative position). All the Pts' values will be updated.

parameters
ptOrIndex: PtLike | number (default value: 0)

a Pt, or a numeric index to target a specific Pt in this Group

returns
void

source

boundingBox ( )

Find the rectangular bounding box of this group's Pts.

returns
Group

a Group of 2 Pts representing the top-left and bottom-right of the rectangle

source

centroid ( )

Find the centroid of this group's Pts, which is the average middle point.

returns
Pt

source

clone ( )

Depp clone this group and its Pts.

returns
Group

source

divide ( args )

Divide this group's Pts 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

forEachPt ( ptFn, args )

Update each Pt in this Group with an existing Pt function.

parameters
ptFn: string

string name of an existing Pt function. Note that the function must return Pt.

args: any[]

arguments for the function specified in ptFn

returns
this

source

STATIC fromArray ( list )

Convert an array of numeric arrays into a Group.

parameters
list: PtLikeIterable

an Iterable such as an array or a generator (of PtLike numeric arrays)

returns
Group

example

Group.fromArray( [[1,2], [3,4], [5,6]] )

source

STATIC fromPtArray ( list )

Convert an Array/Iterable of Pt into a Group.

parameters
list: PtIterable

an Iterable

returns
Group

source

insert ( pts, index )

Insert more Pt into this group.

parameters
pts: PtIterable

a Group or an Iterable

index: number (default value: 0)

the index position to insert into

returns
this

source

interpolate ( t )

Get an interpolated point on the line segments defined by this Group.

parameters
t: number

a value between 0 to 1 usually

returns
Pt

source

lines ( )

Get all the line segments (ie, edges in a graph) of this group.

returns
Group[]

source

moveBy ( args )

Move every Pt's position by a specific amount. Same as Group.add.

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

moveTo ( args )

Move the first Pt in this group to a specific position, and move all the other Pts correspondingly.

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

multiply ( args )

Multiply scalar or vector values (as element-wise) with this group's Pts.

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 Group, passing this Group into a custom function's first parameter. See the Op guide for details.

parameters
fn: Fn(g1:PtIterable, rest:any[])

any function that takes a Group as its first parameter

returns
Fn(rest:any[])

a resulting function that takes other parameters required in fn

example

let myOp = group.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(g1:PtIterable, 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

reflect2D ( line, axis )

Reflect this group's Pts along a 2D line. Default anchor point is the first Pt in this group.

parameters
line: PtLikeIterable

a Group or an Iterable with 2 Pt that defines a line for reflection

axis: string

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

returns
this

source

remove ( index, count )

Like Array's splice function, with support for negative index and a friendlier name.

parameters
index: number (default value: 0)

start index, which can be negative (where -1 is at index 0, -2 at index 1, etc)

count: number (default value: 1)

number of items to remove

returns
Group

The items that are removed.

source

rotate2D ( angle, anchor, axis )

Rotate this group's Pt from an anchor point in 2D. Default anchor point is the first Pt in this group.

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

scale ( scale, anchor )

Scale this group's Pts from an anchor point. Default anchor point is the first Pt in this group.

parameters
scale: number | number[] | PtLike

scale ratio

anchor: PtLike

optional anchor point to scale from

returns
this

source

segments ( pts_per_segment, stride, loopBack )

Split this group into an array of sub-group segments.

parameters
pts_per_segment: number (default value: 2)

number of Pts in each segment

stride: number (default value: 1)

forward-step to take

loopBack: boolean (default value: false)

if true, always go through the array till the end and loop back to the beginning to complete the segments if needed

returns
Group[]

source

shear2D ( scale, anchor, axis )

Shear this group's Pt from an anchor point in 2D. Default anchor point is the first Pt in this group.

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

sortByDimension ( dim, desc )

Sort this group's Pts by values in a specific dimension.

parameters
dim: number

dimensional index

desc: boolean (default value: false)

if true, sort descending. Default is false (ascending)

returns
this

source

split ( chunkSize, stride, loopBack )

Split this Group into an array of sub-groups.

parameters
chunkSize: number

number of items per sub-group

stride: number

forward-steps after each sub-group

loopBack: boolean (default value: false)

if true, always go through the array till the end and loop back to the beginning to complete the segments if needed

returns
Group[]

source

subtract ( args )

Subtract scalar or vector values from this group's Pts.

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

toBound ( )

Get a Bound instance of this group

returns
Group

source

toString ( )

Get a string representation of this group.

returns
string

source
Overrides Array.toString in src/Pt.ts

zipSlice ( index, defaultValue )

Zip one slice of an array of Pt. Imagine the Pts are organized in rows, then this function will take the values in a specific column.

parameters
index: number

defaultValue: number | boolean (default value: false)

a default value to fill if index out of bound. If not provided, it will throw an error instead.

returns
Pt

source

[Symbol.iterator] ( )

Iterator

returns
IterableIterator

source
Inherited from Array.[Symbol.iterator]

[Symbol.unscopables] ( )

Returns an object whose properties have the value 'true' when they will be absent when used in a 'with' statement.

returns
{ copyWithin:`boolean`, entries:`boolean`, fill:`boolean`, find:`boolean`, findIndex:`boolean`, keys:`boolean`, values:`boolean` }

source
Inherited from Array.[Symbol.unscopables]

concat ( items )

Combines two or more arrays. This method returns a new array without modifying any existing arrays.

parameters
items: ConcatArray[]

Additional arrays and/or items to add to the end of the array.

returns
Pt[]

source
Inherited from Array.concat

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 Array.copyWithin

entries ( )

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

returns
IterableIterator

source
Inherited from Array.entries

every ( predicate, thisArg )

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

parameters
predicate: Fn(value:Pt, index:number, array:Pt[])

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
this

source
Inherited from Array.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: Pt

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 Array.fill

filter ( predicate, thisArg )

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

parameters
predicate: Fn(value:Pt, index:number, array:Pt[])

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
S[]

source
Inherited from Array.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:Pt, index:number, obj:Pt[])

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
S | undefined

source
Inherited from Array.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:Pt, index:number, obj:Pt[])

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 Array.findIndex

forEach ( callbackfn, thisArg )

Performs the specified action for each element in an array.

parameters
callbackfn: Fn(value:Pt, index:number, array:Pt[])

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 Array.forEach

includes ( searchElement, fromIndex )

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

parameters
searchElement: Pt

The element to search for.

fromIndex: number

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

returns
boolean

source
Inherited from Array.includes

indexOf ( searchElement, fromIndex )

Returns the index of the first occurrence of a value in an array, or -1 if it is not present.

parameters
searchElement: Pt

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 Array.indexOf

join ( separator )

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

parameters
separator: string

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

returns
string

source
Inherited from Array.join

keys ( )

Returns an iterable of keys in the array

returns
IterableIterator

source
Inherited from Array.keys

lastIndexOf ( searchElement, fromIndex )

Returns the index of the last occurrence of a specified value in an array, or -1 if it is not present.

parameters
searchElement: Pt

The value to locate in the array.

fromIndex: number

The array index at which to begin searching backward. If fromIndex is omitted, the search starts at the last index in the array.

returns
number

source
Inherited from Array.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:Pt, index:number, array:Pt[])

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
U[]

source
Inherited from Array.map

pop ( )

Removes the last element from an array and returns it. If the array is empty, undefined is returned and the array is not modified.

returns
Pt | undefined

source
Inherited from Array.pop

push ( items )

Appends new elements to the end of an array, and returns the new length of the array.

parameters
items: Pt[]

New elements to add to the array.

returns
number

source
Inherited from Array.push

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:Pt, currentValue:Pt, currentIndex:number, array:Pt[])

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

returns
Pt

source
Inherited from Array.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:Pt, currentValue:Pt, currentIndex:number, array:Pt[])

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

returns
Pt

source
Inherited from Array.reduceRight

reverse ( )

Reverses the elements in an array in place. This method mutates the array and returns a reference to the same array.

returns
Pt[]

source
Inherited from Array.reverse

shift ( )

Removes the first element from an array and returns it. If the array is empty, undefined is returned and the array is not modified.

returns
Pt | undefined

source
Inherited from Array.shift

slice ( start, end )

Returns a copy of a section of an array. For both start and end, a negative index can be used to indicate an offset from the end of the array. For example, -2 refers to the second to last element of the array.

parameters
start: number

The beginning index of the specified portion of the array. If start is undefined, then the slice begins at index 0.

end: number

The end index of the specified portion of the array. This is exclusive of the element at the index 'end'. If end is undefined, then the slice extends to the end of the array.

returns
Pt[]

source
Inherited from Array.slice

some ( predicate, thisArg )

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

parameters
predicate: Fn(value:Pt, index:number, array:Pt[])

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 Array.some

sort ( compareFn )

Sorts an array in place. This method mutates the array and returns a reference to the same array.

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

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

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

source
Inherited from Array.sort

splice ( start, deleteCount )

Removes elements from an array and, if necessary, inserts new elements in their place, returning the deleted elements.

parameters
start: number

The zero-based location in the array from which to start removing elements.

deleteCount: number

The number of elements to remove.

returns
Pt[]

An array containing the elements that were deleted.

source
Inherited from Array.splice

toLocaleString ( )

Returns a string representation of an array. The elements are converted to string using their toLocaleString methods.

returns
string

source
Inherited from Array.toLocaleString

unshift ( items )

Inserts new elements at the start of an array, and returns the new length of the array.

parameters
items: Pt[]

Elements to insert at the start of the array.

returns
number

source
Inherited from Array.unshift

values ( )

Returns an iterable of values in the array

returns
IterableIterator

source
Inherited from Array.values