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>
2차원 벡터. (x, y 를 공개 필드로 둔 Copy 값, 계산 함수는 자신을 바꾸지 않고 새 벡터를 돌려준다)
2D vector. (a Copy value with x, y as public fields; computing functions return a new vector without changing self)
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));생성.
Constructor.
- xf64
- x 성분.x component.
- yf64
- y 성분.y component.
반환ReturnsVector2 (x, y) 벡터.The vector (x, y).
짝Counterpart: vanilla.js Vector2.create
(0, 0).
반환ReturnsVector2 두 성분이 0 인 벡터.A vector with both components 0.
짝Counterpart: vanilla.js Vector2.zero
(1, 1).
반환ReturnsVector2 두 성분이 1 인 벡터.A vector with both components 1.
짝Counterpart: vanilla.js Vector2.one
(-1, -1).
반환ReturnsVector2 두 성분이 -1 인 벡터.A vector with both components -1.
짝Counterpart: vanilla.js Vector2.minusOne
(+무한대, +무한대).
(+infinity, +infinity).
반환ReturnsVector2 두 성분이 f64::INFINITY 인 벡터.A vector with both components f64::INFINITY.
짝Counterpart: vanilla.js Vector2.positiveInfinity
(-무한대, -무한대).
(-infinity, -infinity).
반환ReturnsVector2 두 성분이 f64::NEG_INFINITY 인 벡터.A vector with both components f64::NEG_INFINITY.
짝Counterpart: vanilla.js Vector2.negativeInfinity
선형 보간. (원본 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
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);성분별 범위 제한.
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
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));값 설정.
Sets the value.
- xf64
- 넣을 x 성분.x component to set.
- yf64
- 넣을 y 성분.y component to set.
짝Counterpart: vanilla.js Vector2.set
더하기.
Add.
- otherVector2
- 더할 벡터.Vector to add.
반환ReturnsVector2 성분별로 더한 새 벡터. (자신은 바뀌지 않는다)A new vector added per component. (self is not changed)
짝Counterpart: vanilla.js Vector2.add
수 더하기. (두 성분에 같이)
Adds a number. (to both components)
- scalarf64
- 두 성분에 더할 수.Number to add to both components.
반환ReturnsVector2 더한 새 벡터.The new added vector.
짝Counterpart: vanilla.js Vector2.addScalar
빼기.
Subtract.
- otherVector2
- 뺄 벡터.Vector to subtract.
반환ReturnsVector2 성분별로 뺀 새 벡터. (self - other)A new vector subtracted per component. (self - other)
짝Counterpart: vanilla.js Vector2.subtract
수 빼기. (두 성분에서 같이)
Subtracts a number. (from both components)
- scalarf64
- 두 성분에서 뺄 수.Number to subtract from both components.
반환ReturnsVector2 뺀 새 벡터.The new subtracted vector.
짝Counterpart: vanilla.js Vector2.subtractScalar
성분별 곱하기.
Per-component multiplication.
- otherVector2
- 성분별로 곱할 벡터.Vector to multiply per component.
반환ReturnsVector2 성분별로 곱한 새 벡터.A new vector multiplied per component.
짝Counterpart: vanilla.js Vector2.multiply
스칼라 곱하기.
Scalar multiplication.
- scalarf64
- 두 성분에 곱할 수.Number to multiply both components by.
반환ReturnsVector2 곱한 새 벡터.The new multiplied vector.
짝Counterpart: vanilla.js Vector2.multiplyScalar
성분별 나누기.
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
스칼라 나누기.
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
성분별 나머지. (부호는 나뉘는 쪽을 따른다, 원본 % 와 같다)
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
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));스칼라 나머지.
Scalar remainder.
- scalarf64
- 두 성분을 나눌 수.Number to divide both components by.
반환ReturnsVector2 성분별 나머지를 담은 새 벡터.A new vector holding the per-component remainders.
짝Counterpart: vanilla.js Vector2.moduloScalar
반.
Half.
반환ReturnsVector2 두 성분에 0.5 를 곱한 새 벡터.A new vector with both components multiplied by 0.5.
짝Counterpart: vanilla.js Vector2.half
길이.
Length.
반환Returnsf64 벡터의 길이. (0 이상)Length of the vector. (0 or more)
짝Counterpart: vanilla.js Vector2.length
길이의 제곱.
Squared length.
반환Returnsf64 x * x + y * y. (제곱근을 셈하지 않는다)x * x + y * y. (no square root is computed)
짝Counterpart: vanilla.js Vector2.lengthSquared
두 점 사이 거리.
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
단위 벡터. (길이가 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
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());내적.
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
외적의 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
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);두 벡터 사이 각. (라디안, 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
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());