vanilladocs
vanillapunch.com
API / base/vector2

base/vector2

2차원 벡터.

2D vector.

Vector2extends Object

짝Counterpart: vanilla-engine Vector2

2차원 벡터.

2D vector.

예제Example만들고 더하고 길이 재기Create, add and measure length
import { Vector2 } from "./build/vanilla.js";

const position = Vector2.create(3, 4);
const moved = position.add(Vector2.create(1, 0));
console.log(moved.x, moved.y, position.length());
생성자constructorconstructor()#0.0.8 부터since 0.0.8소스source

생성.

Constructor.

정적staticstatic create(x, y)#0.0.8 부터since 0.0.8소스source

새로운 벡터 생성.

Creates a new vector.

xnumber
X 좌표.X coordinate.
ynumber
Y 좌표.Y coordinate.

반환ReturnsVector2 새 벡터.A new vector.

짝Counterpart: vanilla-engine Vector2.create

정적staticstatic zero()#0.0.8 부터since 0.0.8소스source

0의 값을 가진 벡터 생성.

Creates a vector with the value 0.

반환ReturnsVector2 (0, 0) 벡터.A (0, 0) vector.

짝Counterpart: vanilla-engine Vector2.zero

정적staticstatic one()#0.0.8 부터since 0.0.8소스source

1의 값을 가진 벡터 생성.

Creates a vector with the value 1.

반환ReturnsVector2 (1, 1) 벡터.A (1, 1) vector.

짝Counterpart: vanilla-engine Vector2.one

정적staticstatic minusOne()#0.1.0 부터since 0.1.0소스source

-1의 값을 가진 벡터 생성.

Creates a vector with the value -1.

반환ReturnsVector2 (-1, -1) 벡터.A (-1, -1) vector.

짝Counterpart: vanilla-engine Vector2.minus_one

정적staticstatic positiveInfinity()#0.0.8 부터since 0.0.8소스source

최대값 벡터 생성.

Creates a maximum-value vector.

반환ReturnsVector2 (Infinity, Infinity) 벡터.An (Infinity, Infinity) vector.

짝Counterpart: vanilla-engine Vector2.positive_infinity

정적staticstatic negativeInfinity()#0.0.8 부터since 0.0.8소스source

최소값 벡터 생성.

Creates a minimum-value vector.

반환ReturnsVector2 (-Infinity, -Infinity) 벡터.A (-Infinity, -Infinity) vector.

짝Counterpart: vanilla-engine Vector2.negative_infinity

정적staticstatic lerp(from, to, normalizedTime)#0.0.8 부터since 0.0.8소스source

선형 보간.

Linear interpolation.

fromVector2
시작 벡터.Start vector.
toVector2
끝 벡터.End vector.
normalizedTimenumber
0 이면 from, 1 이면 to. (0 ~ 1 밖이어도 자르지 않고 늘여 보간한다)0 gives from, 1 gives to. (values outside 0 ~ 1 are not clamped and extrapolate)

반환ReturnsVector2 보간한 새 벡터.A new interpolated vector.

짝Counterpart: vanilla-engine Vector2.lerp

예제Example두 점 사이의 가운데 구하기Find the midpoint between two points
import { Vector2 } from "./build/vanilla.js";

const start = Vector2.create(0, 0);
const end = Vector2.create(100, 50);
const middle = Vector2.lerp(start, end, 0.5);
console.log(middle.x, middle.y);
정적staticstatic clamp(value, min, max)#0.0.8 부터since 0.0.8소스source

범위 제한.

Clamps to a range.

valueVector2
자를 벡터.Vector to clamp.
minVector2
성분별 최솟값.Per-component minimum.
maxVector2
성분별 최댓값.Per-component maximum.

반환ReturnsVector2 성분마다 min ~ max 로 자른 새 벡터.A new vector with each component clamped to min ~ max.

짝Counterpart: vanilla-engine Vector2.clamp

속성propertyget/set x#0.0.8 부터since 0.0.8소스source

X 좌표 반환 프로퍼티. X 좌표 설정 프로퍼티.

X coordinate getter property. X coordinate setter property.

반환Returnsnumber X 좌표.X coordinate.

짝Counterpart: vanilla-engine Vector2.x

속성propertyget/set y#0.0.8 부터since 0.0.8소스source

Y 좌표 반환 프로퍼티. Y 좌표 설정 프로퍼티.

Y coordinate getter property. Y coordinate setter property.

반환Returnsnumber Y 좌표.Y coordinate.

짝Counterpart: vanilla-engine Vector2.y

