vanilladocs
vanillapunch.com
API / base/vector2

base/vector2

2차원 벡터. (원본 base/vector2.js)

  • 원본 add / subtract / multiply / divide / modulo 는 수와 Vector2 를 다 받는다. 러스트 판은 Vector2 를 받는 것과 수를 받는 ..._scalar 로 나눴다. 수는 원본 자바스크립트 수처럼 f64 다.
  • lerp 는 원본 math.lerp 식(from * (1 - t) + to * t)을 그대로 쓴다. (Vector3 와 같다)
  • 원본에 있던 버그 둘(modulo 가 나머지 대신 나누기를 하고, half 가 반 대신 두 배(0.5 로 나누기)를 돌려주던 것)을 두 엔진에서 같이 고쳤다. modulo 는 나머지(%)를, half 는 반을 돌려준다. (vanilla.js 와 게임에서 둘을 쓰는 곳은 없다)

2D vector. (the original base/vector2.js)

  • The original add / subtract / multiply / divide / modulo accept both numbers and Vector2. The Rust version splits them into ones taking Vector2 and ..._scalar ones taking numbers. Numbers are f64 like the original JavaScript numbers.
  • lerp uses the original math.lerp formula (from * (1 - t) + to * t) as is. (same as Vector3)
  • Two bugs in the original (modulo dividing instead of taking the remainder, and half returning double (dividing by 0.5) instead of half) were fixed together in both engines. modulo returns the remainder (%), and half returns half. (nothing in vanilla.js or the games uses either)

Vector2struct traits Clone, Copy, Debug, Default, PartialEq, Add, Sub, Mul<f64>, AddAssign, SubAssign, GeometryKeyValue, ToReflectValue, FromReflectValue<'a>

짝Counterpart: vanilla.js Vector2소스source

2차원 벡터. (x, y 를 공개 필드로 둔 Copy 값, 계산 함수는 자신을 바꾸지 않고 새 벡터를 돌려준다)

2D vector. (a Copy value with x, y as public fields; computing functions return a new vector without changing self)

예제Example만들고 더하고 길이 재기Create, add and measure length
use vanilla::Vector2;

let start = Vector2::create(1.0, 2.0);
let velocity = Vector2::create(3.0, 4.0);
let moved = start.add(velocity);
assert_eq!(moved, Vector2::create(4.0, 6.0));
assert_eq!(velocity.length(), 5.0);
assert_eq!(start + velocity, moved);

let mut position = Vector2::zero();
position += velocity * 2.0;
assert_eq!(position, Vector2::create(6.0, 8.0));
정적staticpub const fn create(x: f64, y: f64) -> Vector2#0.3.2 부터since 0.3.2소스source

생성.

Constructor.

xf64
x 성분.x component.
yf64
y 성분.y component.

반환ReturnsVector2 (x, y) 벡터.The vector (x, y).

짝Counterpart: vanilla.js Vector2.create

정적staticpub const fn zero() -> Vector2#0.3.2 부터since 0.3.2소스source

(0, 0).

반환ReturnsVector2 두 성분이 0 인 벡터.A vector with both components 0.

짝Counterpart: vanilla.js Vector2.zero

정적staticpub const fn one() -> Vector2#0.3.2 부터since 0.3.2소스source

(1, 1).

반환ReturnsVector2 두 성분이 1 인 벡터.A vector with both components 1.

짝Counterpart: vanilla.js Vector2.one

정적staticpub const fn minus_one() -> Vector2#0.4.0 부터since 0.4.0소스source

(-1, -1).

반환ReturnsVector2 두 성분이 -1 인 벡터.A vector with both components -1.

짝Counterpart: vanilla.js Vector2.minusOne

정적staticpub const fn positive_infinity() -> Vector2#0.4.0 부터since 0.4.0소스source

(+무한대, +무한대).

(+infinity, +infinity).

반환ReturnsVector2 두 성분이 f64::INFINITY 인 벡터.A vector with both components f64::INFINITY.

짝Counterpart: vanilla.js Vector2.positiveInfinity

