Files
esengine/packages/math/src/Vector2.ts

541 lines
12 KiB
TypeScript
Raw Normal View History

2025-08-10 16:00:02 +08:00
/**
* 2D向量类
*
* 2D向量运算功能
* -
* -
* -
* -
*/
export class Vector2 {
/** X分量 */
public x: number;
/** Y分量 */
public y: number;
/**
* 2D向量
* @param x X分量0
* @param y Y分量0
*/
constructor(x: number = 0, y: number = 0) {
this.x = x;
this.y = y;
}
// 静态常量
/** 零向量 (0, 0) */
static readonly ZERO = new Vector2(0, 0);
/** 单位向量 (1, 1) */
static readonly ONE = new Vector2(1, 1);
/** 右方向向量 (1, 0) */
static readonly RIGHT = new Vector2(1, 0);
/** 左方向向量 (-1, 0) */
static readonly LEFT = new Vector2(-1, 0);
/** 上方向向量 (0, 1) */
static readonly UP = new Vector2(0, 1);
/** 下方向向量 (0, -1) */
static readonly DOWN = new Vector2(0, -1);
// 基础属性
/**
*
*/
get length(): number {
return Math.sqrt(this.x * this.x + this.y * this.y);
}
/**
*
*/
get lengthSquared(): number {
return this.x * this.x + this.y * this.y;
}
/**
*
*/
get angle(): number {
return Math.atan2(this.y, this.x);
}
/**
*
*/
get isZero(): boolean {
return this.x === 0 && this.y === 0;
}
/**
*
*/
get isUnit(): boolean {
const lenSq = this.lengthSquared;
return Math.abs(lenSq - 1) < Number.EPSILON;
}
// 基础运算
/**
*
* @param x X分量
* @param y Y分量
* @returns
*/
set(x: number, y: number): this {
this.x = x;
this.y = y;
return this;
}
/**
*
* @param other
* @returns
*/
copy(other: Vector2): this {
this.x = other.x;
this.y = other.y;
return this;
}
/**
*
* @returns
*/
clone(): Vector2 {
return new Vector2(this.x, this.y);
}
/**
*
* @param other
* @returns
*/
add(other: Vector2): this {
this.x += other.x;
this.y += other.y;
return this;
}
/**
*
* @param other
* @returns
*/
subtract(other: Vector2): this {
this.x -= other.x;
this.y -= other.y;
return this;
}
/**
*
* @param scalar
* @returns
*/
multiply(scalar: number): this {
this.x *= scalar;
this.y *= scalar;
return this;
}
/**
*
* @param scalar
* @returns
*/
divide(scalar: number): this {
if (scalar === 0) {
throw new Error('不能除以零');
}
this.x /= scalar;
this.y /= scalar;
return this;
}
/**
*
* @returns
*/
negate(): this {
this.x = -this.x;
this.y = -this.y;
return this;
}
// 向量运算
/**
*
* @param other
* @returns
*/
dot(other: Vector2): number {
return this.x * other.x + this.y * other.y;
}
/**
* 2D中返回标量
* @param other
* @returns
*/
cross(other: Vector2): number {
return this.x * other.y - this.y * other.x;
}
/**
*
* @returns
*/
normalize(): this {
const len = this.length;
if (len === 0) {
return this;
}
return this.divide(len);
}
/**
*
* @returns
*/
normalized(): Vector2 {
return this.clone().normalize();
}
// 几何运算
/**
*
* @param other
* @returns
*/
distanceTo(other: Vector2): number {
const dx = this.x - other.x;
const dy = this.y - other.y;
return Math.sqrt(dx * dx + dy * dy);
}
/**
*
* @param other
* @returns
*/
distanceToSquared(other: Vector2): number {
const dx = this.x - other.x;
const dy = this.y - other.y;
return dx * dx + dy * dy;
}
/**
*
* @param other
* @returns 0π
*/
angleTo(other: Vector2): number {
const dot = this.dot(other);
const lenProduct = this.length * other.length;
if (lenProduct === 0) return 0;
return Math.acos(Math.max(-1, Math.min(1, dot / lenProduct)));
}
/**
*
* @param onto
* @returns
*/
projectOnto(onto: Vector2): Vector2 {
const dot = this.dot(onto);
const lenSq = onto.lengthSquared;
if (lenSq === 0) return new Vector2();
return onto.clone().multiply(dot / lenSq);
}
/**
*
* @param onto
* @returns
*/
projectOntoLength(onto: Vector2): number {
const len = onto.length;
if (len === 0) return 0;
return this.dot(onto) / len;
}
/**
* 90
* @returns
*/
perpendicular(): Vector2 {
return new Vector2(-this.y, this.x);
}
// 变换操作
/**
*
* @param angle
* @returns
*/
rotate(angle: number): this {
const cos = Math.cos(angle);
const sin = Math.sin(angle);
const x = this.x * cos - this.y * sin;
const y = this.x * sin + this.y * cos;
this.x = x;
this.y = y;
return this;
}
/**
*
* @param angle
* @returns
*/
rotated(angle: number): Vector2 {
return this.clone().rotate(angle);
}
/**
*
* @param center
* @param angle
* @returns
*/
rotateAround(center: Vector2, angle: number): this {
return this.subtract(center).rotate(angle).add(center);
}
/**
* 线
* @param normal 线
* @returns
*/
reflect(normal: Vector2): this {
const dot = this.dot(normal);
this.x -= 2 * dot * normal.x;
this.y -= 2 * dot * normal.y;
return this;
}
/**
*
* @param normal 线
* @returns
*/
reflected(normal: Vector2): Vector2 {
return this.clone().reflect(normal);
}
// 插值和限制
/**
* 线
* @param target
* @param t 01
* @returns
*/
lerp(target: Vector2, t: number): this {
this.x += (target.x - this.x) * t;
this.y += (target.y - this.y) * t;
return this;
}
/**
*
* @param maxLength
* @returns
*/
clampLength(maxLength: number): this {
const lenSq = this.lengthSquared;
if (lenSq > maxLength * maxLength) {
return this.normalize().multiply(maxLength);
}
return this;
}
/**
*
* @param min
* @param max
* @returns
*/
clamp(min: Vector2, max: Vector2): this {
this.x = Math.max(min.x, Math.min(max.x, this.x));
this.y = Math.max(min.y, Math.min(max.y, this.y));
return this;
}
// 比较操作
/**
*
* @param other
* @param epsilon Number.EPSILON
* @returns
*/
equals(other: Vector2, epsilon: number = Number.EPSILON): boolean {
return Math.abs(this.x - other.x) < epsilon &&
Math.abs(this.y - other.y) < epsilon;
}
/**
*
* @param other
* @returns
*/
exactEquals(other: Vector2): boolean {
return this.x === other.x && this.y === other.y;
}
// 静态方法
/**
*
* @param a a
* @param b b
* @returns
*/
static add(a: Vector2, b: Vector2): Vector2 {
return new Vector2(a.x + b.x, a.y + b.y);
}
/**
*
* @param a a
* @param b b
* @returns
*/
static subtract(a: Vector2, b: Vector2): Vector2 {
return new Vector2(a.x - b.x, a.y - b.y);
}
/**
*
* @param vector
* @param scalar
* @returns
*/
static multiply(vector: Vector2, scalar: number): Vector2 {
return new Vector2(vector.x * scalar, vector.y * scalar);
}
/**
*
* @param a a
* @param b b
* @returns
*/
static dot(a: Vector2, b: Vector2): number {
return a.x * b.x + a.y * b.y;
}
/**
*
* @param a a
* @param b b
* @returns
*/
static cross(a: Vector2, b: Vector2): number {
return a.x * b.y - a.y * b.x;
}
/**
*
* @param a a
* @param b b
* @returns
*/
static distance(a: Vector2, b: Vector2): number {
const dx = a.x - b.x;
const dy = a.y - b.y;
return Math.sqrt(dx * dx + dy * dy);
}
/**
* 线
* @param a
* @param b
* @param t 01
* @returns
*/
static lerp(a: Vector2, b: Vector2, t: number): Vector2 {
return new Vector2(
a.x + (b.x - a.x) * t,
a.y + (b.y - a.y) * t
);
}
/**
*
* @param angle
* @returns
*/
static fromAngle(angle: number): Vector2 {
return new Vector2(Math.cos(angle), Math.sin(angle));
}
/**
*
* @param length
* @param angle
* @returns
*/
static fromPolar(length: number, angle: number): Vector2 {
return new Vector2(length * Math.cos(angle), length * Math.sin(angle));
}
/**
*
* @param a a
* @param b b
* @returns
*/
static min(a: Vector2, b: Vector2): Vector2 {
return new Vector2(Math.min(a.x, b.x), Math.min(a.y, b.y));
}
/**
*
* @param a a
* @param b b
* @returns
*/
static max(a: Vector2, b: Vector2): Vector2 {
return new Vector2(Math.max(a.x, b.x), Math.max(a.y, b.y));
}
// 字符串转换
/**
*
* @returns
*/
toString(): string {
return `Vector2(${this.x.toFixed(3)}, ${this.y.toFixed(3)})`;
}
/**
*
* @returns [x, y]
*/
toArray(): [number, number] {
return [this.x, this.y];
}
/**
*
* @returns {x, y}
*/
toObject(): { x: number; y: number } {
return { x: this.x, y: this.y };
}
}