Files
esengine/source/src/Utils/Vector2Ext.ts
2020-06-16 00:04:28 +08:00

45 lines
1.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
class Vector2Ext {
/**
* 检查三角形是CCW还是CW
* @param a
* @param center
* @param c
*/
public static isTriangleCCW(a: Vector2, center: Vector2, c: Vector2){
return this.cross(Vector2.subtract(center, a), Vector2.subtract(c, center)) < 0;
}
/**
* 计算二维伪叉乘点(Perp(u) v)
* @param u
* @param v
*/
public static cross(u: Vector2, v: Vector2){
return u.y * v.x - u.x * v.y;
}
/**
* 返回与传入向量垂直的向量
* @param first
* @param second
*/
public static perpendicular(first: Vector2, second: Vector2){
return new Vector2(-1 * (second.y - first.y), second.x - first.x);
}
/**
* Vector2的临时解决方案
* 标准化把向量弄乱了
* @param vec
*/
public static normalize(vec: Vector2){
let magnitude = Math.sqrt((vec.x * vec.x) + (vec.y * vec.y));
if (magnitude > MathHelper.Epsilon){
vec = Vector2.divide(vec, new Vector2(magnitude));
} else {
vec.x = vec.y = 0;
}
return vec;
}
}