Skip to content

API > @blocksuite/blocks > Vec

Class: Vec ​

Constructors ​

constructor ​

new Vec(): Vec

Returns ​

Vec

Methods ​

abs ​

static abs(A): number[]

Absolute value of a vector.

Parameters ​

ParameterType
Anumber[]

Returns ​

number[]

Defined In ​

packages/blocks/src/surface-block/utils/vec.ts:352


add ​

static add(A, B): number[]

Add vectors.

Parameters ​

ParameterType
Anumber[]
Bnumber[]

Returns ​

number[]

Defined In ​

packages/blocks/src/surface-block/utils/vec.ts:42


addScalar ​

static addScalar(A, n): number[]

Add scalar to vector.

Parameters ​

ParameterType
Anumber[]
nnumber

Returns ​

number[]

Defined In ​

packages/blocks/src/surface-block/utils/vec.ts:51


ang ​

static ang(A, B): number

Angle between vector A and vector B in radians

Parameters ​

ParameterType
Anumber[]
Bnumber[]

Returns ​

number

Defined In ​

packages/blocks/src/surface-block/utils/vec.ts:241


ang3 ​

static ang3( p1, pc, p2): number

Get the angle between the three vectors A, B, and C.

Parameters ​

ParameterType
p1number[]
pcnumber[]
p2number[]

Returns ​

number

Defined In ​

packages/blocks/src/surface-block/utils/vec.ts:340


angle ​

static angle(A, B): number

Angle between vector A and vector B in radians

Parameters ​

ParameterType
Anumber[]
Bnumber[]

Returns ​

number

Defined In ​

packages/blocks/src/surface-block/utils/vec.ts:250


clamp ​

static clamp(n, min): number

Clamp a value into a range.

Parameters ​

ParameterType
nnumber
minnumber

Returns ​

number

Defined In ​

packages/blocks/src/surface-block/utils/vec.ts:12

static clamp( n, min, max): number

Parameters ​

ParameterType
nnumber
minnumber
maxnumber

Returns ​

number

Defined In ​

packages/blocks/src/surface-block/utils/vec.ts:13


clampV ​

static clampV(A, min): number[]

Clamp a value into a range.

Parameters ​

ParameterType
Anumber[]
minnumber

Returns ​

number[]

Defined In ​

packages/blocks/src/surface-block/utils/vec.ts:23

static clampV( A, min, max): number[]

Parameters ​

ParameterType
Anumber[]
minnumber
maxnumber

Returns ​

number[]

Defined In ​

packages/blocks/src/surface-block/utils/vec.ts:24


clockwise ​

static clockwise( p1, pc, p2): boolean

Get whether p1 is left of p2, relative to pc.

Parameters ​

ParameterType
p1number[]
pcnumber[]
p2number[]

Returns ​

boolean

Defined In ​

packages/blocks/src/surface-block/utils/vec.ts:382


cpr ​

static cpr(A, B): number

Cross product (outer product) | A X B |

Parameters ​

ParameterType
Anumber[]
Bnumber[]

Returns ​

number

Defined In ​

packages/blocks/src/surface-block/utils/vec.ts:141


cross ​

static cross( x, y, z): number

Cross (for point in polygon)

Parameters ​

ParameterType
xnumber[]
ynumber[]
znumber[]

Returns ​

number

Defined In ​

packages/blocks/src/surface-block/utils/vec.ts:149


dist ​

static dist(A, B): number

Dist length from A to B

Parameters ​

ParameterType
Anumber[]
Bnumber[]

Returns ​

number

Defined In ​

packages/blocks/src/surface-block/utils/vec.ts:218


dist2 ​

static dist2(A, B): number

Dist length from A to B squared.

Parameters ​

ParameterType
Anumber[]
Bnumber[]

Returns ​

number

Defined In ​

packages/blocks/src/surface-block/utils/vec.ts:209


distanceToBounds ​

static distanceToBounds(bounds, P): number

Distance between a point and the nearest point on a bounding box.

Parameters ​

ParameterTypeDescription
boundsobjectThe bounding box.
bounds.maxXnumber-
bounds.maxYnumber-
bounds.minXnumber-
bounds.minYnumber-
Pnumber[]The point