메서드methodequals(other)#0.0.8 부터since 0.0.8소스source

비교.

Comparison.

otherany
비교할 값.Value to compare.

반환Returnsboolean 같은 객체이거나 x, y 가 모두 같은 Vector2 면 참.True if it is the same object or a Vector2 with the same x and y.

메서드methodclone()#0.0.9 부터since 0.0.9소스source

복제.

Clone.

반환Returnsthis x, y 가 같은 새 벡터.A new vector with the same x and y.

메서드methodset(x, y)#0.0.8 부터since 0.0.8소스source

대입.

Assignment.

xnumber
X 좌표.X coordinate.
ynumber
Y 좌표.Y coordinate.

짝Counterpart: vanilla-engine Vector2.set

메서드methodgetX()#0.0.8 부터since 0.0.8소스source

가로축 반환.

Returns the horizontal axis.

반환Returnsnumber X 좌표.X coordinate.

짝Counterpart: vanilla-engine Vector2.get_x

메서드methodgetY()#0.0.8 부터since 0.0.8소스source

세로축 반환.

Returns the vertical axis.

반환Returnsnumber Y 좌표.Y coordinate.

짝Counterpart: vanilla-engine Vector2.get_y

메서드methodlength()#0.0.8 부터since 0.0.8소스source

길이.

Length.

반환Returnsnumber 벡터의 길이.Length of the vector.

짝Counterpart: vanilla-engine Vector2.length

메서드methodnormalize()#0.0.8 부터since 0.0.8소스source

정규화.

Normalize.

반환ReturnsVector2 길이를 1 로 맞춘 새 벡터. (길이가 0 이면 그대로 복사한다)A new vector scaled to length 1. (copied as is if the length is 0)

짝Counterpart: vanilla-engine Vector2.normalize

메서드methodadd(other)#0.0.8 부터since 0.0.8소스source

더하기.

Add.

otherVector2 | number
더할 벡터나 수. (수면 두 성분에 같은 수를 더한다, 그 밖의 꼴이면 오류를 던진다)Vector or number to add. (a number is added to both components, other types throw an error)

반환ReturnsVector2 계산한 새 벡터. (자신은 바뀌지 않는다)A new computed vector. (this vector is not changed)

짝Counterpart: vanilla-engine Vector2.add

메서드methodsubtract(other)#0.0.8 부터since 0.0.8소스source

빼기.

Subtract.

otherVector2 | number
뺄 벡터나 수. (수면 두 성분에 같은 수를 뺀다, 그 밖의 꼴이면 오류를 던진다)Vector or number to subtract. (a number is subtracted from both components, other types throw an error)

반환ReturnsVector2 계산한 새 벡터. (자신은 바뀌지 않는다)A new computed vector. (this vector is not changed)

짝Counterpart: vanilla-engine Vector2.subtract

메서드methodmultiply(other)#0.0.8 부터since 0.0.8소스source

곱하기.

Multiply.

otherVector2 | number
곱할 벡터나 수. (수면 두 성분에 같은 수를 곱한다, 그 밖의 꼴이면 오류를 던진다)Vector or number to multiply by. (both components are multiplied by a number, other types throw an error)

반환ReturnsVector2 계산한 새 벡터. (자신은 바뀌지 않는다)A new computed vector. (this vector is not changed)

짝Counterpart: vanilla-engine Vector2.multiply

메서드methoddivide(other)#0.0.8 부터since 0.0.8소스source

나누기.

Divide.

otherVector2 | number
나눌 벡터나 수. (수면 두 성분을 같은 수로 나눈다, 그 밖의 꼴이면 오류를 던진다)Vector or number to divide by. (both components are divided by a number, other types throw an error)

반환ReturnsVector2 계산한 새 벡터. (자신은 바뀌지 않는다)A new computed vector. (this vector is not changed)

짝Counterpart: vanilla-engine Vector2.divide

메서드methodmodulo(other)#0.0.8 부터since 0.0.8소스source

나머지.

Modulo.

otherVector2 | number
나눌 벡터나 수. (나머지를 구한다, 수면 두 성분을 같은 수로 나누고, 그 밖의 꼴이면 오류를 던진다)Vector or number to divide by. (takes the remainder, both components are divided by a number, other types throw an error)

반환ReturnsVector2 계산한 새 벡터. (자신은 바뀌지 않는다)A new computed vector. (this vector is not changed)

짝Counterpart: vanilla-engine Vector2.modulo

메서드methodaddScalar(scalar)#0.4.0 부터since 0.4.0소스source

