/*///////vector方法列表/////
1.toString()
2.reset(x,y)//重新赋值例如:myv.reset(2,3);
3.getclone()//复制.例如myv2=myv1.getclone();
4.eqV(v)//是否相等例如if(v1.eqv(v2)){};
5.addV(v),addVNew(v)和minusV(v),minusVNew(v)//加法减法v1.addV(v2);v3=v1.addVNew(v2);
6. scaleV(n)和scaleVNew(n)//伸长.见上
7.getLength()和setLength(n)//a=v1.getLength();v1.setLength(30);
8.getAngle()和setAngle(n)//得到与设置角度.
9.rot(n)和rotNew(n)//旋转
10.dot(v)//内积x=v1.dot(v2)
11.angleBetween(v)//得到夹角例如x=v1.angleBetween(v2);
*/
class vector {
var x:Number;
var y:Number;
// 用于返回属性值的方法
function toString():String {
return ("["+x+","+y+"]");
}
//改变属性值的方法.
function reset(getx:Number, gety:Number):Void {
x = getx;
y = gety;
}
//克隆向量.与mx不同//!!!!!!
function getclone():vector {
return new vector(x, y);
}
//比较相等否.
function eqV(getVector:vector):Boolean {
return (x == getVector.x && y == getVector.y);
}
//向量加法.
function addV(getVector:vector):Void {
x += getVector.x;
y += getVector.y;
}
//向量加法得到新向量.
function addVNew(getVector:vector):vector {
return new vector(x+getVector.x, y+getVector.y);
}
//向量减法
function minusV(getVector:vector):Void {
x -= getVector.x;
y -= getVector.y;
}
//向量减法得到新向量.
function minusVNew(getVector:vector):vector {
return new vector(x-getVector.x, y-getVector.y);
}
//向量缩放
function scaleV(n:Number):Void {
x *= n;
y *= n;
}
//向量缩放得到新向量.
function scaleVNew(n:Number):vector {
return new vector(x*n, y*n);
}
//得到向量的长度
function getLength():Number {
return Math.sqrt(x*x+y*y);
}
//设置向量长度
function setLength(len:Number):Void {
var r = this.getLength();
if (r) {
this.scaleV(len/r);
} else {
this.x = len;
}
}
//得到向量角度
function getAngle():Number {
return (180/Math.PI)*Math.atan2(y, x);
}
//设置向量角度
function setAngle(angle:Number):Void {
var r = this.getLength();
var tem = angle*(Math.PI/180);
x = r*Math.cos(tem);
y = r*Math.sin(tem);
}
//旋转.
function rot(angle:Number):Void {
var r = this.getLength();
var tem = angle*(Math.PI/180);
var ca = Math.cos(tem);
var sa = Math.sin(tem);
var rx = x*ca-y*sa;
var ry = x*sa+y*ca;
x = rx;
y = ry;
}
//旋转并得到新向量.
function rotNew(angle:Number):vector {
var v = new vector(x, y);
v.rotate(angle);
return v;
}
//内积
function dot(v:vector):Number {
return x*v.x+y*v.y;
}
//法向量计算省略.可以用rotateNew(90)得到.
//垂直验证省略.用(v1.dot(v2)==0)判断垂直.
//向量夹角.得到向量夹角绝对值.
function angleBetween(v:vector):Number {
var tem = this.dot(v);
return (180/Math.PI)*Math.acos(tem/(this.getLength()*v.getLength()));
}
// 构造函数
function vector(getx:Number, gety:Number) {
x = getx;
y = gety;
}
}
1.toString()
2.reset(x,y)//重新赋值例如:myv.reset(2,3);
3.getclone()//复制.例如myv2=myv1.getclone();
4.eqV(v)//是否相等例如if(v1.eqv(v2)){};
5.addV(v),addVNew(v)和minusV(v),minusVNew(v)//加法减法v1.addV(v2);v3=v1.addVNew(v2);
6. scaleV(n)和scaleVNew(n)//伸长.见上
7.getLength()和setLength(n)//a=v1.getLength();v1.setLength(30);
8.getAngle()和setAngle(n)//得到与设置角度.
9.rot(n)和rotNew(n)//旋转
10.dot(v)//内积x=v1.dot(v2)
11.angleBetween(v)//得到夹角例如x=v1.angleBetween(v2);
*/
class vector {
var x:Number;
var y:Number;
// 用于返回属性值的方法
function toString():String {
return ("["+x+","+y+"]");
}
//改变属性值的方法.
function reset(getx:Number, gety:Number):Void {
x = getx;
y = gety;
}
//克隆向量.与mx不同//!!!!!!
function getclone():vector {
return new vector(x, y);
}
//比较相等否.
function eqV(getVector:vector):Boolean {
return (x == getVector.x && y == getVector.y);
}
//向量加法.
function addV(getVector:vector):Void {
x += getVector.x;
y += getVector.y;
}
//向量加法得到新向量.
function addVNew(getVector:vector):vector {
return new vector(x+getVector.x, y+getVector.y);
}
//向量减法
function minusV(getVector:vector):Void {
x -= getVector.x;
y -= getVector.y;
}
//向量减法得到新向量.
function minusVNew(getVector:vector):vector {
return new vector(x-getVector.x, y-getVector.y);
}
//向量缩放
function scaleV(n:Number):Void {
x *= n;
y *= n;
}
//向量缩放得到新向量.
function scaleVNew(n:Number):vector {
return new vector(x*n, y*n);
}
//得到向量的长度
function getLength():Number {
return Math.sqrt(x*x+y*y);
}
//设置向量长度
function setLength(len:Number):Void {
var r = this.getLength();
if (r) {
this.scaleV(len/r);
} else {
this.x = len;
}
}
//得到向量角度
function getAngle():Number {
return (180/Math.PI)*Math.atan2(y, x);
}
//设置向量角度
function setAngle(angle:Number):Void {
var r = this.getLength();
var tem = angle*(Math.PI/180);
x = r*Math.cos(tem);
y = r*Math.sin(tem);
}
//旋转.
function rot(angle:Number):Void {
var r = this.getLength();
var tem = angle*(Math.PI/180);
var ca = Math.cos(tem);
var sa = Math.sin(tem);
var rx = x*ca-y*sa;
var ry = x*sa+y*ca;
x = rx;
y = ry;
}
//旋转并得到新向量.
function rotNew(angle:Number):vector {
var v = new vector(x, y);
v.rotate(angle);
return v;
}
//内积
function dot(v:vector):Number {
return x*v.x+y*v.y;
}
//法向量计算省略.可以用rotateNew(90)得到.
//垂直验证省略.用(v1.dot(v2)==0)判断垂直.
//向量夹角.得到向量夹角绝对值.
function angleBetween(v:vector):Number {
var tem = this.dot(v);
return (180/Math.PI)*Math.acos(tem/(this.getLength()*v.getLength()));
}
// 构造函数
function vector(getx:Number, gety:Number) {
x = getx;
y = gety;
}
}
回复Comments
作者:
{commentrecontent}