Returns ​

number

Defined In ​

packages/blocks/src/surface-block/utils/vec.ts:508


distanceToLineSegment ​

static distanceToLineSegment( A, B, P, clamp = true): number

Distance between a point and the nearest point on a line segment between A and B

Parameters ​

ParameterTypeDefault valueDescription
Anumber[]undefinedThe start of the line segment
Bnumber[]undefinedThe end of the line segment
Pnumber[]undefinedThe off-line point
clampbooleantrueWhether to clamp the point between A and B.

Returns ​

number

Defined In ​

packages/blocks/src/surface-block/utils/vec.ts:472


distanceToLineThroughPoint ​

static distanceToLineThroughPoint( A, u, P): number

Distance between a point and a line with a known unit vector that passes through a point.

Parameters ​

ParameterTypeDescription
Anumber[]Any point on the line
unumber[]The unit vector for the line.
Pnumber[]A point not on the line to test.

Returns ​

number

Defined In ​

packages/blocks/src/surface-block/utils/vec.ts:429


div ​

static div(A, n): number[]

Vector division by scalar.

Parameters ​

ParameterType
Anumber[]
nnumber

Returns ​

number[]

Defined In ​

packages/blocks/src/surface-block/utils/vec.ts:106


divV ​

static divV(A, B): number[]

Vector division by vector.

Parameters ​

ParameterType
Anumber[]
Bnumber[]

Returns ​

number[]

Defined In ​

packages/blocks/src/surface-block/utils/vec.ts:115


dpr ​

static dpr(A, B): number

Dot product

Parameters ​

ParameterType
Anumber[]
Bnumber[]

Returns ​

number

Defined In ​

packages/blocks/src/surface-block/utils/vec.ts:132


fastDist ​

static fastDist(A, B): number[]

A faster, though less accurate method for testing distances. Maybe faster?

Parameters ​

ParameterType
Anumber[]
Bnumber[]

Returns ​

number[]

Defined In ​

packages/blocks/src/surface-block/utils/vec.ts:228


int ​

static int( A, B, from, to, s = 1): number[]

Interpolate from A to B when curVAL goes fromVAL: number[] => to

Parameters ​

ParameterTypeDefault valueDescription
Anumber[]undefined-
Bnumber[]undefined-
fromnumberundefinedStarting value
tonumberundefinedEnding value
snumber1Strength

Returns ​

number[]

Defined In ​

packages/blocks/src/surface-block/utils/vec.ts:323


isEqual ​

static isEqual(A, B): boolean

Check of two vectors are identical.

Parameters ​

ParameterType
Anumber[]
Bnumber[]

Returns ​

boolean

Defined In ​

packages/blocks/src/surface-block/utils/vec.ts:301


isLeft ​

static isLeft( p1, pc, p2): number

Get whether p1 is left of p2, relative to pc.

Parameters ​

ParameterType
p1number[]
pcnumber[]
p2number[]

Returns ​

number

Defined In ​

packages/blocks/src/surface-block/utils/vec.ts:367


len ​

static len(A): number

Length of the vector

Parameters ​

ParameterType
Anumber[]

Returns ​

number

Defined In ​

packages/blocks/src/surface-block/utils/vec.ts:165


len2 ​

static len2(A): number

Length of the vector squared

Parameters ​

ParameterType
Anumber[]

Returns ​

number

Defined In ​

packages/blocks/src/surface-block/utils/vec.ts:157


lrp ​

static lrp( A, B, t): number[]

Interpolate vector A to B with a scalar t

Parameters ​

ParameterTypeDescription
Anumber[]-
Bnumber[]-
tnumberscalar

Returns ​

number[]

Defined In ​

packages/blocks/src/surface-block/utils/vec.ts:311


max ​

static max(...v): number[]

Get a vector comprised of the maximum of two or more vectors.

Parameters ​

ParameterType
...vnumber[][]

Returns ​

number[]

Defined In ​

packages/blocks/src/surface-block/utils/vec.ts:583


med ​

static med(A, B): number[]

Mean between two vectors or mid vector between two vectors

