全部移动至es模块
This commit is contained in:
@@ -1,121 +1,123 @@
|
||||
/** 贝塞尔帮助类 */
|
||||
class Bezier {
|
||||
/**
|
||||
* 二次贝塞尔曲线
|
||||
* @param p0
|
||||
* @param p1
|
||||
* @param p2
|
||||
* @param t
|
||||
*/
|
||||
public static getPoint(p0: Vector2, p1: Vector2, p2: Vector2, t: number): Vector2 {
|
||||
t = MathHelper.clamp01(t);
|
||||
let oneMinusT = 1 - t;
|
||||
return Vector2.add(Vector2.add(Vector2.multiply(new Vector2(oneMinusT * oneMinusT), p0),
|
||||
Vector2.multiply(new Vector2(2 * oneMinusT * t), p1)), Vector2.multiply(new Vector2(t * t), p2));
|
||||
}
|
||||
|
||||
/**
|
||||
* 得到二次贝塞尔函数的一阶导数
|
||||
* @param p0
|
||||
* @param p1
|
||||
* @param p2
|
||||
* @param t
|
||||
*/
|
||||
public static getFirstDerivative(p0: Vector2, p1: Vector2, p2: Vector2, t: number) {
|
||||
return Vector2.add(Vector2.multiply(new Vector2(2 * (1 - t)), Vector2.subtract(p1, p0)),
|
||||
Vector2.multiply(new Vector2(2 * t), Vector2.subtract(p2, p1)));
|
||||
}
|
||||
|
||||
/**
|
||||
* 得到一个三次贝塞尔函数的一阶导数
|
||||
* @param start
|
||||
* @param firstControlPoint
|
||||
* @param secondControlPoint
|
||||
* @param end
|
||||
* @param t
|
||||
*/
|
||||
public static getFirstDerivativeThree(start: Vector2, firstControlPoint: Vector2, secondControlPoint: Vector2,
|
||||
end: Vector2, t: number) {
|
||||
t = MathHelper.clamp01(t);
|
||||
let oneMunusT = 1 - t;
|
||||
return Vector2.add(Vector2.add(Vector2.multiply(new Vector2(3 * oneMunusT * oneMunusT), Vector2.subtract(firstControlPoint, start)),
|
||||
Vector2.multiply(new Vector2(6 * oneMunusT * t), Vector2.subtract(secondControlPoint, firstControlPoint))),
|
||||
Vector2.multiply(new Vector2(3 * t * t), Vector2.subtract(end, secondControlPoint)));
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算一个三次贝塞尔
|
||||
* @param start
|
||||
* @param firstControlPoint
|
||||
* @param secondControlPoint
|
||||
* @param end
|
||||
* @param t
|
||||
*/
|
||||
public static getPointThree(start: Vector2, firstControlPoint: Vector2, secondControlPoint: Vector2,
|
||||
end: Vector2, t: number) {
|
||||
t = MathHelper.clamp01(t);
|
||||
let oneMunusT = 1 - t;
|
||||
return Vector2.add(Vector2.add(Vector2.add(Vector2.multiply(new Vector2(oneMunusT * oneMunusT * oneMunusT), start),
|
||||
Vector2.multiply(new Vector2(3 * oneMunusT * oneMunusT * t), firstControlPoint)),
|
||||
Vector2.multiply(new Vector2(3 * oneMunusT * t * t), secondControlPoint)),
|
||||
Vector2.multiply(new Vector2(t * t * t), end));
|
||||
}
|
||||
|
||||
/**
|
||||
* 递归地细分bezier曲线,直到满足距离校正
|
||||
* 在这种算法中,平面切片的点要比曲面切片少。返回完成后应返回到ListPool的合并列表。
|
||||
* @param start
|
||||
* @param firstCtrlPoint
|
||||
* @param secondCtrlPoint
|
||||
* @param end
|
||||
* @param distanceTolerance
|
||||
*/
|
||||
public static getOptimizedDrawingPoints(start: Vector2, firstCtrlPoint: Vector2, secondCtrlPoint: Vector2,
|
||||
end: Vector2, distanceTolerance: number = 1) {
|
||||
let points = ListPool.obtain<Vector2>();
|
||||
points.push(start);
|
||||
this.recursiveGetOptimizedDrawingPoints(start, firstCtrlPoint, secondCtrlPoint, end, points, distanceTolerance);
|
||||
points.push(end);
|
||||
|
||||
return points;
|
||||
}
|
||||
|
||||
/**
|
||||
* 递归地细分bezier曲线,直到满足距离校正。在这种算法中,平面切片的点要比曲面切片少。
|
||||
* @param start
|
||||
* @param firstCtrlPoint
|
||||
* @param secondCtrlPoint
|
||||
* @param end
|
||||
* @param points
|
||||
* @param distanceTolerance
|
||||
*/
|
||||
private static recursiveGetOptimizedDrawingPoints(start: Vector2, firstCtrlPoint: Vector2, secondCtrlPoint: Vector2,
|
||||
end: Vector2, points: Vector2[], distanceTolerance: number) {
|
||||
// 计算线段的所有中点
|
||||
let pt12 = Vector2.divide(Vector2.add(start, firstCtrlPoint), new Vector2(2));
|
||||
let pt23 = Vector2.divide(Vector2.add(firstCtrlPoint, secondCtrlPoint), new Vector2(2));
|
||||
let pt34 = Vector2.divide(Vector2.add(secondCtrlPoint, end), new Vector2(2));
|
||||
|
||||
// 计算新半直线的中点
|
||||
let pt123 = Vector2.divide(Vector2.add(pt12, pt23), new Vector2(2));
|
||||
let pt234 = Vector2.divide(Vector2.add(pt23, pt34), new Vector2(2));
|
||||
|
||||
// 最后再细分最后两个中点。如果我们满足我们的距离公差,这将是我们使用的最后一点。
|
||||
let pt1234 = Vector2.divide(Vector2.add(pt123, pt234), new Vector2(2));
|
||||
|
||||
// 试着用一条直线来近似整个三次曲线
|
||||
let deltaLine = Vector2.subtract(end, 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));
|
||||
|
||||
if ((d2 + d3) * (d2 + d3) < distanceTolerance * (deltaLine.x * deltaLine.x + deltaLine.y * deltaLine.y)) {
|
||||
points.push(pt1234);
|
||||
return;
|
||||
module es {
|
||||
/** 贝塞尔帮助类 */
|
||||
export class Bezier {
|
||||
/**
|
||||
* 二次贝塞尔曲线
|
||||
* @param p0
|
||||
* @param p1
|
||||
* @param p2
|
||||
* @param t
|
||||
*/
|
||||
public static getPoint(p0: Vector2, p1: Vector2, p2: Vector2, t: number): Vector2 {
|
||||
t = MathHelper.clamp01(t);
|
||||
let oneMinusT = 1 - t;
|
||||
return Vector2.add(Vector2.add(Vector2.multiply(new Vector2(oneMinusT * oneMinusT), p0),
|
||||
Vector2.multiply(new Vector2(2 * oneMinusT * t), p1)), Vector2.multiply(new Vector2(t * t), p2));
|
||||
}
|
||||
|
||||
// 继续细分
|
||||
this.recursiveGetOptimizedDrawingPoints(start, pt12, pt123, pt1234, points, distanceTolerance);
|
||||
this.recursiveGetOptimizedDrawingPoints(pt1234, pt234, pt34, end, points, distanceTolerance);
|
||||
/**
|
||||
* 得到二次贝塞尔函数的一阶导数
|
||||
* @param p0
|
||||
* @param p1
|
||||
* @param p2
|
||||
* @param t
|
||||
*/
|
||||
public static getFirstDerivative(p0: Vector2, p1: Vector2, p2: Vector2, t: number) {
|
||||
return Vector2.add(Vector2.multiply(new Vector2(2 * (1 - t)), Vector2.subtract(p1, p0)),
|
||||
Vector2.multiply(new Vector2(2 * t), Vector2.subtract(p2, p1)));
|
||||
}
|
||||
|
||||
/**
|
||||
* 得到一个三次贝塞尔函数的一阶导数
|
||||
* @param start
|
||||
* @param firstControlPoint
|
||||
* @param secondControlPoint
|
||||
* @param end
|
||||
* @param t
|
||||
*/
|
||||
public static getFirstDerivativeThree(start: Vector2, firstControlPoint: Vector2, secondControlPoint: Vector2,
|
||||
end: Vector2, t: number) {
|
||||
t = MathHelper.clamp01(t);
|
||||
let oneMunusT = 1 - t;
|
||||
return Vector2.add(Vector2.add(Vector2.multiply(new Vector2(3 * oneMunusT * oneMunusT), Vector2.subtract(firstControlPoint, start)),
|
||||
Vector2.multiply(new Vector2(6 * oneMunusT * t), Vector2.subtract(secondControlPoint, firstControlPoint))),
|
||||
Vector2.multiply(new Vector2(3 * t * t), Vector2.subtract(end, secondControlPoint)));
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算一个三次贝塞尔
|
||||
* @param start
|
||||
* @param firstControlPoint
|
||||
* @param secondControlPoint
|
||||
* @param end
|
||||
* @param t
|
||||
*/
|
||||
public static getPointThree(start: Vector2, firstControlPoint: Vector2, secondControlPoint: Vector2,
|
||||
end: Vector2, t: number) {
|
||||
t = MathHelper.clamp01(t);
|
||||
let oneMunusT = 1 - t;
|
||||
return Vector2.add(Vector2.add(Vector2.add(Vector2.multiply(new Vector2(oneMunusT * oneMunusT * oneMunusT), start),
|
||||
Vector2.multiply(new Vector2(3 * oneMunusT * oneMunusT * t), firstControlPoint)),
|
||||
Vector2.multiply(new Vector2(3 * oneMunusT * t * t), secondControlPoint)),
|
||||
Vector2.multiply(new Vector2(t * t * t), end));
|
||||
}
|
||||
|
||||
/**
|
||||
* 递归地细分bezier曲线,直到满足距离校正
|
||||
* 在这种算法中,平面切片的点要比曲面切片少。返回完成后应返回到ListPool的合并列表。
|
||||
* @param start
|
||||
* @param firstCtrlPoint
|
||||
* @param secondCtrlPoint
|
||||
* @param end
|
||||
* @param distanceTolerance
|
||||
*/
|
||||
public static getOptimizedDrawingPoints(start: Vector2, firstCtrlPoint: Vector2, secondCtrlPoint: Vector2,
|
||||
end: Vector2, distanceTolerance: number = 1) {
|
||||
let points = ListPool.obtain<Vector2>();
|
||||
points.push(start);
|
||||
this.recursiveGetOptimizedDrawingPoints(start, firstCtrlPoint, secondCtrlPoint, end, points, distanceTolerance);
|
||||
points.push(end);
|
||||
|
||||
return points;
|
||||
}
|
||||
|
||||
/**
|
||||
* 递归地细分bezier曲线,直到满足距离校正。在这种算法中,平面切片的点要比曲面切片少。
|
||||
* @param start
|
||||
* @param firstCtrlPoint
|
||||
* @param secondCtrlPoint
|
||||
* @param end
|
||||
* @param points
|
||||
* @param distanceTolerance
|
||||
*/
|
||||
private static recursiveGetOptimizedDrawingPoints(start: Vector2, firstCtrlPoint: Vector2, secondCtrlPoint: Vector2,
|
||||
end: Vector2, points: Vector2[], distanceTolerance: number) {
|
||||
// 计算线段的所有中点
|
||||
let pt12 = Vector2.divide(Vector2.add(start, firstCtrlPoint), new Vector2(2));
|
||||
let pt23 = Vector2.divide(Vector2.add(firstCtrlPoint, secondCtrlPoint), new Vector2(2));
|
||||
let pt34 = Vector2.divide(Vector2.add(secondCtrlPoint, end), new Vector2(2));
|
||||
|
||||
// 计算新半直线的中点
|
||||
let pt123 = Vector2.divide(Vector2.add(pt12, pt23), new Vector2(2));
|
||||
let pt234 = Vector2.divide(Vector2.add(pt23, pt34), new Vector2(2));
|
||||
|
||||
// 最后再细分最后两个中点。如果我们满足我们的距离公差,这将是我们使用的最后一点。
|
||||
let pt1234 = Vector2.divide(Vector2.add(pt123, pt234), new Vector2(2));
|
||||
|
||||
// 试着用一条直线来近似整个三次曲线
|
||||
let deltaLine = Vector2.subtract(end, 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));
|
||||
|
||||
if ((d2 + d3) * (d2 + d3) < distanceTolerance * (deltaLine.x * deltaLine.x + deltaLine.y * deltaLine.y)) {
|
||||
points.push(pt1234);
|
||||
return;
|
||||
}
|
||||
|
||||
// 继续细分
|
||||
this.recursiveGetOptimizedDrawingPoints(start, pt12, pt123, pt1234, points, distanceTolerance);
|
||||
this.recursiveGetOptimizedDrawingPoints(pt1234, pt234, pt34, end, points, distanceTolerance);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,62 +1,64 @@
|
||||
/**
|
||||
* 帮助处理位掩码的实用程序类
|
||||
* 除了isFlagSet之外,所有方法都期望flag参数是一个非移位的标志
|
||||
* 允许您使用普通的(0、1、2、3等)来设置/取消您的标记
|
||||
*/
|
||||
class Flags {
|
||||
module es {
|
||||
/**
|
||||
* 检查位标志是否已在数值中设置
|
||||
* 检查期望标志是否已经移位
|
||||
* @param self
|
||||
* @param flag
|
||||
* 帮助处理位掩码的实用程序类
|
||||
* 除了isFlagSet之外,所有方法都期望flag参数是一个非移位的标志
|
||||
* 允许您使用普通的(0、1、2、3等)来设置/取消您的标记
|
||||
*/
|
||||
public static isFlagSet(self: number, flag: number): boolean{
|
||||
return (self & flag) != 0;
|
||||
}
|
||||
export class Flags {
|
||||
/**
|
||||
* 检查位标志是否已在数值中设置
|
||||
* 检查期望标志是否已经移位
|
||||
* @param self
|
||||
* @param flag
|
||||
*/
|
||||
public static isFlagSet(self: number, flag: number): boolean{
|
||||
return (self & flag) != 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查位标志是否在数值中设置
|
||||
* @param self
|
||||
* @param flag
|
||||
*/
|
||||
public static isUnshiftedFlagSet(self: number, flag: number): boolean{
|
||||
flag = 1 << flag;
|
||||
return (self & flag) != 0;
|
||||
}
|
||||
/**
|
||||
* 检查位标志是否在数值中设置
|
||||
* @param self
|
||||
* @param flag
|
||||
*/
|
||||
public static isUnshiftedFlagSet(self: number, flag: number): boolean{
|
||||
flag = 1 << flag;
|
||||
return (self & flag) != 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置数值标志位,移除所有已经设置的标志
|
||||
* @param self
|
||||
* @param flag
|
||||
*/
|
||||
public static setFlagExclusive(self: number, flag: number){
|
||||
return 1 << flag;
|
||||
}
|
||||
/**
|
||||
* 设置数值标志位,移除所有已经设置的标志
|
||||
* @param self
|
||||
* @param flag
|
||||
*/
|
||||
public static setFlagExclusive(self: number, flag: number){
|
||||
return 1 << flag;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置标志位
|
||||
* @param self
|
||||
* @param flag
|
||||
*/
|
||||
public static setFlag(self: number, flag: number){
|
||||
return (self | 1 << flag);
|
||||
}
|
||||
/**
|
||||
* 设置标志位
|
||||
* @param self
|
||||
* @param flag
|
||||
*/
|
||||
public static setFlag(self: number, flag: number){
|
||||
return (self | 1 << flag);
|
||||
}
|
||||
|
||||
/**
|
||||
* 取消标志位
|
||||
* @param self
|
||||
* @param flag
|
||||
*/
|
||||
public static unsetFlag(self: number, flag: number){
|
||||
flag = 1 << flag;
|
||||
return (self & (~flag));
|
||||
}
|
||||
/**
|
||||
* 取消标志位
|
||||
* @param self
|
||||
* @param flag
|
||||
*/
|
||||
public static unsetFlag(self: number, flag: number){
|
||||
flag = 1 << flag;
|
||||
return (self & (~flag));
|
||||
}
|
||||
|
||||
/**
|
||||
* 反转数值集合位
|
||||
* @param self
|
||||
*/
|
||||
public static invertFlags(self: number){
|
||||
return ~self;
|
||||
/**
|
||||
* 反转数值集合位
|
||||
* @param self
|
||||
*/
|
||||
public static invertFlags(self: number){
|
||||
return ~self;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,78 +1,80 @@
|
||||
class MathHelper {
|
||||
public static readonly Epsilon: number = 0.00001;
|
||||
public static readonly Rad2Deg = 57.29578;
|
||||
public static readonly Deg2Rad = 0.0174532924;
|
||||
module es {
|
||||
export class MathHelper {
|
||||
public static readonly Epsilon: number = 0.00001;
|
||||
public static readonly Rad2Deg = 57.29578;
|
||||
public static readonly Deg2Rad = 0.0174532924;
|
||||
|
||||
/**
|
||||
* 将弧度转换成角度。
|
||||
* @param radians 用弧度表示的角
|
||||
*/
|
||||
public static toDegrees(radians: number){
|
||||
return radians * 57.295779513082320876798154814105;
|
||||
/**
|
||||
* 将弧度转换成角度。
|
||||
* @param radians 用弧度表示的角
|
||||
*/
|
||||
public static toDegrees(radians: number){
|
||||
return radians * 57.295779513082320876798154814105;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将角度转换为弧度
|
||||
* @param degrees
|
||||
*/
|
||||
public static toRadians(degrees: number){
|
||||
return degrees * 0.017453292519943295769236907684886;
|
||||
}
|
||||
|
||||
/**
|
||||
* mapps值(在leftMin - leftMax范围内)到rightMin - rightMax范围内的值
|
||||
* @param value
|
||||
* @param leftMin
|
||||
* @param leftMax
|
||||
* @param rightMin
|
||||
* @param rightMax
|
||||
*/
|
||||
public static map(value: number, leftMin: number, leftMax: number, rightMin: number, rightMax: number){
|
||||
return rightMin + (value - leftMin) * (rightMax - rightMin) / (leftMax - leftMin);
|
||||
}
|
||||
|
||||
public static lerp(value1: number, value2: number, amount: number){
|
||||
return value1 + (value2 - value1) * amount;
|
||||
}
|
||||
|
||||
public static clamp(value: number, min: number, max: number){
|
||||
if (value < min)
|
||||
return min;
|
||||
|
||||
if (value > max)
|
||||
return max;
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
public static pointOnCirlce(circleCenter: Vector2, radius: number, angleInDegrees: number){
|
||||
let radians = MathHelper.toRadians(angleInDegrees);
|
||||
return new Vector2(Math.cos(radians) * radians + circleCenter.x, Math.sin(radians) * radians + circleCenter.y);
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果值为偶数,返回true
|
||||
* @param value
|
||||
*/
|
||||
public static isEven(value: number){
|
||||
return value % 2 == 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 数值限定在0-1之间
|
||||
* @param value
|
||||
*/
|
||||
public static clamp01(value: number){
|
||||
if (value < 0)
|
||||
return 0;
|
||||
|
||||
if (value > 1)
|
||||
return 1;
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
public static angleBetweenVectors(from: Vector2, to: Vector2){
|
||||
return Math.atan2(to.y - from.y, to.x - from.x);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 将角度转换为弧度
|
||||
* @param degrees
|
||||
*/
|
||||
public static toRadians(degrees: number){
|
||||
return degrees * 0.017453292519943295769236907684886;
|
||||
}
|
||||
|
||||
/**
|
||||
* mapps值(在leftMin - leftMax范围内)到rightMin - rightMax范围内的值
|
||||
* @param value
|
||||
* @param leftMin
|
||||
* @param leftMax
|
||||
* @param rightMin
|
||||
* @param rightMax
|
||||
*/
|
||||
public static map(value: number, leftMin: number, leftMax: number, rightMin: number, rightMax: number){
|
||||
return rightMin + (value - leftMin) * (rightMax - rightMin) / (leftMax - leftMin);
|
||||
}
|
||||
|
||||
public static lerp(value1: number, value2: number, amount: number){
|
||||
return value1 + (value2 - value1) * amount;
|
||||
}
|
||||
|
||||
public static clamp(value: number, min: number, max: number){
|
||||
if (value < min)
|
||||
return min;
|
||||
|
||||
if (value > max)
|
||||
return max;
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
public static pointOnCirlce(circleCenter: Vector2, radius: number, angleInDegrees: number){
|
||||
let radians = MathHelper.toRadians(angleInDegrees);
|
||||
return new Vector2(Math.cos(radians) * radians + circleCenter.x, Math.sin(radians) * radians + circleCenter.y);
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果值为偶数,返回true
|
||||
* @param value
|
||||
*/
|
||||
public static isEven(value: number){
|
||||
return value % 2 == 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 数值限定在0-1之间
|
||||
* @param value
|
||||
*/
|
||||
public static clamp01(value: number){
|
||||
if (value < 0)
|
||||
return 0;
|
||||
|
||||
if (value > 1)
|
||||
return 1;
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
public static angleBetweenVectors(from: Vector2, to: Vector2){
|
||||
return Math.atan2(to.y - from.y, to.x - from.x);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,217 +1,219 @@
|
||||
/**
|
||||
* 表示右手3 * 3的浮点矩阵,可以存储平移、缩放和旋转信息。
|
||||
*/
|
||||
class Matrix2D {
|
||||
public m11: number = 0;
|
||||
public m12: number = 0;
|
||||
|
||||
public m21: number = 0;
|
||||
public m22: number = 0;
|
||||
|
||||
public m31: number = 0;
|
||||
public m32: number = 0;
|
||||
|
||||
private static _identity: Matrix2D = new Matrix2D(1, 0, 0, 1, 0, 0);
|
||||
|
||||
module es {
|
||||
/**
|
||||
* 单位矩阵
|
||||
* 表示右手3 * 3的浮点矩阵,可以存储平移、缩放和旋转信息。
|
||||
*/
|
||||
public static get identity(){
|
||||
return Matrix2D._identity;
|
||||
export class Matrix2D {
|
||||
public m11: number = 0;
|
||||
public m12: number = 0;
|
||||
|
||||
public m21: number = 0;
|
||||
public m22: number = 0;
|
||||
|
||||
public m31: number = 0;
|
||||
public m32: number = 0;
|
||||
|
||||
private static _identity: Matrix2D = new Matrix2D(1, 0, 0, 1, 0, 0);
|
||||
|
||||
/**
|
||||
* 单位矩阵
|
||||
*/
|
||||
public static get identity(){
|
||||
return Matrix2D._identity;
|
||||
}
|
||||
|
||||
constructor(m11?: number, m12?: number, m21?: number, m22?: number, m31?: number, m32?: number){
|
||||
this.m11 = m11 ? m11 : 1;
|
||||
this.m12 = m12 ? m12 : 0;
|
||||
|
||||
this.m21 = m21 ? m21 : 0;
|
||||
this.m22 = m22 ? m22 : 1;
|
||||
|
||||
this.m31 = m31 ? m31 : 0;
|
||||
this.m32 = m32 ? m32 : 0;
|
||||
}
|
||||
|
||||
/** 存储在这个矩阵中的位置 */
|
||||
public get translation(){
|
||||
return new Vector2(this.m31, this.m32);
|
||||
}
|
||||
|
||||
public set translation(value: Vector2){
|
||||
this.m31 = value.x;
|
||||
this.m32 = value.y;
|
||||
}
|
||||
|
||||
/** 以弧度表示的旋转存储在这个矩阵中 */
|
||||
public get rotation(){
|
||||
return Math.atan2(this.m21, this.m11);
|
||||
}
|
||||
|
||||
public set rotation(value: number){
|
||||
let val1 = Math.cos(value);
|
||||
let val2 = Math.sin(value);
|
||||
|
||||
this.m11 = val1;
|
||||
this.m12 = val2;
|
||||
this.m21 = -val2;
|
||||
this.m22 = val1;
|
||||
}
|
||||
|
||||
/**
|
||||
* 以度为单位的旋转存储在这个矩阵中
|
||||
*/
|
||||
public get rotationDegrees(){
|
||||
return MathHelper.toDegrees(this.rotation);
|
||||
}
|
||||
|
||||
public set rotationDegrees(value: number){
|
||||
this.rotation = MathHelper.toRadians(value);
|
||||
}
|
||||
|
||||
public get scale(){
|
||||
return new Vector2(this.m11, this.m22);
|
||||
}
|
||||
|
||||
public set scale(value: Vector2){
|
||||
this.m11 = value.x;
|
||||
this.m12 = value.y;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建一个新的matrix, 它包含两个矩阵的和。
|
||||
* @param matrix1
|
||||
* @param matrix2
|
||||
*/
|
||||
public static add(matrix1: Matrix2D, matrix2: Matrix2D){
|
||||
matrix1.m11 += matrix2.m11;
|
||||
matrix1.m12 += matrix2.m12;
|
||||
|
||||
matrix1.m21 += matrix2.m21;
|
||||
matrix1.m22 += matrix2.m22;
|
||||
|
||||
matrix1.m31 += matrix2.m31;
|
||||
matrix1.m32 += matrix2.m32;
|
||||
|
||||
return matrix1;
|
||||
}
|
||||
|
||||
public static divide(matrix1: Matrix2D, matrix2: Matrix2D){
|
||||
matrix1.m11 /= matrix2.m11;
|
||||
matrix1.m12 /= matrix2.m12;
|
||||
|
||||
matrix1.m21 /= matrix2.m21;
|
||||
matrix1.m22 /= matrix2.m22;
|
||||
|
||||
matrix1.m31 /= matrix2.m31;
|
||||
matrix1.m32 /= matrix2.m32;
|
||||
|
||||
return matrix1;
|
||||
}
|
||||
|
||||
public static multiply(matrix1: Matrix2D, matrix2: Matrix2D){
|
||||
let result = new Matrix2D();
|
||||
|
||||
let m11 = ( matrix1.m11 * matrix2.m11 ) + ( matrix1.m12 * matrix2.m21 );
|
||||
let 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 );
|
||||
|
||||
let m31 = ( matrix1.m31 * matrix2.m11 ) + ( matrix1.m32 * matrix2.m21 ) + matrix2.m31;
|
||||
let m32 = ( matrix1.m31 * matrix2.m12 ) + ( matrix1.m32 * matrix2.m22 ) + matrix2.m32;
|
||||
|
||||
result.m11 = m11;
|
||||
result.m12 = m12;
|
||||
|
||||
result.m21 = m21;
|
||||
result.m22 = m22;
|
||||
|
||||
result.m31 = m31;
|
||||
result.m32 = m32;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public static multiplyTranslation(matrix: Matrix2D, x: number, y: number){
|
||||
let trans = Matrix2D.createTranslation(x, y);
|
||||
return Matrix2D.multiply(matrix, trans);
|
||||
}
|
||||
|
||||
public determinant(){
|
||||
return this.m11 * this.m22 - this.m12 * this.m21;
|
||||
}
|
||||
|
||||
public static invert(matrix: Matrix2D, result: Matrix2D = new Matrix2D()){
|
||||
let det = 1 / matrix.determinant();
|
||||
|
||||
result.m11 = matrix.m22 * det;
|
||||
result.m12 = -matrix.m12 * det;
|
||||
|
||||
result.m21 = -matrix.m21 * det;
|
||||
result.m22 = matrix.m11 * det;
|
||||
|
||||
result.m31 = (matrix.m32 * matrix.m21 - matrix.m31 * matrix.m22) * det;
|
||||
result.m32 = -(matrix.m32 * matrix.m11 - matrix.m31 * matrix.m12) * det;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建一个新的tranlation
|
||||
* @param xPosition
|
||||
* @param yPosition
|
||||
*/
|
||||
public static createTranslation(xPosition: number, yPosition: number){
|
||||
let result = new Matrix2D();
|
||||
|
||||
result.m11 = 1;
|
||||
result.m12 = 0;
|
||||
|
||||
result.m21 = 0;
|
||||
result.m22 = 1;
|
||||
|
||||
result.m31 = xPosition;
|
||||
result.m32 = yPosition;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据position 创建一个translation
|
||||
* @param position
|
||||
*/
|
||||
public static createTranslationVector(position: Vector2){
|
||||
return this.createTranslation(position.x, position.y);
|
||||
}
|
||||
|
||||
public static createRotation(radians: number, result?: Matrix2D){
|
||||
result = new Matrix2D();
|
||||
|
||||
let val1 = Math.cos(radians);
|
||||
let val2 = Math.sin(radians);
|
||||
|
||||
result.m11 = val1;
|
||||
result.m12 = val2;
|
||||
result.m21 = -val2;
|
||||
result.m22 = val1;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public static createScale(xScale: number, yScale: number, result: Matrix2D = new Matrix2D()){
|
||||
result.m11 = xScale;
|
||||
result.m12 = 0;
|
||||
|
||||
result.m21 = 0;
|
||||
result.m22 = yScale;
|
||||
|
||||
result.m31 = 0;
|
||||
result.m32 = 0;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public toEgretMatrix(): egret.Matrix{
|
||||
let matrix = new egret.Matrix(this.m11, this.m12, this.m21, this.m22, this.m31, this.m32);
|
||||
return matrix;
|
||||
}
|
||||
}
|
||||
|
||||
constructor(m11?: number, m12?: number, m21?: number, m22?: number, m31?: number, m32?: number){
|
||||
this.m11 = m11 ? m11 : 1;
|
||||
this.m12 = m12 ? m12 : 0;
|
||||
|
||||
this.m21 = m21 ? m21 : 0;
|
||||
this.m22 = m22 ? m22 : 1;
|
||||
|
||||
this.m31 = m31 ? m31 : 0;
|
||||
this.m32 = m32 ? m32 : 0;
|
||||
}
|
||||
|
||||
/** 存储在这个矩阵中的位置 */
|
||||
public get translation(){
|
||||
return new Vector2(this.m31, this.m32);
|
||||
}
|
||||
|
||||
public set translation(value: Vector2){
|
||||
this.m31 = value.x;
|
||||
this.m32 = value.y;
|
||||
}
|
||||
|
||||
/** 以弧度表示的旋转存储在这个矩阵中 */
|
||||
public get rotation(){
|
||||
return Math.atan2(this.m21, this.m11);
|
||||
}
|
||||
|
||||
public set rotation(value: number){
|
||||
let val1 = Math.cos(value);
|
||||
let val2 = Math.sin(value);
|
||||
|
||||
this.m11 = val1;
|
||||
this.m12 = val2;
|
||||
this.m21 = -val2;
|
||||
this.m22 = val1;
|
||||
}
|
||||
|
||||
/**
|
||||
* 以度为单位的旋转存储在这个矩阵中
|
||||
*/
|
||||
public get rotationDegrees(){
|
||||
return MathHelper.toDegrees(this.rotation);
|
||||
}
|
||||
|
||||
public set rotationDegrees(value: number){
|
||||
this.rotation = MathHelper.toRadians(value);
|
||||
}
|
||||
|
||||
public get scale(){
|
||||
return new Vector2(this.m11, this.m22);
|
||||
}
|
||||
|
||||
public set scale(value: Vector2){
|
||||
this.m11 = value.x;
|
||||
this.m12 = value.y;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建一个新的matrix, 它包含两个矩阵的和。
|
||||
* @param matrix1
|
||||
* @param matrix2
|
||||
*/
|
||||
public static add(matrix1: Matrix2D, matrix2: Matrix2D){
|
||||
matrix1.m11 += matrix2.m11;
|
||||
matrix1.m12 += matrix2.m12;
|
||||
|
||||
matrix1.m21 += matrix2.m21;
|
||||
matrix1.m22 += matrix2.m22;
|
||||
|
||||
matrix1.m31 += matrix2.m31;
|
||||
matrix1.m32 += matrix2.m32;
|
||||
|
||||
return matrix1;
|
||||
}
|
||||
|
||||
public static divide(matrix1: Matrix2D, matrix2: Matrix2D){
|
||||
matrix1.m11 /= matrix2.m11;
|
||||
matrix1.m12 /= matrix2.m12;
|
||||
|
||||
matrix1.m21 /= matrix2.m21;
|
||||
matrix1.m22 /= matrix2.m22;
|
||||
|
||||
matrix1.m31 /= matrix2.m31;
|
||||
matrix1.m32 /= matrix2.m32;
|
||||
|
||||
return matrix1;
|
||||
}
|
||||
|
||||
public static multiply(matrix1: Matrix2D, matrix2: Matrix2D){
|
||||
let result = new Matrix2D();
|
||||
|
||||
let m11 = ( matrix1.m11 * matrix2.m11 ) + ( matrix1.m12 * matrix2.m21 );
|
||||
let 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 );
|
||||
|
||||
let m31 = ( matrix1.m31 * matrix2.m11 ) + ( matrix1.m32 * matrix2.m21 ) + matrix2.m31;
|
||||
let m32 = ( matrix1.m31 * matrix2.m12 ) + ( matrix1.m32 * matrix2.m22 ) + matrix2.m32;
|
||||
|
||||
result.m11 = m11;
|
||||
result.m12 = m12;
|
||||
|
||||
result.m21 = m21;
|
||||
result.m22 = m22;
|
||||
|
||||
result.m31 = m31;
|
||||
result.m32 = m32;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public static multiplyTranslation(matrix: Matrix2D, x: number, y: number){
|
||||
let trans = Matrix2D.createTranslation(x, y);
|
||||
return Matrix2D.multiply(matrix, trans);
|
||||
}
|
||||
|
||||
public determinant(){
|
||||
return this.m11 * this.m22 - this.m12 * this.m21;
|
||||
}
|
||||
|
||||
public static invert(matrix: Matrix2D, result: Matrix2D = new Matrix2D()){
|
||||
let det = 1 / matrix.determinant();
|
||||
|
||||
result.m11 = matrix.m22 * det;
|
||||
result.m12 = -matrix.m12 * det;
|
||||
|
||||
result.m21 = -matrix.m21 * det;
|
||||
result.m22 = matrix.m11 * det;
|
||||
|
||||
result.m31 = (matrix.m32 * matrix.m21 - matrix.m31 * matrix.m22) * det;
|
||||
result.m32 = -(matrix.m32 * matrix.m11 - matrix.m31 * matrix.m12) * det;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建一个新的tranlation
|
||||
* @param xPosition
|
||||
* @param yPosition
|
||||
*/
|
||||
public static createTranslation(xPosition: number, yPosition: number){
|
||||
let result = new Matrix2D();
|
||||
|
||||
result.m11 = 1;
|
||||
result.m12 = 0;
|
||||
|
||||
result.m21 = 0;
|
||||
result.m22 = 1;
|
||||
|
||||
result.m31 = xPosition;
|
||||
result.m32 = yPosition;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据position 创建一个translation
|
||||
* @param position
|
||||
*/
|
||||
public static createTranslationVector(position: Vector2){
|
||||
return this.createTranslation(position.x, position.y);
|
||||
}
|
||||
|
||||
public static createRotation(radians: number, result?: Matrix2D){
|
||||
result = new Matrix2D();
|
||||
|
||||
let val1 = Math.cos(radians);
|
||||
let val2 = Math.sin(radians);
|
||||
|
||||
result.m11 = val1;
|
||||
result.m12 = val2;
|
||||
result.m21 = -val2;
|
||||
result.m22 = val1;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public static createScale(xScale: number, yScale: number, result: Matrix2D = new Matrix2D()){
|
||||
result.m11 = xScale;
|
||||
result.m12 = 0;
|
||||
|
||||
result.m21 = 0;
|
||||
result.m22 = yScale;
|
||||
|
||||
result.m31 = 0;
|
||||
result.m32 = 0;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public toEgretMatrix(): egret.Matrix{
|
||||
let matrix = new egret.Matrix(this.m11, this.m12, this.m21, this.m22, this.m31, this.m32);
|
||||
return matrix;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,183 +1,185 @@
|
||||
class Rectangle extends egret.Rectangle {
|
||||
/**
|
||||
* 获取矩形的最大点,即右下角
|
||||
*/
|
||||
public get max() {
|
||||
return new Vector2(this.right, this.bottom);
|
||||
}
|
||||
module es {
|
||||
export class Rectangle extends egret.Rectangle {
|
||||
/**
|
||||
* 获取矩形的最大点,即右下角
|
||||
*/
|
||||
public get max() {
|
||||
return new Vector2(this.right, this.bottom);
|
||||
}
|
||||
|
||||
/** 中心点坐标 */
|
||||
public get center() {
|
||||
return new Vector2(this.x + (this.width / 2), this.y + (this.height / 2));
|
||||
}
|
||||
/** 中心点坐标 */
|
||||
public get center() {
|
||||
return new Vector2(this.x + (this.width / 2), this.y + (this.height / 2));
|
||||
}
|
||||
|
||||
/** 左上角的坐标 */
|
||||
public get location() {
|
||||
return new Vector2(this.x, this.y);
|
||||
}
|
||||
/** 左上角的坐标 */
|
||||
public set location(value: Vector2) {
|
||||
this.x = value.x;
|
||||
this.y = value.y;
|
||||
}
|
||||
/** 左上角的坐标 */
|
||||
public get location() {
|
||||
return new Vector2(this.x, this.y);
|
||||
}
|
||||
/** 左上角的坐标 */
|
||||
public set location(value: Vector2) {
|
||||
this.x = value.x;
|
||||
this.y = value.y;
|
||||
}
|
||||
|
||||
public get size() {
|
||||
return new Vector2(this.width, this.height);
|
||||
}
|
||||
public get size() {
|
||||
return new Vector2(this.width, this.height);
|
||||
}
|
||||
|
||||
public set size(value: Vector2) {
|
||||
this.width = value.x;
|
||||
this.height = value.y;
|
||||
}
|
||||
public set size(value: Vector2) {
|
||||
this.width = value.x;
|
||||
this.height = value.y;
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否与另一个矩形相交
|
||||
* @param value
|
||||
*/
|
||||
public intersects(value: egret.Rectangle) {
|
||||
return value.left < this.right &&
|
||||
this.left < value.right &&
|
||||
value.top < this.bottom &&
|
||||
this.top < value.bottom;
|
||||
}
|
||||
/**
|
||||
* 是否与另一个矩形相交
|
||||
* @param value
|
||||
*/
|
||||
public intersects(value: egret.Rectangle) {
|
||||
return value.left < this.right &&
|
||||
this.left < value.right &&
|
||||
value.top < this.bottom &&
|
||||
this.top < value.bottom;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所提供的矩形是否在此矩形的边界内
|
||||
* @param value
|
||||
*/
|
||||
public containsRect(value: Rectangle) {
|
||||
return ((((this.x <= value.x) && (value.x < (this.x + this.width))) &&
|
||||
(this.y <= value.y)) &&
|
||||
(value.y < (this.y + this.height)));
|
||||
}
|
||||
/**
|
||||
* 获取所提供的矩形是否在此矩形的边界内
|
||||
* @param value
|
||||
*/
|
||||
public containsRect(value: Rectangle) {
|
||||
return ((((this.x <= value.x) && (value.x < (this.x + this.width))) &&
|
||||
(this.y <= value.y)) &&
|
||||
(value.y < (this.y + this.height)));
|
||||
}
|
||||
|
||||
public getHalfSize() {
|
||||
return new Vector2(this.width * 0.5, this.height * 0.5);
|
||||
}
|
||||
public getHalfSize() {
|
||||
return new Vector2(this.width * 0.5, this.height * 0.5);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建一个矩形的最小/最大点(左上角,右下角的点)
|
||||
* @param minX
|
||||
* @param minY
|
||||
* @param maxX
|
||||
* @param maxY
|
||||
*/
|
||||
public static fromMinMax(minX: number, minY: number, maxX: number, maxY: number) {
|
||||
return new Rectangle(minX, minY, maxX - minX, maxY - minY);
|
||||
}
|
||||
/**
|
||||
* 创建一个矩形的最小/最大点(左上角,右下角的点)
|
||||
* @param minX
|
||||
* @param minY
|
||||
* @param maxX
|
||||
* @param maxY
|
||||
*/
|
||||
public static fromMinMax(minX: number, minY: number, maxX: number, maxY: number) {
|
||||
return new Rectangle(minX, minY, maxX - minX, maxY - minY);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取矩形边界上与给定点最近的点
|
||||
* @param point
|
||||
*/
|
||||
public getClosestPointOnRectangleBorderToPoint(point: Vector2): { res: Vector2, edgeNormal: Vector2 } {
|
||||
let edgeNormal = Vector2.zero;
|
||||
/**
|
||||
* 获取矩形边界上与给定点最近的点
|
||||
* @param point
|
||||
*/
|
||||
public getClosestPointOnRectangleBorderToPoint(point: Vector2): { res: Vector2, edgeNormal: Vector2 } {
|
||||
let edgeNormal = Vector2.zero;
|
||||
|
||||
// 对于每个轴,如果点在盒子外面
|
||||
let res = new Vector2();
|
||||
res.x = MathHelper.clamp(point.x, this.left, this.right);
|
||||
res.y = MathHelper.clamp(point.y, this.top, this.bottom);
|
||||
// 对于每个轴,如果点在盒子外面
|
||||
let res = new Vector2();
|
||||
res.x = MathHelper.clamp(point.x, this.left, this.right);
|
||||
res.y = MathHelper.clamp(point.y, this.top, this.bottom);
|
||||
|
||||
// 如果点在矩形内,我们需要推res到边界,因为它将在矩形内
|
||||
if (this.contains(res.x, res.y)) {
|
||||
let dl = res.x - this.left;
|
||||
let dr = this.right - res.x;
|
||||
let dt = res.y - this.top;
|
||||
let db = this.bottom - res.y;
|
||||
// 如果点在矩形内,我们需要推res到边界,因为它将在矩形内
|
||||
if (this.contains(res.x, res.y)) {
|
||||
let dl = res.x - this.left;
|
||||
let dr = this.right - res.x;
|
||||
let dt = res.y - this.top;
|
||||
let db = this.bottom - res.y;
|
||||
|
||||
let min = Math.min(dl, dr, dt, db);
|
||||
if (min == dt) {
|
||||
res.y = this.top;
|
||||
edgeNormal.y = -1;
|
||||
} else if (min == db) {
|
||||
res.y = this.bottom;
|
||||
edgeNormal.y = 1;
|
||||
} else if (min == dl) {
|
||||
res.x = this.left;
|
||||
edgeNormal.x = -1;
|
||||
let min = Math.min(dl, dr, dt, db);
|
||||
if (min == dt) {
|
||||
res.y = this.top;
|
||||
edgeNormal.y = -1;
|
||||
} else if (min == db) {
|
||||
res.y = this.bottom;
|
||||
edgeNormal.y = 1;
|
||||
} else if (min == dl) {
|
||||
res.x = this.left;
|
||||
edgeNormal.x = -1;
|
||||
} else {
|
||||
res.x = this.right;
|
||||
edgeNormal.x = 1;
|
||||
}
|
||||
} else {
|
||||
res.x = this.right;
|
||||
edgeNormal.x = 1;
|
||||
if (res.x == this.left) edgeNormal.x = -1;
|
||||
if (res.x == this.right) edgeNormal.x = 1;
|
||||
if (res.y == this.top) edgeNormal.y = -1;
|
||||
if (res.y == this.bottom) edgeNormal.y = 1;
|
||||
}
|
||||
} else {
|
||||
if (res.x == this.left) edgeNormal.x = -1;
|
||||
if (res.x == this.right) edgeNormal.x = 1;
|
||||
if (res.y == this.top) edgeNormal.y = -1;
|
||||
if (res.y == this.bottom) edgeNormal.y = 1;
|
||||
|
||||
return { res: res, edgeNormal: edgeNormal };
|
||||
}
|
||||
|
||||
return { res: res, edgeNormal: edgeNormal };
|
||||
}
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public getClosestPointOnBoundsToOrigin() {
|
||||
let max = this.max;
|
||||
let minDist = Math.abs(this.location.x);
|
||||
let boundsPoint = new Vector2(this.location.x, 0);
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public getClosestPointOnBoundsToOrigin() {
|
||||
let max = this.max;
|
||||
let minDist = Math.abs(this.location.x);
|
||||
let boundsPoint = new Vector2(this.location.x, 0);
|
||||
if (Math.abs(max.x) < minDist) {
|
||||
minDist = Math.abs(max.x);
|
||||
boundsPoint.x = max.x;
|
||||
boundsPoint.y = 0;
|
||||
}
|
||||
|
||||
if (Math.abs(max.x) < minDist) {
|
||||
minDist = Math.abs(max.x);
|
||||
boundsPoint.x = max.x;
|
||||
boundsPoint.y = 0;
|
||||
if (Math.abs(max.y) < minDist) {
|
||||
minDist = Math.abs(max.y);
|
||||
boundsPoint.x = 0;
|
||||
boundsPoint.y = max.y;
|
||||
}
|
||||
|
||||
if (Math.abs(this.location.y) < minDist) {
|
||||
minDist = Math.abs(this.location.y);
|
||||
boundsPoint.x = 0;
|
||||
boundsPoint.y = this.location.y;
|
||||
}
|
||||
|
||||
return boundsPoint;
|
||||
}
|
||||
|
||||
if (Math.abs(max.y) < minDist) {
|
||||
minDist = Math.abs(max.y);
|
||||
boundsPoint.x = 0;
|
||||
boundsPoint.y = max.y;
|
||||
public calculateBounds(parentPosition: Vector2, position: Vector2, origin: Vector2, scale: Vector2, rotation: number, width: number, height: number){
|
||||
this.x = parentPosition.x + position.x - origin.x * scale.x;
|
||||
this.y = parentPosition.y + position.y - origin.y * scale.y;
|
||||
this.width = width * scale.x;
|
||||
this.height = height * scale.y;
|
||||
}
|
||||
|
||||
if (Math.abs(this.location.y) < minDist) {
|
||||
minDist = Math.abs(this.location.y);
|
||||
boundsPoint.x = 0;
|
||||
boundsPoint.y = this.location.y;
|
||||
/**
|
||||
* 将egret矩形转化为Rectangle
|
||||
* @param rect
|
||||
*/
|
||||
public setEgretRect(rect: egret.Rectangle): Rectangle{
|
||||
this.x = rect.x;
|
||||
this.y = rect.y;
|
||||
this.width = rect.width;
|
||||
this.height = rect.height;
|
||||
return this;
|
||||
}
|
||||
|
||||
return boundsPoint;
|
||||
}
|
||||
/**
|
||||
* 给定多边形的点,计算边界
|
||||
* @param points
|
||||
*/
|
||||
public static rectEncompassingPoints(points: Vector2[]) {
|
||||
// 我们需要求出x/y的最小值/最大值
|
||||
let minX = Number.POSITIVE_INFINITY;
|
||||
let minY = Number.POSITIVE_INFINITY;
|
||||
let maxX = Number.NEGATIVE_INFINITY;
|
||||
let maxY = Number.NEGATIVE_INFINITY;
|
||||
|
||||
public calculateBounds(parentPosition: Vector2, position: Vector2, origin: Vector2, scale: Vector2, rotation: number, width: number, height: number){
|
||||
this.x = parentPosition.x + position.x - origin.x * scale.x;
|
||||
this.y = parentPosition.y + position.y - origin.y * scale.y;
|
||||
this.width = width * scale.x;
|
||||
this.height = height * scale.y;
|
||||
}
|
||||
for (let i = 0; i < points.length; i++) {
|
||||
let pt = points[i];
|
||||
|
||||
/**
|
||||
* 将egret矩形转化为Rectangle
|
||||
* @param rect
|
||||
*/
|
||||
public setEgretRect(rect: egret.Rectangle): Rectangle{
|
||||
this.x = rect.x;
|
||||
this.y = rect.y;
|
||||
this.width = rect.width;
|
||||
this.height = rect.height;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 给定多边形的点,计算边界
|
||||
* @param points
|
||||
*/
|
||||
public static rectEncompassingPoints(points: Vector2[]) {
|
||||
// 我们需要求出x/y的最小值/最大值
|
||||
let minX = Number.POSITIVE_INFINITY;
|
||||
let minY = Number.POSITIVE_INFINITY;
|
||||
let maxX = Number.NEGATIVE_INFINITY;
|
||||
let maxY = Number.NEGATIVE_INFINITY;
|
||||
if (pt.x < minX) minX = pt.x;
|
||||
if (pt.x > maxX) maxX = pt.x;
|
||||
|
||||
for (let i = 0; i < points.length; i++) {
|
||||
let pt = points[i];
|
||||
if (pt.y < minY) minY = pt.y;
|
||||
if (pt.y > maxY) maxY = pt.y;
|
||||
}
|
||||
|
||||
if (pt.x < minX) minX = pt.x;
|
||||
if (pt.x > maxX) maxX = pt.x;
|
||||
|
||||
if (pt.y < minY) minY = pt.y;
|
||||
if (pt.y > maxY) maxY = pt.y;
|
||||
return this.fromMinMax(minX, minY, maxX, maxY);
|
||||
}
|
||||
|
||||
return this.fromMinMax(minX, minY, maxX, maxY);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,183 +1,185 @@
|
||||
/** 2d 向量 */
|
||||
class Vector2 {
|
||||
public x: number = 0;
|
||||
public y: number = 0;
|
||||
module es {
|
||||
/** 2d 向量 */
|
||||
export class Vector2 {
|
||||
public x: number = 0;
|
||||
public y: number = 0;
|
||||
|
||||
private static readonly unitYVector = new Vector2(0, 1);
|
||||
private static readonly unitXVector = new Vector2(1, 0);
|
||||
private static readonly unitVector2 = new Vector2(1, 1);
|
||||
private static readonly zeroVector2 = new Vector2(0, 0);
|
||||
public static get zero(){
|
||||
return Vector2.zeroVector2;
|
||||
private static readonly unitYVector = new Vector2(0, 1);
|
||||
private static readonly unitXVector = new Vector2(1, 0);
|
||||
private static readonly unitVector2 = new Vector2(1, 1);
|
||||
private static readonly zeroVector2 = new Vector2(0, 0);
|
||||
public static get zero(){
|
||||
return Vector2.zeroVector2;
|
||||
}
|
||||
|
||||
public static get one(){
|
||||
return Vector2.unitVector2;
|
||||
}
|
||||
|
||||
public static get unitX(){
|
||||
return Vector2.unitXVector;
|
||||
}
|
||||
|
||||
public static get unitY(){
|
||||
return Vector2.unitYVector;
|
||||
}
|
||||
|
||||
/**
|
||||
* 从两个值构造一个带有X和Y的二维向量。
|
||||
* @param x 二维空间中的x坐标
|
||||
* @param y 二维空间的y坐标
|
||||
*/
|
||||
constructor(x? : number, y?: number){
|
||||
this.x = x ? x : 0;
|
||||
this.y = y ? y : this.x;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param value1
|
||||
* @param value2
|
||||
*/
|
||||
public static add(value1: Vector2, value2: Vector2){
|
||||
let result: Vector2 = new Vector2(0, 0);
|
||||
result.x = value1.x + value2.x;
|
||||
result.y = value1.y + value2.y;
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param value1
|
||||
* @param value2
|
||||
*/
|
||||
public static divide(value1: Vector2, value2: Vector2){
|
||||
let result: Vector2 = new Vector2(0, 0);
|
||||
result.x = value1.x / value2.x;
|
||||
result.y = value1.y / value2.y;
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param value1
|
||||
* @param value2
|
||||
*/
|
||||
public static multiply(value1: Vector2, value2: Vector2){
|
||||
let result: Vector2 = new Vector2(0, 0);
|
||||
result.x = value1.x * value2.x;
|
||||
result.y = value1.y * value2.y;
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param value1
|
||||
* @param value2
|
||||
*/
|
||||
public static subtract(value1: Vector2, value2: Vector2){
|
||||
let result: Vector2 = new Vector2(0, 0);
|
||||
result.x = value1.x - value2.x;
|
||||
result.y = value1.y - value2.y;
|
||||
return result;
|
||||
}
|
||||
|
||||
/** 变成一个方向相同的单位向量 */
|
||||
public normalize(){
|
||||
let val = 1 / Math.sqrt((this.x * this.x) + (this.y * this.y));
|
||||
this.x *= val;
|
||||
this.y *= val;
|
||||
}
|
||||
|
||||
/** 返回它的长度 */
|
||||
public length(){
|
||||
return Math.sqrt((this.x * this.x) + (this.y * this.y));
|
||||
}
|
||||
|
||||
/** 对x和y值四舍五入 */
|
||||
public round(): Vector2{
|
||||
return new Vector2(Math.round(this.x), Math.round(this.y));
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建一个新的Vector2
|
||||
* 它包含来自另一个向量的标准化值。
|
||||
* @param value
|
||||
*/
|
||||
public static normalize(value: Vector2){
|
||||
let val = 1 / Math.sqrt((value.x * value.x) + (value.y * value.y));
|
||||
value.x *= val;
|
||||
value.y *= val;
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回两个向量的点积
|
||||
* @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);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param value1
|
||||
* @param min
|
||||
* @param max
|
||||
*/
|
||||
public static clamp(value1: Vector2, min: Vector2, max: Vector2){
|
||||
return new Vector2(MathHelper.clamp(value1.x, min.x, max.x),
|
||||
MathHelper.clamp(value1.y, min.y, max.y));
|
||||
}
|
||||
|
||||
/**
|
||||
* 包含指定向量的线性插值
|
||||
* @param value1 第一个向量
|
||||
* @param value2 第二个向量
|
||||
* @param amount 权重值(0.0到1.0之间)
|
||||
*/
|
||||
public static lerp(value1: Vector2, value2: Vector2, amount: number){
|
||||
return new Vector2(MathHelper.lerp(value1.x, value2.x, amount), MathHelper.lerp(value1.y, value2.y, amount));
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param position
|
||||
* @param matrix
|
||||
*/
|
||||
public static transform(position: Vector2, matrix: Matrix2D){
|
||||
return new Vector2((position.x * matrix.m11) + (position.y * matrix.m21), (position.x * matrix.m12) + (position.y * matrix.m22));
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回两个向量之间的距离
|
||||
* @param value1
|
||||
* @param value2
|
||||
*/
|
||||
public static distance(value1: Vector2, value2: Vector2){
|
||||
let v1 = value1.x - value2.x, v2 = value1.y - value2.y;
|
||||
return Math.sqrt((v1 * v1) + (v2 * v2));
|
||||
}
|
||||
|
||||
/**
|
||||
* 矢量反演的结果
|
||||
* @param value
|
||||
*/
|
||||
public static negate(value: Vector2){
|
||||
let result: Vector2 = new Vector2();
|
||||
result.x = -value.x;
|
||||
result.y = -value.y;
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
public static get one(){
|
||||
return Vector2.unitVector2;
|
||||
}
|
||||
|
||||
public static get unitX(){
|
||||
return Vector2.unitXVector;
|
||||
}
|
||||
|
||||
public static get unitY(){
|
||||
return Vector2.unitYVector;
|
||||
}
|
||||
|
||||
/**
|
||||
* 从两个值构造一个带有X和Y的二维向量。
|
||||
* @param x 二维空间中的x坐标
|
||||
* @param y 二维空间的y坐标
|
||||
*/
|
||||
constructor(x? : number, y?: number){
|
||||
this.x = x ? x : 0;
|
||||
this.y = y ? y : this.x;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param value1
|
||||
* @param value2
|
||||
*/
|
||||
public static add(value1: Vector2, value2: Vector2){
|
||||
let result: Vector2 = new Vector2(0, 0);
|
||||
result.x = value1.x + value2.x;
|
||||
result.y = value1.y + value2.y;
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param value1
|
||||
* @param value2
|
||||
*/
|
||||
public static divide(value1: Vector2, value2: Vector2){
|
||||
let result: Vector2 = new Vector2(0, 0);
|
||||
result.x = value1.x / value2.x;
|
||||
result.y = value1.y / value2.y;
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param value1
|
||||
* @param value2
|
||||
*/
|
||||
public static multiply(value1: Vector2, value2: Vector2){
|
||||
let result: Vector2 = new Vector2(0, 0);
|
||||
result.x = value1.x * value2.x;
|
||||
result.y = value1.y * value2.y;
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param value1
|
||||
* @param value2
|
||||
*/
|
||||
public static subtract(value1: Vector2, value2: Vector2){
|
||||
let result: Vector2 = new Vector2(0, 0);
|
||||
result.x = value1.x - value2.x;
|
||||
result.y = value1.y - value2.y;
|
||||
return result;
|
||||
}
|
||||
|
||||
/** 变成一个方向相同的单位向量 */
|
||||
public normalize(){
|
||||
let val = 1 / Math.sqrt((this.x * this.x) + (this.y * this.y));
|
||||
this.x *= val;
|
||||
this.y *= val;
|
||||
}
|
||||
|
||||
/** 返回它的长度 */
|
||||
public length(){
|
||||
return Math.sqrt((this.x * this.x) + (this.y * this.y));
|
||||
}
|
||||
|
||||
/** 对x和y值四舍五入 */
|
||||
public round(): Vector2{
|
||||
return new Vector2(Math.round(this.x), Math.round(this.y));
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建一个新的Vector2
|
||||
* 它包含来自另一个向量的标准化值。
|
||||
* @param value
|
||||
*/
|
||||
public static normalize(value: Vector2){
|
||||
let val = 1 / Math.sqrt((value.x * value.x) + (value.y * value.y));
|
||||
value.x *= val;
|
||||
value.y *= val;
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回两个向量的点积
|
||||
* @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);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param value1
|
||||
* @param min
|
||||
* @param max
|
||||
*/
|
||||
public static clamp(value1: Vector2, min: Vector2, max: Vector2){
|
||||
return new Vector2(MathHelper.clamp(value1.x, min.x, max.x),
|
||||
MathHelper.clamp(value1.y, min.y, max.y));
|
||||
}
|
||||
|
||||
/**
|
||||
* 包含指定向量的线性插值
|
||||
* @param value1 第一个向量
|
||||
* @param value2 第二个向量
|
||||
* @param amount 权重值(0.0到1.0之间)
|
||||
*/
|
||||
public static lerp(value1: Vector2, value2: Vector2, amount: number){
|
||||
return new Vector2(MathHelper.lerp(value1.x, value2.x, amount), MathHelper.lerp(value1.y, value2.y, amount));
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param position
|
||||
* @param matrix
|
||||
*/
|
||||
public static transform(position: Vector2, matrix: Matrix2D){
|
||||
return new Vector2((position.x * matrix.m11) + (position.y * matrix.m21), (position.x * matrix.m12) + (position.y * matrix.m22));
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回两个向量之间的距离
|
||||
* @param value1
|
||||
* @param value2
|
||||
*/
|
||||
public static distance(value1: Vector2, value2: Vector2){
|
||||
let v1 = value1.x - value2.x, v2 = value1.y - value2.y;
|
||||
return Math.sqrt((v1 * v1) + (v2 * v2));
|
||||
}
|
||||
|
||||
/**
|
||||
* 矢量反演的结果
|
||||
* @param value
|
||||
*/
|
||||
public static negate(value: Vector2){
|
||||
let result: Vector2 = new Vector2();
|
||||
result.x = -value.x;
|
||||
result.y = -value.y;
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
class Vector3 {
|
||||
public x: number;
|
||||
public y: number;
|
||||
public z: number;
|
||||
module es {
|
||||
export class Vector3 {
|
||||
public x: number;
|
||||
public y: number;
|
||||
public z: number;
|
||||
|
||||
constructor(x: number, y: number, z: number){
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.z = z;
|
||||
constructor(x: number, y: number, z: number){
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.z = z;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user