수 더하기. (모든 성분에 같은 수, add(number) 와 같다)

Adds a number. (Same number to every component, same as add(number))

scalarnumber
더할 수.Number to add.

반환ReturnsVector2 계산한 새 벡터.A new computed vector.

짝Counterpart: vanilla-engine Vector2.add_scalar

메서드methodsubtractScalar(scalar)#0.4.0 부터since 0.4.0소스source

수 빼기. (모든 성분에 같은 수, subtract(number) 와 같다)

Subtracts a number. (Same number from every component, same as subtract(number))

scalarnumber
뺄 수.Number to subtract.

반환ReturnsVector2 계산한 새 벡터.A new computed vector.

짝Counterpart: vanilla-engine Vector2.subtract_scalar

메서드methodmultiplyScalar(scalar)#0.4.0 부터since 0.4.0소스source

수 곱하기. (모든 성분에 같은 수, multiply(number) 와 같다)

Multiplies by a number. (Same number for every component, same as multiply(number))

scalarnumber
곱할 수.Number to multiply by.

반환ReturnsVector2 계산한 새 벡터.A new computed vector.

짝Counterpart: vanilla-engine Vector2.multiply_scalar

메서드methoddivideScalar(scalar)#0.4.0 부터since 0.4.0소스source

수 나누기. (모든 성분에 같은 수, divide(number) 와 같다)

Divides by a number. (Same number for every component, same as divide(number))

scalarnumber
나눌 수.Number to divide by.

반환ReturnsVector2 계산한 새 벡터.A new computed vector.

짝Counterpart: vanilla-engine Vector2.divide_scalar

메서드methodmoduloScalar(scalar)#0.4.0 부터since 0.4.0소스source

수 나머지. (모든 성분에 같은 수, modulo(number) 와 같다)

Modulo by a number. (Same number for every component, same as modulo(number))

scalarnumber
나눌 수. (나머지를 구한다)Number to divide by. (takes the remainder)

반환ReturnsVector2 계산한 새 벡터.A new computed vector.

짝Counterpart: vanilla-engine Vector2.modulo_scalar

메서드methodhalf()#0.0.8 부터since 0.0.8소스source

절반.

Half.

반환ReturnsVector2 x, y 를 2 로 나눈 새 벡터.A new vector with x and y divided by 2.

짝Counterpart: vanilla-engine Vector2.half

메서드methoddot(other)#0.0.8 부터since 0.0.8소스source

내적.

Dot product.

otherVector2
내적할 벡터.Vector for the dot product.

반환Returnsnumber 내적 값.Dot product.

짝Counterpart: vanilla-engine Vector2.dot

메서드methodcross(other)#0.0.8 부터since 0.0.8소스source

외적 (2D cross product -> 스칼라 반환).

Cross product (2D cross product -> returns a scalar).

otherVector2
외적할 벡터.Vector for the cross product.

반환Returnsnumber 외적 값. (this.x * other.y - this.y * other.x)Cross product. (this.x * other.y - this.y * other.x)

짝Counterpart: vanilla-engine Vector2.cross

메서드methoddistance(other)#0.0.8 부터since 0.0.8소스source

두 벡터 사이의 거리.

Distance between two vectors.

otherVector2
거리를 잴 벡터.Vector to measure the distance to.

반환Returnsnumber 두 점 사이의 거리.Distance between the two points.

짝Counterpart: vanilla-engine Vector2.distance

메서드methodlengthSquared()#0.0.8 부터since 0.0.8소스source

길이의 제곱 (루트 연산 최적화).

Squared length (square root optimization).

반환Returnsnumber 길이의 제곱.Squared length.

짝Counterpart: vanilla-engine Vector2.length_squared

메서드methodangle(other)#0.0.8 부터since 0.0.8소스source

두 벡터 사이의 각도 반환 (라디안).

Returns the angle between two vectors (radians).

otherVector2
각도를 잴 벡터.Vector to measure the angle to.

반환Returnsnumber 두 벡터 사이의 각도. (라디안, 0 ~ π, 어느 한쪽 길이가 0 이면 NaN)Angle between the two vectors. (radians, 0 ~ π, NaN if either length is 0)

짝Counterpart: vanilla-engine Vector2.angle

예제Example두 벡터 사이의 각도를 도로 보기View the angle between two vectors in degrees
import { Vector2, Math } from "./build/vanilla.js";

const right = Vector2.create(1, 0);
const up = Vector2.create(0, -1);
const radian = right.angle(up);
console.log(radian, Math.radianToDegree(radian));