Parameters ​

ParameterType
Anumber[]
Bnumber[]

Returns ​

number[]

Defined In ​

packages/blocks/src/surface-block/utils/vec.ts:259


min ​

static min(...v): number[]

Get a vector comprised of the minimum of two or more vectors.

Parameters ​

ParameterType
...vnumber[][]

Returns ​

number[]

Defined In ​

packages/blocks/src/surface-block/utils/vec.ts:590


mul ​

static mul(A, n): number[]

Vector multiplication by scalar

Parameters ​

ParameterType
Anumber[]
nnumber

Returns ​

number[]

Defined In ​

packages/blocks/src/surface-block/utils/vec.ts:88


mulV ​

static mulV(A, B): number[]

Multiple two vectors.

Parameters ​

ParameterType
Anumber[]
Bnumber[]

Returns ​

number[]

Defined In ​

packages/blocks/src/surface-block/utils/vec.ts:97


nearestPointOnBounds ​

static nearestPointOnBounds(bounds, P): number[]

Get the nearest point on a bounding box to a point P.

Parameters ​

ParameterTypeDescription
boundsobjectThe bounding box
bounds.maxXnumber-
bounds.maxYnumber-
bounds.minXnumber-
bounds.minYnumber-
Pnumber[]The point point

Returns ​

number[]

Defined In ​

packages/blocks/src/surface-block/utils/vec.ts:487


nearestPointOnLineSegment ​

static nearestPointOnLineSegment( A, B, P, clamp = true): number[]

Get the nearest point on a line segment between A and B

Parameters ​

ParameterTypeDefault valueDescription
Anumber[]undefinedThe start of the line segment
Bnumber[]undefinedThe end of the line segment
Pnumber[]undefinedThe off-line point
clampbooleantrueWhether to clamp the point between A and B.

Returns ​

number[]

Defined In ​

packages/blocks/src/surface-block/utils/vec.ts:445


nearestPointOnLineThroughPoint ​

static nearestPointOnLineThroughPoint( A, u, P): number[]

Get the nearest point on a line with a known unit vector that passes through point A

Parameters ​

ParameterTypeDescription
Anumber[]Any point on the line
unumber[]The unit vector for the line.
Pnumber[]A point not on the line to test.

Returns ​

number[]

Defined In ​

packages/blocks/src/surface-block/utils/vec.ts:414


neg ​

static neg(A): number[]

Negate a vector.

Parameters ​

ParameterType
Anumber[]

Returns ​

number[]

Defined In ​

packages/blocks/src/surface-block/utils/vec.ts:33


normalize ​

static normalize(A): number[]

Get normalized / unit vector.

Parameters ​

ParameterType
Anumber[]

Returns ​

number[]

Defined In ​

packages/blocks/src/surface-block/utils/vec.ts:190


nudge ​

static nudge( A, B, d): number[]

Push a point A towards point B by a given distance.

Parameters ​

ParameterType
Anumber[]
Bnumber[]
dnumber

Returns ​

number[]

Defined In ​

packages/blocks/src/surface-block/utils/vec.ts:527


nudgeAtAngle ​

static nudgeAtAngle( A, a, d): number[]

Push a point in a given angle by a given distance.

Parameters ​

ParameterType
Anumber[]
anumber
dnumber

Returns ​

number[]

Defined In ​

packages/blocks/src/surface-block/utils/vec.ts:538


per ​

static per(A): number[]

Perpendicular rotation of a vector A

Parameters ​

ParameterType
Anumber[]

Returns ​

number[]

Defined In ​

packages/blocks/src/surface-block/utils/vec.ts:123


pointOffset ​

static pointOffset( A, B, offset): IVec

Parameters ​

ParameterType
AIVec
BIVec
offsetnumber

Returns ​

IVec

Defined In ​

packages/blocks/src/surface-block/utils/vec.ts:565


pointsBetween ​

static pointsBetween( A, B, steps = 6): number[][]

Get an array of points between two points.

Parameters ​

ParameterTypeDefault valueDescription
Anumber[]undefinedThe first point.
Bnumber[]undefinedThe second point.
stepsnumber6The number of points to return.