정적staticpub const fn negative_infinity() -> Vector2#0.4.0 부터since 0.4.0소스source

(-무한대, -무한대).

(-infinity, -infinity).

반환ReturnsVector2 두 성분이 f64::NEG_INFINITY 인 벡터.A vector with both components f64::NEG_INFINITY.

짝Counterpart: vanilla.js Vector2.negativeInfinity

정적staticpub fn lerp(from: Vector2, to: Vector2, normalized_time: f64) -> Vector2#0.4.0 부터since 0.4.0소스source

선형 보간. (원본 math.lerp 식: from * (1 - t) + to * t)

Linear interpolation. (the original math.lerp formula: from * (1 - t) + to * t)

fromVector2
시작 벡터.Start vector.
toVector2
끝 벡터.End vector.
normalized_timef64
보간 비율. (0 이면 from, 1 이면 to, 0 ~ 1 밖도 자르지 않고 늘여 잇는다)Interpolation ratio. (0 is from, 1 is to; values outside 0 ~ 1 are not clamped and extrapolate)

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

짝Counterpart: vanilla.js Vector2.lerp

예제Example4 분의 1 지점The quarter point
use vanilla::Vector2;

let from = Vector2::create(0.0, 10.0);
let to = Vector2::create(8.0, -6.0);
assert_eq!(Vector2::lerp(from, to, 0.25), Vector2::create(2.0, 6.0));
assert_eq!(Vector2::lerp(from, to, 1.0), to);
정적staticpub fn clamp(value: Vector2, minimum: Vector2, maximum: Vector2) -> Vector2#0.4.0 부터since 0.4.0소스source

성분별 범위 제한.

Clamps per component.

valueVector2
제한할 벡터.Vector to clamp.
minimumVector2
성분별 최소값.Per-component minimum.
maximumVector2
성분별 최대값.Per-component maximum.

반환ReturnsVector2 성분마다 minimum ~ maximum 으로 자른 새 벡터. (math::clamp 를 성분마다 쓴다)A new vector with each component clamped to minimum ~ maximum. (uses math::clamp per component)

짝Counterpart: vanilla.js Vector2.clamp

예제Example범위 안으로 자르기Clamp into a range
use vanilla::Vector2;

let clamped = Vector2::clamp(Vector2::create(-5.0, 50.0), Vector2::zero(), Vector2::create(10.0, 10.0));
assert_eq!(clamped, Vector2::create(0.0, 10.0));
필드fieldpub x: f64#0.3.2 부터since 0.3.2소스source

x 성분.

x component.

반환Returnsf64

짝Counterpart: vanilla.js Vector2.x

필드fieldpub y: f64#0.3.2 부터since 0.3.2소스source

y 성분.

y component.

반환Returnsf64

짝Counterpart: vanilla.js Vector2.y

메서드methodpub fn set(&mut self, x: f64, y: f64)#0.3.2 부터since 0.3.2소스source

값 설정.

Sets the value.

xf64
넣을 x 성분.x component to set.
yf64
넣을 y 성분.y component to set.

짝Counterpart: vanilla.js Vector2.set

메서드methodpub fn get_x(&self) -> f64#0.4.0 부터since 0.4.0소스source

x 반환.

Returns x.

반환Returnsf64 x 성분.x component.

짝Counterpart: vanilla.js Vector2.getX

메서드methodpub fn get_y(&self) -> f64#0.4.0 부터since 0.4.0소스source

y 반환.

Returns y.

반환Returnsf64 y 성분.y component.

짝Counterpart: vanilla.js Vector2.getY

메서드methodpub fn add(&self, other: Vector2) -> Vector2#0.3.2 부터since 0.3.2소스source

더하기.

Add.

otherVector2
더할 벡터.Vector to add.

반환ReturnsVector2 성분별로 더한 새 벡터. (자신은 바뀌지 않는다)A new vector added per component. (self is not changed)

짝Counterpart: vanilla.js Vector2.add

메서드methodpub fn add_scalar(&self, scalar: f64) -> Vector2#0.4.0 부터since 0.4.0소스source

