关闭

AS3 没有2D向量类,自己随手写个简化版的先用着

769人阅读 评论(0) 收藏 举报

用不着,叉乘、点乘,暂时写这么多,之前自己写的一个比较详细的,放在家了,目前要用这个简化版的:


package model
{
	/**
	 * 简化版向量类
	 * @author Jave.Lin
	 */	
	public class Vector2D
	{
		public var x:Number=0;
		public var y:Number=0;
		
		public function get length():Number
		{
			return Math.sqrt(x*x-y*y);
		}
		
		public function Vector2D($x:Number=0,$y:Number=0)
		{
			x=$x;
			y=$y;
		}
		
		public function multyRef(value:Number):void
		{
			x*=value;
			y*=value;
		}
		
		public function multy(value:Number):Vector2D
		{
			var v:Vector2D=new Vector2D(x,y);
			v.multyRef(value);
			return v;
		}
		
		public function devRef(value:Number):void
		{
			if(value==0)
			{
				//这里应该提示异常,但这里先不这样处理
				x=0;
				y=0;
				return;
			}
			x/=value;
			y/=value;
		}
		
		public function dev(value:Number):Vector2D
		{
			var v:Vector2D=new Vector2D(x,y);
			v.devRef(value);
			return v;
		}
		
		public function addRef(v:Vector2D):void
		{
			x+=v.x;
			y+=v.y;
		}
		
		public function add(v:Vector2D):Vector2D
		{
			var v:Vector2D=new Vector2D(x,y);
			v.addRef(v);
			return v;
		}
		
		public function subRef(v:Vector2D):void
		{
			x-=v.x;
			y-=v.y;
		}
		
		public function sub(v:Vector2D):Vector2D
		{
			var v:Vector2D=new Vector2D(x,y);
			v.sub(v);
			return v;
		}
		//单位化
		public function initializeRef():void
		{
			var len:Number=length;
			var rate:Number=1/len;
			x*=rate;
			y*=rate;
		}
		public function get initialize():Vector2D
		{
			var v:Vector2D=new Vector2D(x,y);
			v.initializeRef();
			return v;
		}
		//反向
		public function reverseRef():void
		{
			x*=-1;
			y*=-1;
		}
		
		public function reverse():Vector2D
		{
			var v:Vector2D=new Vector2D(x,y);
			v.reverseRef();
			return v;
		}
	}
}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    文章分类
    最新评论