数学转换类

      flash转载 2004-12-31 9:52
角度类

包括角、弧度的转换,三角函数等也是以角度为单位

/*
Flash ActionScript Class File V1.0
@author zjs35
@example
JDgree.agnleToRadian(360)
//2P
*/
class fj.math.JDegree {
//角度转换为弧度
static function angleToRadian(angle:Number):Number {
return angle*(Math.PI/180);
}
//弧度转换为角度
static function radianToAngle(radian:Number):Number {
return radian*(180/Math.PI);
}
//修正角度在360度以内
static function fixAngle(angle:Number) {
return (angle %= 360)<0 ? angle+360 : angle;
}
//以角度为单位计算三角函数值
static function sinD(angle:Number):Number {
return Math.sin(angleToRadian(angle));
}
static function cosD(angle:Number):Number {
return Math.cos(angleToRadian(angle));
}
static function tanD(angle:Number):Number {
return Math.tan(angleToRadian(angle));
}
//返回的值是以角度为单位
static function asinD(radian:Number):Number {
return radianToAngle(Math.acos(radian));
}
static function acosD(radian:Number):Number {
return radianToAngle(Math.acos(radian));
}
static function atanD(radian:Number) {
return radianToAngle(Math.acos(radian));
}
static function atan2D(y:Number, x:Number):Number {
return radianToAngle(Math.atan2(y, x));
}
}


向量类

/*
Flash ActionScript Class File V1.0
@author zjs35
@example
var p=new JVector(2,3)
//p.x=2
*/
import fj.math.JDegree;
//二维向量类
class fj.math.JVector {
public var x:Number;
public var y:Number;
public function JVector(tx, ty) {
x = tx;
y = ty;
}
//重新初始化
public function reSet(tx, ty) {
x = tx;
y = ty;
}
//得到一个复制的向量
public function getClone() {
return new JVector(x, y);
}
//比较两个对象是否相等
public function equals(v:Object) {
return (x == v.x && y == v.y);
}
//两个向量相加
public function plus(v:Object) {
x += v.x;
y += v.y;
}
//两个向量相加的结果返回给另一个新的向量
public function plusNew(v:Object) {
return new JVector(x+v.x, y+v.y);
}
//两个向量相减
public function minus(v:Object) {
x -= v.x;
y -= v.y;
}
public function minusNew(v:Object) {
return new JVector(x-v.x, y-v.y);
}
//向量求逆,即旋转180度
public function negate() {
x = -x;
y = -y;
}
public function negateNew() {
return new JVector(-x, -y);
}
//向量缩放
public function scale(s) {
x *= s;
y *= s;
}
public function scaleNew(s:Number) {
return new JVector(x*s, y*s);
}
//得到向量长度
public function getLength() {
return Math.sqrt(x*x+y*y);
}
//设置向量长度
public function setLength(len) {
var r = getLength();
if (r) {
scale(len/r);
} else {
x = len;
}
}
//得到向量角度
public function getAngle() {
var r = Math.atan2(y, x);
return JDegree.radianToAngle(r);
}
//设置向量的绝对角度
public function setAngle(angle) {
var l = getLength();
var r = Math.round(JDegree.angleToRadian(angle)*1000)/1000;
x = l*Math.cos(r);
y = l*Math.sin(r);
}
//向量旋转相对角度
public function rotate(angle) {
var r = Math.round(JDegree.angleToRadian(angle)*1000)/1000;
var ca = Math.cos(r);
var sa = Math.sin(r);
var rx = x*ca-y*sa;
var ry = x*sa+y*ca;
x = rx;
y = ry;
}
public function rotateNew(angle) {
var v = new JVector(x, y);
v.rotate(angle);
return v;
}
//点积,如结果为0,两向量垂直
public function dot(v) {
return (x*v.x+y*v.y);
}
//法向量,返回与当前向量垂直的向量
public function getNormal() {
return new JVector(-y, x);
}
public function isPerpTo(v) {
return dot(v) == 0;
}
//向量的夹角
public function angleBetween(v) {
var dp = dot(v);
var cosAngle = dp/(getLength()*v.getLength());
var radian = Math.acos(cosAngle);
return JDegree.radianToAngle(radian);
}
}

坐标类

/*
Flash ActionScript Class File V1.0
@author zjs35
@example
var p1=new JVector(0,0)
var p1=new JVector(100,0)
JCoord.distance(p1,p2)
//100
*/
// 坐标转换类
import fj.math.JDegree;
import fj.math.JVector;
class fj.math.JCoord {
//计算两点间距离
static function distance(p1:JVector, p2:JVector) {
var dx = p2.x-p1.x;
var dy = p2.y-p1.y;
return Math.sqrt(dx*dx+dy*dy);
}
//笛卡儿坐标转化为flash坐标
static function cartesianToFlash(Object) {
o._y *= -1;
o._rotation *= -1;
}
//flash坐标转化为笛卡儿坐标
static function flashToCartesian(Object) {
return cartesianToFlash(o);
}
//极坐标转化为笛卡儿坐标
static function cartesianToPolar(p:JVector) {
//p={x,y};
var radius = Math.sqrt(p.x*p.x+p.y*p.y);
var theta = JDegree.atan2D(p.y, p.x);
return {r:radius, t:theta};
}
//笛卡儿坐标转化为极坐标
static function polarToCartesian(p:Object) {
var x = p.r*JDegree.cosD(p.t);
var y = p.r*JDegree.sinD(p.t);
return {x:x, y:y};
}
}

标签集:TAGS:
回复Comments() 点击Count()

回复Comments

{commentauthor}
{commentauthor}
{commenttime}
{commentnum}
{commentcontent}
作者:
{commentrecontent}