수 더하기. (두 성분에 같이)

Adds a number. (to both components)

scalarf64
두 성분에 더할 수.Number to add to both components.

반환ReturnsVector2 더한 새 벡터.The new added vector.

짝Counterpart: vanilla.js Vector2.addScalar

메서드methodpub fn subtract(&self, other: Vector2) -> Vector2#0.3.2 부터since 0.3.2소스source

빼기.

Subtract.

otherVector2
뺄 벡터.Vector to subtract.

반환ReturnsVector2 성분별로 뺀 새 벡터. (self - other)A new vector subtracted per component. (self - other)

짝Counterpart: vanilla.js Vector2.subtract

메서드methodpub fn subtract_scalar(&self, scalar: f64) -> Vector2#0.4.0 부터since 0.4.0소스source

수 빼기. (두 성분에서 같이)

Subtracts a number. (from both components)

scalarf64
두 성분에서 뺄 수.Number to subtract from both components.

반환ReturnsVector2 뺀 새 벡터.The new subtracted vector.

짝Counterpart: vanilla.js Vector2.subtractScalar

메서드methodpub fn multiply(&self, other: Vector2) -> Vector2#0.3.2 부터since 0.3.2소스source

성분별 곱하기.

Per-component multiplication.

otherVector2
성분별로 곱할 벡터.Vector to multiply per component.

반환ReturnsVector2 성분별로 곱한 새 벡터.A new vector multiplied per component.

짝Counterpart: vanilla.js Vector2.multiply

메서드methodpub fn multiply_scalar(&self, scalar: f64) -> Vector2#0.3.2 부터since 0.3.2소스source

스칼라 곱하기.

Scalar multiplication.

scalarf64
두 성분에 곱할 수.Number to multiply both components by.

반환ReturnsVector2 곱한 새 벡터.The new multiplied vector.

짝Counterpart: vanilla.js Vector2.multiplyScalar

메서드methodpub fn divide(&self, other: Vector2) -> Vector2#0.4.0 부터since 0.4.0소스source

성분별 나누기.

Per-component division.

otherVector2
성분별로 나눌 벡터. (0 인 성분은 무한대나 NaN 이 된다)Vector to divide by per component. (components that are 0 give infinity or NaN)

반환ReturnsVector2 성분별로 나눈 새 벡터.A new vector divided per component.

짝Counterpart: vanilla.js Vector2.divide

메서드methodpub fn divide_scalar(&self, scalar: f64) -> Vector2#0.4.0 부터since 0.4.0소스source

스칼라 나누기.

Scalar division.

scalarf64
두 성분을 나눌 수. (0 이면 무한대나 NaN 이 된다)Number to divide both components by. (0 gives infinity or NaN)

반환ReturnsVector2 나눈 새 벡터.The new divided vector.

짝Counterpart: vanilla.js Vector2.divideScalar

메서드methodpub fn modulo(&self, other: Vector2) -> Vector2#0.4.0 부터since 0.4.0소스source

성분별 나머지. (부호는 나뉘는 쪽을 따른다, 원본 % 와 같다)

Per-component remainder. (the sign follows the dividend, same as the original %)

otherVector2
성분별로 나눌 벡터.Vector to divide by per component.

반환ReturnsVector2 성분별 나머지를 담은 새 벡터.A new vector holding the per-component remainders.

짝Counterpart: vanilla.js Vector2.modulo

예제Example음수의 나머지는 음수The remainder of a negative is negative
use vanilla::Vector2;

let remainder = Vector2::create(7.0, -9.0).modulo(Vector2::create(4.0, 4.0));
assert_eq!(remainder, Vector2::create(3.0, -1.0));
메서드methodpub fn modulo_scalar(&self, scalar: f64) -> Vector2#0.4.0 부터since 0.4.0소스source

스칼라 나머지.

Scalar remainder.

scalarf64
두 성분을 나눌 수.Number to divide both components by.

반환ReturnsVector2 성분별 나머지를 담은 새 벡터.A new vector holding the per-component remainders.

