框架优化
This commit is contained in:
@@ -44,8 +44,8 @@ module es {
|
||||
* @param t
|
||||
*/
|
||||
public static getFirstDerivative(p0: Vector2, p1: Vector2, p2: Vector2, t: number) {
|
||||
return new Vector2(2 * (1 - t)).multiply(Vector2.subtract(p1, p0))
|
||||
.addEqual(new Vector2(2 * t).multiply(Vector2.subtract(p2, p1)));
|
||||
return p1.sub(p0).scale(2 * (1 - t))
|
||||
.addEqual(p2.sub(p1).scale(2 * t));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -60,9 +60,9 @@ module es {
|
||||
end: Vector2, t: number) {
|
||||
t = MathHelper.clamp01(t);
|
||||
let oneMunusT = 1 - t;
|
||||
return new Vector2(3 * oneMunusT * oneMunusT).multiply(Vector2.subtract(firstControlPoint, start))
|
||||
.addEqual(new Vector2(6 * oneMunusT * t).multiply(Vector2.subtract(secondControlPoint, firstControlPoint)))
|
||||
.addEqual(new Vector2(3 * t * t).multiply(Vector2.subtract(end, secondControlPoint)));
|
||||
return firstControlPoint.sub(start).scale(3 * oneMunusT * oneMunusT)
|
||||
.addEqual(secondControlPoint.sub(firstControlPoint).scale(6 * oneMunusT * t))
|
||||
.addEqual(end.sub(secondControlPoint).scale(3 * t * t));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -96,19 +96,19 @@ module es {
|
||||
private static recursiveGetOptimizedDrawingPoints(start: Vector2, firstCtrlPoint: Vector2, secondCtrlPoint: Vector2,
|
||||
end: Vector2, points: Vector2[], distanceTolerance: number) {
|
||||
// 计算线段的所有中点
|
||||
let pt12 = Vector2.divideScaler(Vector2.add(start, firstCtrlPoint), 2);
|
||||
let pt23 = Vector2.divideScaler(Vector2.add(firstCtrlPoint, secondCtrlPoint), 2);
|
||||
let pt34 = Vector2.divideScaler(Vector2.add(secondCtrlPoint, end), 2);
|
||||
let pt12 = Vector2.divideScaler(start.add(firstCtrlPoint), 2);
|
||||
let pt23 = Vector2.divideScaler(firstCtrlPoint.add(secondCtrlPoint), 2);
|
||||
let pt34 = Vector2.divideScaler(secondCtrlPoint.add(end), 2);
|
||||
|
||||
// 计算新半直线的中点
|
||||
let pt123 = Vector2.divideScaler(Vector2.add(pt12, pt23), 2);
|
||||
let pt234 = Vector2.divideScaler(Vector2.add(pt23, pt34), 2);
|
||||
let pt123 = Vector2.divideScaler(pt12.add(pt23), 2);
|
||||
let pt234 = Vector2.divideScaler(pt23.add(pt34), 2);
|
||||
|
||||
// 最后再细分最后两个中点。如果我们满足我们的距离公差,这将是我们使用的最后一点。
|
||||
let pt1234 = Vector2.divideScaler(Vector2.add(pt123, pt234), 2);
|
||||
let pt1234 = Vector2.divideScaler(pt123.add(pt234), 2);
|
||||
|
||||
// 试着用一条直线来近似整个三次曲线
|
||||
let deltaLine = Vector2.subtract(end, start);
|
||||
let deltaLine = end.sub(start);
|
||||
|
||||
let d2 = Math.abs(((firstCtrlPoint.x, end.x) * deltaLine.y - (firstCtrlPoint.y - end.y) * deltaLine.x));
|
||||
let d3 = Math.abs(((secondCtrlPoint.x - end.x) * deltaLine.y - (secondCtrlPoint.y - end.y) * deltaLine.x));
|
||||
|
||||
@@ -10,19 +10,20 @@ module es {
|
||||
* 在这个过程中,t被修改为在曲线段的范围内。
|
||||
* @param t
|
||||
*/
|
||||
public pointIndexAtTime(t: Ref<number>): number {
|
||||
let i = 0;
|
||||
if (t.value >= 1) {
|
||||
t.value = 1;
|
||||
i = this._points.length - 4;
|
||||
public pointIndexAtTime(t: number): {time: number, range: number} {
|
||||
const res = {time: 0, range: 0};
|
||||
if (t >= 1) {
|
||||
t = 1;
|
||||
res.range = this._points.length - 4;
|
||||
} else {
|
||||
t.value = MathHelper.clamp01(t.value) * this._curveCount;
|
||||
i = ~~t;
|
||||
t.value -= i;
|
||||
i *= 3;
|
||||
t = MathHelper.clamp01(t) * this._curveCount;
|
||||
res.range = Math.floor(t);
|
||||
t -= res.range;
|
||||
res.range *= 3;
|
||||
}
|
||||
|
||||
return i;
|
||||
res.time = t;
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -32,7 +33,7 @@ module es {
|
||||
*/
|
||||
public setControlPoint(index: number, point: Vector2) {
|
||||
if (index % 3 == 0) {
|
||||
let delta = Vector2.subtract(point, this._points[index]);
|
||||
const delta = point.sub(this._points[index]);
|
||||
if (index > 0)
|
||||
this._points[index - 1].addEqual(delta);
|
||||
|
||||
@@ -48,7 +49,8 @@ module es {
|
||||
* @param t
|
||||
*/
|
||||
public getPointAtTime(t: number): Vector2{
|
||||
let i = this.pointIndexAtTime(new Ref(t));
|
||||
const res = this.pointIndexAtTime(t);
|
||||
const i = res.range;
|
||||
return Bezier.getPointThree(this._points[i], this._points[i + 1], this._points[i + 2],
|
||||
this._points[i + 3], t);
|
||||
}
|
||||
@@ -58,7 +60,8 @@ module es {
|
||||
* @param t
|
||||
*/
|
||||
public getVelocityAtTime(t: number): Vector2 {
|
||||
let i = this.pointIndexAtTime(new Ref(t));
|
||||
const res = this.pointIndexAtTime(t);
|
||||
const i = res.range;
|
||||
return Bezier.getFirstDerivativeThree(this._points[i], this._points[i + 1], this._points[i + 2],
|
||||
this._points[i + 3], t);
|
||||
}
|
||||
@@ -68,7 +71,7 @@ module es {
|
||||
* @param t
|
||||
*/
|
||||
public getDirectionAtTime(t: number) {
|
||||
return Vector2.normalize(this.getVelocityAtTime(t));
|
||||
return this.getVelocityAtTime(t).normalize();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -390,8 +390,8 @@ module es {
|
||||
* @param other
|
||||
*/
|
||||
public static project(self: Vector2, other: Vector2) {
|
||||
let amt = Vector2.dot(self, other) / other.lengthSquared();
|
||||
let vec = new Vector2(amt * other.x, amt * other.y);
|
||||
let amt = self.dot(other) / other.lengthSquared();
|
||||
let vec = other.scale(amt);
|
||||
return vec;
|
||||
}
|
||||
|
||||
@@ -606,7 +606,7 @@ module es {
|
||||
return false;
|
||||
}
|
||||
|
||||
return !Number.isFinite(x);
|
||||
return x !== Infinity;
|
||||
}
|
||||
|
||||
public static smoothDamp(current: number, target: number, currentVelocity: number, smoothTime: number, maxSpeed: number, deltaTime: number): { value: number; currentVelocity: number } {
|
||||
@@ -671,5 +671,9 @@ module es {
|
||||
public static mapMinMax(value: number, leftMin: number, leftMax: number, rightMin: number, rightMax): number {
|
||||
return rightMin + ((MathHelper.clamp(value, leftMin, leftMax) - leftMin) * (rightMax - rightMin)) / (leftMax - leftMin);
|
||||
}
|
||||
|
||||
public static fromAngle(angle: number) {
|
||||
return new Vector2(Math.cos(angle), Math.sin(angle)).normalizeEqual();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,7 +16,28 @@ module es {
|
||||
* 返回标识矩阵
|
||||
*/
|
||||
public static get identity(): Matrix2D {
|
||||
return new Matrix2D(1, 0, 0, 1, 0, 0);
|
||||
return new Matrix2D().setIdentity();
|
||||
}
|
||||
|
||||
public setIdentity(): Matrix2D {
|
||||
return this.setValues(1, 0, 0, 1, 0, 0);
|
||||
}
|
||||
|
||||
public setValues(
|
||||
m11: number,
|
||||
m12: number,
|
||||
m21: number,
|
||||
m22: number,
|
||||
m31: number,
|
||||
m32: number
|
||||
): Matrix2D {
|
||||
this.m11 = m11;
|
||||
this.m12 = m12;
|
||||
this.m21 = m21;
|
||||
this.m22 = m22;
|
||||
this.m31 = m31;
|
||||
this.m32 = m32;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -38,7 +59,7 @@ module es {
|
||||
return Math.atan2(this.m21, this.m11);
|
||||
}
|
||||
|
||||
public set rotation(value: number){
|
||||
public set rotation(value: number) {
|
||||
let val1 = Math.cos(value);
|
||||
let val2 = Math.sin(value);
|
||||
|
||||
@@ -71,42 +92,18 @@ module es {
|
||||
this.m22 = value.y;
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建一个矩阵
|
||||
* @param m11
|
||||
* @param m12
|
||||
* @param m21
|
||||
* @param m22
|
||||
* @param m31
|
||||
* @param m32
|
||||
*/
|
||||
constructor(m11: number, m12: number, m21: number, m22: number, m31: number, m32: number){
|
||||
this.m11 = m11;
|
||||
this.m12 = m12;
|
||||
|
||||
this.m21 = m21;
|
||||
this.m22 = m22;
|
||||
|
||||
this.m31 = m31;
|
||||
this.m32 = m32;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建一个新的围绕Z轴的旋转矩阵2D
|
||||
* @param radians
|
||||
*/
|
||||
public static createRotation(radians: number){
|
||||
let result: Matrix2D = this.identity;
|
||||
|
||||
let val1 = Math.cos(radians);
|
||||
let val2 = Math.sin(radians);
|
||||
|
||||
public static createRotation(radians: number, result: Matrix2D) {
|
||||
result.setIdentity();
|
||||
const val1 = Math.cos(radians);
|
||||
const val2 = Math.sin(radians);
|
||||
result.m11 = val1;
|
||||
result.m12 = val2;
|
||||
result.m21 = -val2;
|
||||
result.m21 = val2 * -1;
|
||||
result.m22 = val1;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public static createRotationOut(radians: number, result: Matrix2D) {
|
||||
@@ -124,18 +121,13 @@ module es {
|
||||
* @param xScale
|
||||
* @param yScale
|
||||
*/
|
||||
public static createScale(xScale: number, yScale: number){
|
||||
let result: Matrix2D = this.identity;
|
||||
public static createScale(xScale: number, yScale: number, result: Matrix2D) {
|
||||
result.m11 = xScale;
|
||||
result.m12 = 0;
|
||||
|
||||
result.m21 = 0;
|
||||
result.m22 = yScale;
|
||||
|
||||
result.m31 = 0;
|
||||
result.m32 = 0;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public static createScaleOut(xScale: number, yScale: number, result: Matrix2D) {
|
||||
@@ -154,14 +146,11 @@ module es {
|
||||
* @param xPosition
|
||||
* @param yPosition
|
||||
*/
|
||||
public static createTranslation(xPosition: number, yPosition: number) {
|
||||
let result: Matrix2D = this.identity;
|
||||
public static createTranslation(xPosition: number, yPosition: number, result: Matrix2D) {
|
||||
result.m11 = 1;
|
||||
result.m12 = 0;
|
||||
|
||||
result.m21 = 0;
|
||||
result.m22 = 1;
|
||||
|
||||
result.m31 = xPosition;
|
||||
result.m32 = yPosition;
|
||||
|
||||
@@ -261,14 +250,14 @@ module es {
|
||||
}
|
||||
|
||||
public static multiply(matrix1: Matrix2D, matrix2: Matrix2D, result: Matrix2D) {
|
||||
let m11 = (matrix1.m11 * matrix2.m11) + (matrix1.m12 * matrix2.m21);
|
||||
let m12 = (matrix1.m11 * matrix2.m12) + (matrix1.m12 * matrix2.m22);
|
||||
const m11 = (matrix1.m11 * matrix2.m11) + (matrix1.m12 * matrix2.m21);
|
||||
const m12 = (matrix1.m11 * matrix2.m12) + (matrix1.m12 * matrix2.m22);
|
||||
|
||||
let m21 = (matrix1.m21 * matrix2.m11) + (matrix1.m22 * matrix2.m21);
|
||||
let m22 = (matrix1.m21 * matrix2.m12) + (matrix1.m22 * matrix2.m22);
|
||||
const m21 = (matrix1.m21 * matrix2.m11) + (matrix1.m22 * matrix2.m21);
|
||||
const m22 = (matrix1.m21 * matrix2.m12) + (matrix1.m22 * matrix2.m22);
|
||||
|
||||
let m31 = (matrix1.m31 * matrix2.m11) + (matrix1.m32 * matrix2.m21) + matrix2.m31;
|
||||
let m32 = (matrix1.m31 * matrix2.m12) + (matrix1.m32 * matrix2.m22) + matrix2.m32;
|
||||
const m31 = (matrix1.m31 * matrix2.m11) + (matrix1.m32 * matrix2.m21) + matrix2.m31;
|
||||
const m32 = (matrix1.m31 * matrix2.m12) + (matrix1.m32 * matrix2.m22) + matrix2.m32;
|
||||
|
||||
result.m11 = m11;
|
||||
result.m12 = m12;
|
||||
@@ -278,8 +267,6 @@ module es {
|
||||
|
||||
result.m31 = m31;
|
||||
result.m32 = m32;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public determinant() {
|
||||
@@ -292,10 +279,10 @@ module es {
|
||||
* @param matrix2
|
||||
* @param amount
|
||||
*/
|
||||
public static lerp(matrix1: Matrix2D, matrix2: Matrix2D, amount: number){
|
||||
public static lerp(matrix1: Matrix2D, matrix2: Matrix2D, amount: number) {
|
||||
matrix1.m11 = matrix1.m11 + ((matrix2.m11 - matrix1.m11) * amount);
|
||||
matrix1.m12 = matrix1.m12 + ((matrix2.m12 - matrix1.m12) * amount);
|
||||
|
||||
|
||||
matrix1.m21 = matrix1.m21 + ((matrix2.m21 - matrix1.m21) * amount);
|
||||
matrix1.m22 = matrix1.m22 + ((matrix2.m22 - matrix1.m22) * amount);
|
||||
|
||||
@@ -321,8 +308,9 @@ module es {
|
||||
return ret;
|
||||
}
|
||||
|
||||
public mutiplyTranslation(x: number, y: number){
|
||||
let trans = Matrix2D.createTranslation(x, y);
|
||||
public mutiplyTranslation(x: number, y: number) {
|
||||
let trans = new Matrix2D();
|
||||
Matrix2D.createTranslation(x, y, trans);
|
||||
return MatrixHelper.mutiply(this, trans);
|
||||
}
|
||||
|
||||
@@ -330,7 +318,7 @@ module es {
|
||||
* 比较当前实例是否等于指定的Matrix2D
|
||||
* @param other
|
||||
*/
|
||||
public equals(other: Matrix2D){
|
||||
public equals(other: Matrix2D) {
|
||||
return this == other;
|
||||
}
|
||||
|
||||
|
||||
@@ -107,8 +107,8 @@ module es {
|
||||
}
|
||||
|
||||
// temp 用于计算边界的矩阵
|
||||
public _tempMat: Matrix2D;
|
||||
public _transformMat: Matrix2D;
|
||||
public _tempMat: Matrix2D = new Matrix2D();
|
||||
public _transformMat: Matrix2D = new Matrix2D();
|
||||
|
||||
/**
|
||||
* 创建一个新的Rectanglestruct实例,指定位置、宽度和高度。
|
||||
@@ -213,49 +213,50 @@ module es {
|
||||
this.top < value.bottom;
|
||||
}
|
||||
|
||||
public rayIntersects(ray: Ray2D, distance: Ref<number>): boolean {
|
||||
distance.value = 0;
|
||||
public rayIntersects(ray: Ray2D): { intersected: boolean; distance: number } {
|
||||
const res = {intersected: false, distance: 0};
|
||||
let maxValue = Number.MAX_VALUE;
|
||||
|
||||
if (Math.abs(ray.direction.x) < 1E-06) {
|
||||
if ((ray.start.x < this.x) || (ray.start.x > this.x + this.width))
|
||||
return false;
|
||||
return res;
|
||||
} else {
|
||||
let num11 = 1 / ray.direction.x;
|
||||
const num11 = 1 / ray.direction.x;
|
||||
let num8 = (this.x - ray.start.x) * num11;
|
||||
let num7 = (this.x + this.width - ray.start.x) * num11;
|
||||
if (num8 > num7) {
|
||||
let num14 = num8;
|
||||
const num14 = num8;
|
||||
num8 = num7;
|
||||
num7 = num14;
|
||||
}
|
||||
|
||||
distance.value = Math.max(num8, distance.value);
|
||||
res.distance = Math.max(num8, res.distance);
|
||||
maxValue = Math.min(num7, maxValue);
|
||||
if (distance.value > maxValue)
|
||||
return false;
|
||||
if (res.distance > maxValue)
|
||||
return res;
|
||||
}
|
||||
|
||||
if (Math.abs(ray.direction.y) < 1E-06) {
|
||||
if (Math.abs(ray.direction.y) < 1e-06) {
|
||||
if ((ray.start.y < this.y) || (ray.start.y > this.y + this.height))
|
||||
return false;
|
||||
return res;
|
||||
} else {
|
||||
let num10 = 1 / ray.direction.y;
|
||||
const num10 = 1 / ray.direction.y;
|
||||
let num6 = (this.y - ray.start.y) * num10;
|
||||
let num5 = (this.y + this.height - ray.start.y) * num10;
|
||||
if (num6 > num5) {
|
||||
let num13 = num6;
|
||||
const num13 = num6;
|
||||
num6 = num5;
|
||||
num5 = num13;
|
||||
}
|
||||
|
||||
distance.value = Math.max(num6, distance.value);
|
||||
res.distance = Math.max(num6, res.distance);
|
||||
maxValue = Math.max(num5, maxValue);
|
||||
if (distance.value > maxValue)
|
||||
return false;
|
||||
if (res.distance > maxValue)
|
||||
return res;
|
||||
}
|
||||
|
||||
return true;
|
||||
res.intersected = true;
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -421,12 +422,12 @@ module es {
|
||||
let worldPosY = parentPosition.y + position.y;
|
||||
|
||||
// 考虑到原点,将参考点设置为世界参考
|
||||
this._transformMat = Matrix2D.createTranslation(-worldPosX - origin.x, -worldPosY - origin.y);
|
||||
this._tempMat = Matrix2D.createScale(scale.x, scale.y);
|
||||
Matrix2D.createTranslation(-worldPosX - origin.x, -worldPosY - origin.y, this._transformMat);
|
||||
Matrix2D.createScale(scale.x, scale.y, this._tempMat);
|
||||
this._transformMat = this._transformMat.multiply(this._tempMat);
|
||||
this._tempMat = Matrix2D.createRotation(rotation);
|
||||
Matrix2D.createRotation(rotation, this._tempMat);
|
||||
this._transformMat = this._transformMat.multiply(this._tempMat);
|
||||
this._tempMat = Matrix2D.createTranslation(worldPosX, worldPosY);
|
||||
Matrix2D.createTranslation(worldPosX, worldPosY, this._tempMat);
|
||||
this._transformMat = this._transformMat.multiply(this._tempMat);
|
||||
|
||||
// TODO: 我们可以把世界变换留在矩阵中,避免在世界空间中得到所有的四个角
|
||||
|
||||
@@ -77,74 +77,13 @@ module es {
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param value1
|
||||
* @param value2
|
||||
*/
|
||||
public static multiply(value1: Vector2, value2: Vector2) {
|
||||
let result: Vector2 = es.Vector2.zero;
|
||||
result.x = value1.x * value2.x;
|
||||
result.y = value1.y * value2.y;
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param value1
|
||||
* @param value2
|
||||
* @returns
|
||||
*/
|
||||
public static multiplyScaler(value1: Vector2, value2: number) {
|
||||
let result = es.Vector2.zero;
|
||||
result.x = value1.x * value2;
|
||||
result.y = value1.y * value2;
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param value1
|
||||
* @param value2
|
||||
*/
|
||||
public static subtract(value1: Vector2, value2: Vector2) {
|
||||
let result: Vector2 = es.Vector2.zero;
|
||||
result.x = value1.x - value2.x;
|
||||
result.y = value1.y - value2.y;
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建一个新的Vector2
|
||||
* 它包含来自另一个向量的标准化值。
|
||||
* @param value
|
||||
*/
|
||||
public static normalize(value: Vector2) {
|
||||
const d = value.distance();
|
||||
if (d > 0) {
|
||||
return new Vector2(value.x / d, value.y / d);
|
||||
} else {
|
||||
return new Vector2(0, 1);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回两个向量的点积
|
||||
* @param value1
|
||||
* @param value2
|
||||
*/
|
||||
public static dot(value1: Vector2, value2: Vector2): number {
|
||||
return (value1.x * value2.x) + (value1.y * value2.y);
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回两个向量之间距离的平方
|
||||
* @param value1
|
||||
* @param value2
|
||||
*/
|
||||
public static distanceSquared(value1: Vector2, value2: Vector2) {
|
||||
let v1 = value1.x - value2.x, v2 = value1.y - value2.y;
|
||||
return (v1 * v1) + (v2 * v2);
|
||||
public static sqrDistance(value1: Vector2, value2: Vector2) {
|
||||
return Math.pow(value1.x - value2.x, 2) + Math.pow(value1.y - value2.y, 2);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -207,9 +146,8 @@ module es {
|
||||
* @param value2
|
||||
* @returns 两个向量之间的距离
|
||||
*/
|
||||
public static distance(value1: Vector2, value2: Vector2): number {
|
||||
let v1 = value1.x - value2.x, v2 = value1.y - value2.y;
|
||||
return Math.sqrt((v1 * v1) + (v2 * v2));
|
||||
public static distance(vec1: Vector2, vec2: Vector2): number {
|
||||
return Math.sqrt(Math.pow(vec1.x - vec2.x, 2) + Math.pow(vec1.y - vec2.y, 2));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -218,9 +156,9 @@ module es {
|
||||
* @param to
|
||||
*/
|
||||
public static angle(from: Vector2, to: Vector2): number {
|
||||
from = Vector2.normalize(from);
|
||||
to = Vector2.normalize(to);
|
||||
return Math.acos(MathHelper.clamp(Vector2.dot(from, to), -1, 1)) * MathHelper.Rad2Deg;
|
||||
from = from.normalize();
|
||||
to = to.normalize();
|
||||
return Math.acos(MathHelper.clamp(from.dot(to), -1, 1)) * MathHelper.Rad2Deg;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -266,6 +204,10 @@ module es {
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
public negate(): Vector2 {
|
||||
return this.scale(-1);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param value
|
||||
@@ -301,9 +243,7 @@ module es {
|
||||
* @param value
|
||||
*/
|
||||
public multiply(value: Vector2): Vector2 {
|
||||
this.x *= value.x;
|
||||
this.y *= value.y;
|
||||
return this;
|
||||
return new Vector2(value.x * this.x, value.y * this.y);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -332,6 +272,10 @@ module es {
|
||||
return this;
|
||||
}
|
||||
|
||||
public dot(v: Vector2): number {
|
||||
return this.x * v.x + this.y * v.y;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param size
|
||||
@@ -341,10 +285,32 @@ module es {
|
||||
return new Vector2(this.x * size, this.y * size);
|
||||
}
|
||||
|
||||
public scaleEqual(size: number): Vector2 {
|
||||
this.x *= size;
|
||||
this.y *= size;
|
||||
return this;
|
||||
}
|
||||
|
||||
public transform(matrix: Matrix2D): Vector2 {
|
||||
return new Vector2(
|
||||
this.x * matrix.m11 + this.y * matrix.m21 + matrix.m31,
|
||||
this.x * matrix.m12 + this.y * matrix.m22 + matrix.m32
|
||||
);
|
||||
}
|
||||
|
||||
public normalize(): Vector2 {
|
||||
const d = this.distance();
|
||||
if (d > 0) {
|
||||
return new Vector2(this.x / d, this.y / d);
|
||||
} else {
|
||||
return new Vector2(0, 1);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 将这个Vector2变成一个方向相同的单位向量
|
||||
*/
|
||||
public normalize() {
|
||||
public normalizeEqual(): Vector2 {
|
||||
const d = this.distance();
|
||||
if (d > 0) {
|
||||
this.setTo(this.x / d, this.y / d);
|
||||
@@ -355,14 +321,9 @@ module es {
|
||||
}
|
||||
}
|
||||
|
||||
/** 返回它的长度 */
|
||||
public length() {
|
||||
return Math.sqrt((this.x * this.x) + (this.y * this.y));
|
||||
}
|
||||
|
||||
public magnitude(): number {
|
||||
return this.distance();
|
||||
}
|
||||
}
|
||||
|
||||
public distance(v?: Vector2): number {
|
||||
if (!v) {
|
||||
@@ -393,8 +354,8 @@ module es {
|
||||
* @param right
|
||||
*/
|
||||
public angleBetween(left: Vector2, right: Vector2) {
|
||||
let one = Vector2.subtract(left, this);
|
||||
let two = Vector2.subtract(right, this);
|
||||
let one = left.sub(this);
|
||||
let two = right.sub(this);
|
||||
return Vector2Ext.angle(one, two);
|
||||
}
|
||||
|
||||
@@ -403,12 +364,8 @@ module es {
|
||||
* @param other 要比较的对象
|
||||
* @returns 如果实例相同true 否则false
|
||||
*/
|
||||
public equals(other: Vector2 | object): boolean {
|
||||
if (other instanceof Vector2) {
|
||||
return other.x == this.x && other.y == this.y;
|
||||
}
|
||||
|
||||
return false;
|
||||
public equals(other: Vector2, tolerance: number = 0.001): boolean {
|
||||
return Math.abs(this.x - other.x) <= tolerance && Math.abs(this.y - other.y) <= tolerance;
|
||||
}
|
||||
|
||||
public isValid(): boolean {
|
||||
@@ -452,10 +409,10 @@ module es {
|
||||
}
|
||||
|
||||
public static unsignedAngle(from: Vector2, to: Vector2, round: boolean = true) {
|
||||
from.normalize();
|
||||
to.normalize();
|
||||
from.normalizeEqual();
|
||||
to.normalizeEqual();
|
||||
const angle =
|
||||
Math.acos(MathHelper.clamp(Vector2.dot(from, to), -1, 1)) * MathHelper.Rad2Deg;
|
||||
Math.acos(MathHelper.clamp(from.dot(to), -1, 1)) * MathHelper.Rad2Deg;
|
||||
return round ? Math.round(angle) : angle;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user