Skip to main content

Class: Vector2

二维向量

代码示例:

const a = new Vector2(0, 1);

const b = new Vector2();

const d = a.distanceTo(b);

Hierarchy

  • default

    Vector2

Table of contents

Constructors

Properties

Accessors

Methods

Constructors

constructor

new Vector2(x?, y?): Vector2

Parameters

NameTypeDefault valueDescription
xnumber0向量的x值,默认为0
ynumber0向量的y值,默认为0

Returns

Vector2

Overrides

Vector.constructor

Defined in

src/math/Vector2.ts:46

Properties

elements

elements: Float32Array | Float64Array

Overrides

Vector.elements

Defined in

src/math/Vector2.ts:40

Accessors

x

get x(): number

获取向量 x 值

Returns

number

number;

Defined in

src/math/Vector2.ts:57

set x(x): void

设置向量 x 值

Parameters

NameType
xnumber

Returns

void

Defined in

src/math/Vector2.ts:65


y

get y(): number

获取向量 y 值

Returns

number

number;

Defined in

src/math/Vector2.ts:73

set y(y): void

设置向量 y 值

Parameters

NameType
ynumber

Returns

void

Defined in

src/math/Vector2.ts:81

Methods

fromArray

fromArray(array, offset?): Vector2

从数组构建向量数据

Parameters

NameTypeDefault valueDescription
arrayanyundefined原始数组
offsetnumber0数组偏移量

Returns

Vector2

Inherited from

Vector.fromArray

Defined in

src/math/Vector.ts:15


toArray

toArray(out?, offset?): Float32Array | Float64Array | number[]

从向量转换到数组

Parameters

NameTypeDefault valueDescription
outFloat32Array | Float64Array | number[][]输出数据
offsetnumber0偏移量

Returns

Float32Array | Float64Array | number[]

Inherited from

Vector.toArray

Defined in

src/math/Vector.ts:28


fromObject

fromObject(object): Vector2

从对象赋值 Vector2 的 x、y 值

Parameters

NameType
objectany

Returns

Vector2

Defined in

src/math/Vector2.ts:89


toObject

toObject(): Object

转换到对象

Returns

Object

NameType
xnumber
ynumber

Defined in

src/math/Vector2.ts:99


set

set(x, y): Vector2

设置向量的 x 和 y 值

Parameters

NameType
xany
yany

Returns

Vector2

Defined in

src/math/Vector2.ts:111


setScalar

setScalar(s): Vector2

将向量的每个值设置为标量 s

Parameters

NameType
sany

Returns

Vector2

Defined in

src/math/Vector2.ts:121


add

add(vec): Vector2

将传入的向量vec和这个向量相加

Parameters

NameTypeDescription
vecVector2传入的 Vector2

Returns

Vector2

Defined in

src/math/Vector2.ts:129


addScalar

addScalar(v): Vector2

将传入的标量 v 和这个向量相加,默认构建一个二维向量 [v, v]

Parameters

NameTypeDescription
vnumber标量值

Returns

Vector2

Defined in

src/math/Vector2.ts:138


subtract

subtract(vec): Vector2

从此向量减去 vec 二维向量

Parameters

NameTypeDescription
vecVector2传入的 Vector2

Returns

Vector2

Defined in

src/math/Vector2.ts:147


subtractScalar

subtractScalar(v): Vector2

从此向量各分量减去标量 v,默认构建一个二维向量 [v, v]

Parameters

NameTypeDescription
vnumber标量值

Returns

Vector2

Defined in

src/math/Vector2.ts:156


multiply

multiply(vec): Vector2

将此向量与 vec 向量相乘

Parameters

NameType
vecVector2

Returns

Vector2

Defined in

src/math/Vector2.ts:165


multiplyScalar

multiplyScalar(v): Vector2

从此向量各分量与标量 v 相乘,默认构建一个二维向量 [v, v]

Parameters

NameTypeDescription
vnumber标量值

Returns

Vector2

Defined in

src/math/Vector2.ts:174


divide

divide(vec): Vector2

将此向量与 vec 向量相除

Parameters

NameType
vecVector2

Returns

Vector2

Defined in

src/math/Vector2.ts:183


divideScalar

divideScalar(v): Vector2

从此向量各分量与标量 v 相除,默认构建一个二维向量 [v, v]

Parameters

NameTypeDescription
vnumber标量值

Returns

Vector2

Defined in

src/math/Vector2.ts:192


scale

scale(s): Vector2

缩放此向量

Parameters

NameType
snumber

Returns

Vector2

Defined in

src/math/Vector2.ts:201


distanceTo

distanceTo(vec): any

计算该向量到所传入的 v 间的距离

Parameters

NameType
vecVector2

Returns

any

Defined in

src/math/Vector2.ts:210


length

length(): any

计算从 [0, 0] 到此向量的直线距离

Returns

any

Defined in

src/math/Vector2.ts:217


distanceToSquared

distanceToSquared(v): any

计算该向量到传入的v的平方距离

Parameters

NameType
vVector2

Returns

any

Defined in

src/math/Vector2.ts:225


angle

angle(): any

计算此向量相对于正 x 轴的弧度角

Returns

any

Defined in

src/math/Vector2.ts:232


angleTo

angleTo(v): any

以弧度返回该向量与向量 v 之间的角度

Parameters

NameType
vVector2

Returns

any

Defined in

src/math/Vector2.ts:239


dot

dot(vec): any

计算此向量与 vec 向量的点积

Parameters

NameType
vecVector2

Returns

any

Defined in

src/math/Vector2.ts:247


equals

equals(vec): any

判断传入的向量 vec 与此向量是否相等

Parameters

NameType
vecVector2

Returns

any

Defined in

src/math/Vector2.ts:255


cross

cross(vec): Vector2

将该向量设置为它本身与传入的vec的叉积

Parameters

NameType
vecVector2

Returns

Vector2

Defined in

src/math/Vector2.ts:263


negate

negate(): Vector2

针对此向量取反

Returns

Vector2

Defined in

src/math/Vector2.ts:271


inverse

inverse(): Vector2

针对此向量取倒数

Returns

Vector2

Defined in

src/math/Vector2.ts:279


lerp

lerp(vec, t): Vector2

从此向量到 vec 向量进行插值

Parameters

NameTypeDescription
vecVector2目标向量
tnumber插值因数,范围一般在[0-1],通常在这两个值之间

Returns

Vector2

Defined in

src/math/Vector2.ts:289


normalize

normalize(): Vector2

将此向量转换为单位向量 (将其设置为与此向量具有相同方向但长度为 1 的向量)

Returns

Vector2

Defined in

src/math/Vector2.ts:297


applyMatrix3

applyMatrix3(matrix): Vector2

将此向量(隐含的 1 作为第三个分量)乘以 Matrix3

Parameters

NameType
matrixMatrix3

Returns

Vector2

Defined in

src/math/Vector2.ts:306


applyMatrix4

applyMatrix4(matrix): Vector2

将此向量乘以 Matrix4

Parameters

NameType
matrixMatrix4

Returns

Vector2

Defined in

src/math/Vector2.ts:315


copy

copy(vec2): Vector2

将传递的 vec2 的 x 和 y 属性的值复制到此 Vector2

Parameters

NameType
vec2Vector2

Returns

Vector2

Defined in

src/math/Vector2.ts:324


clone

clone(): Vector2

克隆此向量

Returns

Vector2

Defined in

src/math/Vector2.ts:333


toString

toString(): string

转换为字符串

Returns

string

Defined in

src/math/Vector2.ts:340