class Vector2 extends egret.Point{
private _x:number;
private _y:number;
public constructor(x:number = 0, y:number = 0){
super(x, y)
}
public SetXY(x:number, y:number):Vector2{
this.x = x;
this.y = y;
return this;
}
public SetValue(v:Vector2){
if(v == null){
console.log(111);
}
this.x = v.x;
this.y = v.y;
return this;
}
//偏移
public Offset(dv:Vector2):Vector2{
this.x += dv.x;
this.y += dv.y;
return this;
}
//偏移
public OffsetXY(dx:number, dy:number):Vector2{
this.x += dx;
this.y += dy;
return this;
}
//乘以
public Multiply(value:number):Vector2{
this.x *= value;
this.y *= value;
return this;
}
//是否与另一个Vector2相等
public Equals(v:Vector2, eps:number = Calculate.EPS):boolean{
return Math.abs(this.x - v.x) + Math.abs(this.y - v.y) < eps;
}
//标准化
public Normalize():Vector2{
this.normalize(1);
return this;
}
//旋转 A是弧度
public Rotate(A:number):Vector2{
let x = this.x * Math.cos(A) - this.y * Math.sin(A);
let y = this.y * Math.cos(A) + this.x * Math.sin(A);
this.x = x;
this.y = y;
return this;
}
//弧度
public get Radian():number{
return Math.atan2(this.y, this.x);
}
//角度
public get Angle():number{
return OdinMath.ToAngle(this.Radian);
}
//绕着pos点旋转A弧度
public RotateAbout(pos:Vector2, A:number){
let x =(this.x - pos.x) * Math.cos(A)-(this.y - pos.y) * Math.sin(A) + pos.x;
let y =(this.y - pos.y) * Math.cos(A)+(this.x - pos.x) * Math.sin(A) + pos.y;
this.SetXY(x, y);
return this;
}
//克隆
public Clone():Vector2{
return new Vector2(this.x, this.y);
}
public toString():string{
return '(' + this.x + ',' + this.y + ')';
}
//用一个point构建一个Vector2
public static CreatByPoint(point:egret.Point):Vector2{
return new Vector2(point.x, point.y);
}
public static get Zero():Vector2{
return new Vector2(0, 0);
}
public static get One():Vector2{
return new Vector2(1, 1);
}
//加
public static Add(a:Vector2, b:Vector2, result?:Vector2):Vector2{
if(result == null)
return new Vector2(a.x + b.x, a.y + b.y);
result.x = a.x + b.x;
result.y = a.y + b.y;
return result;
}
//减
public static Subtract(a:Vector2, b:Vector2, result?:Vector2):Vector2{
if(result == null)
return new Vector2(a.x - b.x, a.y - b.y);
result.x = a.x - b.x;
result.y = a.y - b.y;
return result;
}
//叉乘
public static Cross(a:Vector2, b:Vector2):number{
return a.x * b.y - b.x * a.y;
}
//点乘
public static Dot(a:Vector2, b:Vector2):number{
return a.x * b.x + a.y * b.y;
}
//距离
public static Distance(a:Vector2, b:Vector2):number{
return Math.pow((a.x - b.x)*(a.x - b.x) + (a.y - b.y)*(a.y - b.y), 0.5);
}
//距离的平方
public static DistanceSquare(a:Vector2, b:Vector2):number{
return (a.x - b.x)*(a.x - b.x) + (a.y - b.y)*(a.y - b.y);
}
//距离的平方
public static DistanceSquare2(x1:number, y1:number, x2:number, y2:number):number{
return (x1 - x2)*(x1 - x2) + (y1 - y2)*(y1 - y2);
}
//世界坐标转本地坐标
public static ToLocal(dis:egret.DisplayObject, globalPos:Vector2, result?:Vector2):Vector2{
if(result != null){
dis.globalToLocal(globalPos.x, globalPos.y, result);
return result;
} else{
var pos = dis.globalToLocal(globalPos.x, globalPos.y);
return Vector2.CreatByPoint(pos);
}
}
//本地坐标转世界坐标
public static ToGlobal(dis:egret.DisplayObject, localPos:Vector2, result?:Vector2):Vector2{
if(result != null){
dis.localToGlobal(localPos.x, localPos.y, result);
return result;
} else{
var pos = dis.localToGlobal(localPos.x, localPos.y);
return Vector2.CreatByPoint(pos);
}
}
//获取从startPos到targetPos的方向弧度
public static GetRadian(startPos:Vector2, targetPos:Vector2):number{
return (Math.atan2(targetPos.y - startPos.y, targetPos.x - startPos.x) + Math.PI * 2) % (Math.PI * 2);
}
//判断两个向量是否相等
public static Equal(a:Vector2, b:Vector2):boolean{
return Math.abs(a.x - b.x) + Math.abs(a.y - b.y) < 0.00001;
}
//获取中间点
public static GetMiddleVector(a:Vector2, b:Vector2){
return new Vector2((a.x + b.x) * 0.5, (a.y + b.y) * 0.5);
}
}
白鹭向量工具类(Vector2)
最新推荐文章于 2024-04-24 09:44:33 发布