Returns ​

number[][]

Defined In ​

packages/blocks/src/surface-block/utils/vec.ts:557


pry ​

static pry(A, B): number

Project A over B

Parameters ​

ParameterType
Anumber[]
Bnumber[]

Returns ​

number

Defined In ​

packages/blocks/src/surface-block/utils/vec.ts:174


rescale ​

static rescale(a, n): number[]

Parameters ​

ParameterType
anumber[]
nnumber

Returns ​

number[]

Defined In ​

packages/blocks/src/surface-block/utils/vec.ts:356


rot ​

static rot(A, r = 0): number[]

Vector rotation by r (radians)

Parameters ​

ParameterTypeDefault valueDescription
Anumber[]undefined-
rnumber0rotation in radians

Returns ​

number[]

Defined In ​

packages/blocks/src/surface-block/utils/vec.ts:268


rotWith ​

static rotWith( A, C, r = 0): number[]

Rotate a vector around another vector by r (radians)

Parameters ​

ParameterTypeDefault valueDescription
Anumber[]undefinedvector
Cnumber[]undefinedcenter
rnumber0rotation in radians

Returns ​

number[]

Defined In ​

packages/blocks/src/surface-block/utils/vec.ts:281


slope ​

static slope(A, B): number

Get the slope between two points.

Parameters ​

ParameterType
Anumber[]
Bnumber[]

Returns ​

number

Defined In ​

packages/blocks/src/surface-block/utils/vec.ts:575


snap ​

static snap(a, step = 1): number[]

Snap vector to nearest step.

Parameters ​

ParameterTypeDefault value
anumber[]undefined
stepnumber1

Returns ​

number[]

Example ​

ts
Vec.snap([10.5, 28], 10) // [10, 30]
Vec.snap([10.5, 28], 10) // [10, 30]

Defined In ​

packages/blocks/src/surface-block/utils/vec.ts:403


sub ​

static sub(A, B): number[]

Subtract vectors.

Parameters ​

ParameterType
Anumber[]
Bnumber[]

Returns ​

number[]

Defined In ​

packages/blocks/src/surface-block/utils/vec.ts:60


subScalar ​

static subScalar(A, n): number[]

Subtract scalar from vector.

Parameters ​

ParameterType
Anumber[]
nnumber

Returns ​

number[]

Defined In ​

packages/blocks/src/surface-block/utils/vec.ts:69


tangent ​

static tangent(A, B): number[]

Get the tangent between two vectors.

Parameters ​

ParameterType
Anumber[]
Bnumber[]

Returns ​

number[]

Defined In ​

packages/blocks/src/surface-block/utils/vec.ts:200


toFixed ​

static toFixed(a): number[]

Round a vector to two decimal places.

Parameters ​

ParameterType
anumber[]

Returns ​

number[]

Defined In ​

packages/blocks/src/surface-block/utils/vec.ts:390


toPoint ​

static toPoint(v): object

Parameters ​

ParameterType
vIVec

Returns ​

x ​

x: number

y ​

y: number

Defined In ​

packages/blocks/src/surface-block/utils/vec.ts:596


toPrecision ​

static toPrecision(a, n = 4): number[]

Round a vector to a precision length.

Parameters ​

ParameterTypeDefault value
anumber[]undefined
nnumber4

Returns ​

number[]

Defined In ​

packages/blocks/src/surface-block/utils/vec.ts:547


toVec ​

static toVec(v): number[]

Parameters ​

ParameterType
vobject
v.xnumber
v.ynumber

Returns ​

number[]

Defined In ​

packages/blocks/src/surface-block/utils/vec.ts:594


uni ​

static uni(A): number[]

Get normalized / unit vector.

Parameters ​

ParameterType
Anumber[]

Returns ​

number[]

Defined In ​

packages/blocks/src/surface-block/utils/vec.ts:182


vec ​

static vec(A, B): number[]

Get the vector from vectors A to B.

Parameters ​

ParameterType
Anumber[]
Bnumber[]

Returns ​

number[]

Defined In ​

packages/blocks/src/surface-block/utils/vec.ts:78