import ExtMath;
class Vector {
public var x:Number;
//声明公用属性y
public var y:Number;
//定义构造函数
public function Vector(x:Number, y:Number) {
x = x;
y = y;
}
//定义公用方法reset,即重设方法
public function reset(x:Number, y:Number) {
with (this) {
__constructor__(x, y);
}
}
//定义公用方法getClone,即克隆方法
public function getClone() {
with (this) {
return new __constructor__(x, y);
}
}
//定义公用方法equals,即比较是否相等的方法
public function equals(v:Vector) {
return (this.x == v.x && this.y == v.y);
}
//定义公用方法plus,即向量求和方法
public function plus(v:Vector) {
x += v.x;
y += v.y;
}
//定义公用方法plusNew,即返回向量和的方法
public function plusNew(v:Vector) {
with (this) {
return new __constructor__(x+v.x, y+v.y);
}
}
//定义公用方法minus,即向量求减方法
public function minus(v:Vector) {
x -= v.x;
y -= v.y;
}
//定义公用方法minusNew,即返回向量差方法
public function minusNew(v:Vector) {
with (this) {
return new __constructor__(x-v.x, y-v.y);
}
}
//定义公用方法negate,即向量求逆方法
public function negate() {
x = -x;
y = -y;
}
//定义公用方法negateNew,即返回逆向量方法
public function negateNew():Vector {
with (this) {
return new __constructor__(-x, -y);
}
}
public function scale(s:Number) {
x *= s;
y *= s;
}
public function scaleNew(s:Number):Vector {
with (this) {
return new __constructor__(s);
}
}
public function getLength():Number {
return Math.sqrt(x*x+y*y);
}
public function setLength(len:Number) {
var r = this.getLength();
if (r) {
this.scale(len/r);
} else {
//如果r==0
this.x = len;
}
}
public function getAngle() {
return ExtMath.atan2D(x, y);
}
public function setAngle(angle:Number) {
var r = getLength();
x = r*ExtMath.cosD(angle);
y = r*ExtMath.sinD(angle);
}
public function rotate(angle:Number) {
var ca = ExtMath.cosD(angle);
var sa = ExtMath.sinD(angle);
with (this) {
var rx = x*ca-y*sa;
var ry = x*sa+y*ca;
x = rx;
y = ry;
}
}
public function rotateNew(angle:Number) {
with (this) {
var v = new __constructor__(x, y);
v.rotate(angle);
return v;
}
}
public function dot(v:Vector) {
//点积结果为0,两个向量就是相互垂直的
return (x*v.x+y*v.y);
}
public function isPerpTo(v:Vector) {
return (this.dot(v) == 0);
}
public function getNormal() {
with (this) {
return new __constructor__(-y, x);
}
}
public function angleBetween(v:Vector) {
var dp = dot(v);
var cosAngle = dp/(getLength()*v.getLength());
return ExtMath.acosD(cosAngle);
}
} |