짝Counterpart: vanilla.js Vector2.moduloScalar

메서드methodpub fn half(&self) -> Vector2#0.4.0 부터since 0.4.0소스source

반.

Half.

반환ReturnsVector2 두 성분에 0.5 를 곱한 새 벡터.A new vector with both components multiplied by 0.5.

짝Counterpart: vanilla.js Vector2.half

메서드methodpub fn length(&self) -> f64#0.3.2 부터since 0.3.2소스source

길이.

Length.

반환Returnsf64 벡터의 길이. (0 이상)Length of the vector. (0 or more)

짝Counterpart: vanilla.js Vector2.length

메서드methodpub fn length_squared(&self) -> f64#0.4.0 부터since 0.4.0소스source

길이의 제곱.

Squared length.

반환Returnsf64 x * x + y * y. (제곱근을 셈하지 않는다)x * x + y * y. (no square root is computed)

짝Counterpart: vanilla.js Vector2.lengthSquared

메서드methodpub fn distance(&self, other: Vector2) -> f64#0.3.2 부터since 0.3.2소스source

두 점 사이 거리.

Distance between two points.

otherVector2
거리를 잴 점.Point to measure the distance to.

반환Returnsf64 두 점 사이 거리. (0 이상)Distance between the two points. (0 or more)

짝Counterpart: vanilla.js Vector2.distance

메서드methodpub fn normalize(&self) -> Vector2#0.3.2 부터since 0.3.2소스source

단위 벡터. (길이가 0 이면 (0, 0))

Unit vector. ((0, 0) if the length is 0)

반환ReturnsVector2 길이가 1 인 새 벡터. 길이가 0 이하이면 (0, 0).A new vector of length 1. (0, 0) if the length is 0 or less.

짝Counterpart: vanilla.js Vector2.normalize

예제Example방향만 남기기Keep only the direction
use vanilla::Vector2;

let direction = Vector2::create(3.0, 4.0).normalize();
assert_eq!(direction, Vector2::create(0.6, 0.8));
assert_eq!(Vector2::zero().normalize(), Vector2::zero());
메서드methodpub fn dot(&self, other: Vector2) -> f64#0.3.2 부터since 0.3.2소스source

내적.

Dot product.

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

반환Returnsf64 내적 값. (self.x * other.x + self.y * other.y)Dot product. (self.x * other.x + self.y * other.y)

짝Counterpart: vanilla.js Vector2.dot

메서드methodpub fn cross(&self, other: Vector2) -> f64#0.4.0 부터since 0.4.0소스source

외적의 z. (self.x * other.y - self.y * other.x, 양수면 other 가 반시계 쪽)

z of the cross product. (self.x * other.y - self.y * other.x, positive if other is counterclockwise)

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

반환Returnsf64 외적의 z 값.z value of the cross product.

짝Counterpart: vanilla.js Vector2.cross

예제Example순서를 바꾸면 부호가 바뀐다Swapping the order flips the sign
use vanilla::Vector2;

let axis_x = Vector2::create(1.0, 0.0);
let axis_y = Vector2::create(0.0, 1.0);
assert_eq!(axis_x.cross(axis_y), 1.0);
assert_eq!(axis_y.cross(axis_x), -1.0);
메서드methodpub fn angle(&self, other: Vector2) -> f64#0.4.0 부터since 0.4.0소스source

두 벡터 사이 각. (라디안, 0 ~ PI, 길이가 0 인 벡터면 NaN)

Angle between two vectors. (radians, 0 ~ PI, NaN for a zero-length vector)

otherVector2
사이 각을 잴 벡터.Vector to measure the angle with.

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

짝Counterpart: vanilla.js Vector2.angle

예제Example직각과 길이 0 인 벡터A right angle and a zero-length vector
use vanilla::{math, Vector2};

let angle = Vector2::create(1.0, 0.0).angle(Vector2::create(0.0, 2.0));
assert!((angle - math::PI * 0.5).abs() < 1e-6);
assert!(Vector2::zero().angle(Vector2::one()).is_nan());