修复normalized
This commit is contained in:
@@ -14,8 +14,8 @@ module es {
|
||||
t = MathHelper.clamp01(t);
|
||||
let oneMinusT = 1 - t;
|
||||
return new Vector2(oneMinusT * oneMinusT).multiply(p0)
|
||||
.add(new Vector2(2 * oneMinusT * t).multiply(p1))
|
||||
.add(new Vector2(t * t).multiply(p2));
|
||||
.addEqual(new Vector2(2 * oneMinusT * t).multiply(p1))
|
||||
.addEqual(new Vector2(t * t).multiply(p2));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -31,9 +31,9 @@ module es {
|
||||
t = MathHelper.clamp01(t);
|
||||
let oneMinusT = 1 - t;
|
||||
return new Vector2(oneMinusT * oneMinusT * oneMinusT).multiply(start)
|
||||
.add(new Vector2(3 * oneMinusT * oneMinusT * t).multiply(firstControlPoint))
|
||||
.add(new Vector2(3 * oneMinusT * t * t).multiply(secondControlPoint))
|
||||
.add(new Vector2(t * t * t).multiply(end));
|
||||
.addEqual(new Vector2(3 * oneMinusT * oneMinusT * t).multiply(firstControlPoint))
|
||||
.addEqual(new Vector2(3 * oneMinusT * t * t).multiply(secondControlPoint))
|
||||
.addEqual(new Vector2(t * t * t).multiply(end));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -45,7 +45,7 @@ module es {
|
||||
*/
|
||||
public static getFirstDerivative(p0: Vector2, p1: Vector2, p2: Vector2, t: number) {
|
||||
return new Vector2(2 * (1 - t)).multiply(Vector2.subtract(p1, p0))
|
||||
.add(new Vector2(2 * t).multiply(Vector2.subtract(p2, p1)));
|
||||
.addEqual(new Vector2(2 * t).multiply(Vector2.subtract(p2, p1)));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -61,8 +61,8 @@ module es {
|
||||
t = MathHelper.clamp01(t);
|
||||
let oneMunusT = 1 - t;
|
||||
return new Vector2(3 * oneMunusT * oneMunusT).multiply(Vector2.subtract(firstControlPoint, start))
|
||||
.add(new Vector2(6 * oneMunusT * t).multiply(Vector2.subtract(secondControlPoint, firstControlPoint)))
|
||||
.add(new Vector2(3 * t * t).multiply(Vector2.subtract(end, secondControlPoint)));
|
||||
.addEqual(new Vector2(6 * oneMunusT * t).multiply(Vector2.subtract(secondControlPoint, firstControlPoint)))
|
||||
.addEqual(new Vector2(3 * t * t).multiply(Vector2.subtract(end, secondControlPoint)));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -34,10 +34,10 @@ module es {
|
||||
if (index % 3 == 0) {
|
||||
let delta = Vector2.subtract(point, this._points[index]);
|
||||
if (index > 0)
|
||||
this._points[index - 1].add(delta);
|
||||
this._points[index - 1].addEqual(delta);
|
||||
|
||||
if (index + 1 < this._points.length)
|
||||
this._points[index + 1].add(delta);
|
||||
this._points[index + 1].addEqual(delta);
|
||||
}
|
||||
|
||||
this._points[index] = point;
|
||||
|
||||
@@ -120,11 +120,12 @@ module es {
|
||||
* @param value
|
||||
*/
|
||||
public static normalize(value: Vector2) {
|
||||
let nValue = new Vector2(value.x, value.y);
|
||||
let val = 1 / Math.sqrt((nValue.x * nValue.x) + (nValue.y * nValue.y));
|
||||
nValue.x *= val;
|
||||
nValue.y *= val;
|
||||
return nValue;
|
||||
const d = value.distance();
|
||||
if (d > 0) {
|
||||
return new Vector2(value.x / d, value.y / d);
|
||||
} else {
|
||||
return new Vector2(0, 1);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -260,13 +261,22 @@ module es {
|
||||
MathHelper.smoothStep(value1.y, value2.y, amount));
|
||||
}
|
||||
|
||||
public setTo(x: number, y: number) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param value
|
||||
*/
|
||||
public add(value: Vector2): Vector2 {
|
||||
this.x += value.x;
|
||||
this.y += value.y;
|
||||
public add(v: Vector2): Vector2 {
|
||||
return new Vector2(this.x + v.x, this.y + v.y);
|
||||
}
|
||||
|
||||
public addEqual(v: Vector2): Vector2 {
|
||||
this.x += v.x;
|
||||
this.y += v.y;
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -312,19 +322,37 @@ module es {
|
||||
* @param value 要减去的Vector2
|
||||
* @returns 当前Vector2
|
||||
*/
|
||||
public subtract(value: Vector2) {
|
||||
this.x -= value.x;
|
||||
this.y -= value.y;
|
||||
public sub(value: Vector2) {
|
||||
return new Vector2(this.x - value.x, this.y - value.y);
|
||||
}
|
||||
|
||||
public subEqual(v: Vector2): Vector2 {
|
||||
this.x -= v.x;
|
||||
this.y -= v.y;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param size
|
||||
* @returns
|
||||
*/
|
||||
public scale(size: number): Vector2 {
|
||||
return new Vector2(this.x * size, this.y * size);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将这个Vector2变成一个方向相同的单位向量
|
||||
*/
|
||||
public normalize() {
|
||||
let val = 1 / Math.sqrt((this.x * this.x) + (this.y * this.y));
|
||||
this.x *= val;
|
||||
this.y *= val;
|
||||
const d = this.distance();
|
||||
if (d > 0) {
|
||||
this.setTo(this.x / d, this.y / d);
|
||||
return this;
|
||||
} else {
|
||||
this.setTo(0, 1);
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
/** 返回它的长度 */
|
||||
@@ -332,6 +360,18 @@ module es {
|
||||
return Math.sqrt((this.x * this.x) + (this.y * this.y));
|
||||
}
|
||||
|
||||
public magnitude(): number {
|
||||
return this.distance();
|
||||
}
|
||||
|
||||
public distance(v?: Vector2): number {
|
||||
if (!v) {
|
||||
v = Vector2.zero;
|
||||
}
|
||||
|
||||
return Math.sqrt(Math.pow(this.x - v.x, 2) + Math.pow(this.y - v.y, 2));
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回该Vector2的平方长度
|
||||
* @returns 这个Vector2的平方长度
|
||||
|
||||
Reference in New Issue
Block a user