AS3 Vector2D 详细类

转载 2012年03月26日 10:38:46
package  
{	
	/**
	 * Vector2D
	 * Purpose: Utility class to assist in Vector manipulation in a 2D environment
	 * @author Shiu
	 * @version 1.0	9 August 2011
	 * @version 2.0	22 August 2011
	 */
	public class Vector2D 
	{
		private var _vecX:Number;
		private var _vecY:Number;
		
		/**
		 * Constructor of Vector2D
		 * @param	mc_x	x-component of vector
		 * @param	mc_y	y-component of vector
		 */
		public function Vector2D(mc_x:Number, mc_y:Number) 
		{
			this._vecX = mc_x;
			this._vecY = mc_y;
		}
		
		/**
		 * Provision for an alternate constructor
		 * @param	mag		Magnitude of vector
		 * @param	ang_rad Orientation of vector in radians
		 */
		public function polar (mag:Number, ang_rad:Number):void
		{
			this._vecX = mag*Math.cos(ang_rad);
			this._vecY = mag*Math.sin(ang_rad);
		}
		
		/**
		 * Accessors of individual x-component
		 */
		public function set x(new_x:Number):void
		{
			this._vecX = new_x;
		}
		
		public function get x ():Number
		{
			return this._vecX;
		}
		
		/**
		 * Accessors of individual y-component
		 */
		public function set y(new_y:Number):void
		{
			this._vecY = new_y;
		}
		
		public function get y ():Number
		{
			return _vecY;
		}
		
		/**
		 * Method to obtain the vector
		 * @return A vector array of x and y components
		 */
		public function getVector ():Vector.<Number>
		{
			return new <Number>[_vecX, _vecY]
		}
		
		/**
		 * Method to obtain current angle of vector
		 * @return Angle in radians
		 */
		public function getAngle ():Number 
		{
			return Math.atan2(_vecY, _vecX);
		}
		
		/**
		 * Method tl alter current angle of vector
		 * @param	ang_rad New angle for current vector in radians
		 */
		public function setAngle(ang_rad:Number):void
		{
			var mag_current:Number = this.getMagnitude();
			this.polar(mag_current, ang_rad);
		}
		
		/**
		 * Method to obtain current magnitude of vector
		 * @return Magnitude of type Number
		 */
		public function getMagnitude():Number
		{
			return Math.sqrt(_vecX * _vecX + _vecY * _vecY);
		}
		
		/**
		 * Method ot alter current magnitude of vector
		 * @param	magnitude New magnitude of vector
		 */
		public function setMagnitude(magnitude:Number):void
		{
			var ang_current:Number = this.getAngle();
			this.polar(magnitude, ang_current);
		}
		
		/**
		 * Method to invert both x and y components of vector
		 */
		public function invert():void
		{
			_vecX *= -1;
			_vecY *= -1;
		}
		
		/**
		 * Version 2.0
		 * Method to invert only the x-component of Vector2D
		 */
		public function invertX():void
		{
			_vecX *= -1;
		}
		
		/**
		 * Version 2.0
		 * Method to invert only the x-component of Vector2D
		 */
		public function invertY():void
		{
			_vecY *= -1;
		}
		
		/**
		 * Version 2.0
		 * Method to scale current vector by a scalar magnitude
		 * @param	multiplier A scalar number to mulitply current vector
		 */
		public function scale (magnitude:Number):void
		{
			_vecX *= magnitude;
			_vecY *= magnitude;
		}
		
		/**
		 * Method to generate a copy of current vector
		 * @return A copy of current vector
		 */
		public function clone ():Vector2D
		{
			var clone1:Vector2D = new Vector2D(_vecX, _vecY);
			return clone1;
		}
		
		/**
		 * Method to perform subtration on current vector
		 * @param	toMinus A vector to minus current vector
		 * @return A copy of subtration product
		 */
		public function minus(toMinus:Vector2D):Vector2D
		{
			var newX:Number = this._vecX - toMinus.x;
			var newY:Number = this._vecY - toMinus.y;
			return new Vector2D(newX, newY);
		}
		
		/**
		 * Method to perform addition on current vector
		 * @param	toAdd A vector to add onto current vector
		 * @return A copy of addition product
		 */
		public function add(toAdd:Vector2D):Vector2D
		{
			var newX:Number = this._vecX + toAdd.x;
			var newY:Number = this._vecY + toAdd.y;
			return new Vector2D(newX, newY);
		}
		
		/**
		 * Version 2.0
		 * Method to multiply current vector with a certain magnitude
		 * @param	multiplier
		 * @return A copy of the multiplied product
		 */
		public function multiply(multiplier:Number):Vector2D
		{
			var newX:Number = this._vecX * multiplier;
			var newY:Number = this._vecY * multiplier;
			return new Vector2D(newX, newY);
		}
		
		/**
		 * Method to rotate current vector
		 * @param	angle_rad Angle in radian to rotate current vector
		 * @param	offset A vector to offset current circular into an eliptical course
		 * @return A copy of the rotated vector
		 */
		public function rotate(angle_rad:Number):Vector2D
		{
			var newX:Number = _vecX * Math.cos(angle_rad) - _vecY * Math.sin(angle_rad);
			var newY:Number = _vecX * Math.sin(angle_rad) + _vecY * Math.cos(angle_rad);
			
			return new Vector2D(newX, newY);
		}
		
		/**
		 * Method to obtain vector unit of current vector
		 * @return A copy of normalised vector
		 */
		public function normalise():Vector2D
		{
			return new Vector2D(this._vecX/this.getMagnitude(), this._vecY/this.getMagnitude())
		}
		
		/**
		 * Method to perform dot product with another vector
		 * @param	vector2 A vector to perform dot product with current vector
		 * @return A scalar number of dot product
		 */
		public function dotProduct(vector2:Vector2D):Number
		{
			var componentX:Number = this._vecX * vector2.x;
			var componentY:Number = this._vecY * vector2.y;
			return componentX+componentY;
		}
		
		/**
		 * Method to perform vector product of current vector with input vector
		 * @param	vector2 A vector to perform vector product with current vector
		 * @return The magnitude of pseudo-vector on z-axis
		 */
		public function vectorProduct(vector2:Vector2D):Number 
		{
			return this._vecX * vector2.y - this._vecY * vector2.x;
		}
		
		/**
		 * Method to obtain the smaller angle, in radian, sandwiched from current vector to input vector
		 * @param	vector2 A vector to bound the angle
		 * @return Angle in radian, positive is clockwise, negative is anti-clockwise
		 */
		public function angleBetween(vector2:Vector2D):Number
		{	
			//get normalised vectors
			var norm1:Vector2D = this.normalise();
			var norm2:Vector2D = vector2.normalise();
			
			//dot product of vectors to find angle
			var product:Number = norm1.dotProduct(norm2);
			product = Math.min(1, product);
			var angle:Number = Math.acos(product);
			
			//sides of angle
			if (this.vectorProduct(vector2) < 0) angle *= -1
			return angle;
		}
		
		/**
		 * Method to obtain the projection of current vector on a given axis
		 * @param	axis An axis where vector is projected on
		 * @return The projection length of current vector on given axis
		 */
		public function projectionOn(axis:Vector2D):Number
		{
			return this.dotProduct(axis.normalise())
		}
	}

}


相关文章推荐

vector2D向量实例

/** 在x-y平面上给定四个顶点ABCD,确定它是否是一个矩形 */ #include class vector2D { public: vector2D(){} vector2D(...
  • a197p
  • a197p
  • 2015年06月30日 21:42
  • 1174

LeetCode 251. Flatten 2D Vector(摊平二维向量)

原题网址:https://leetcode.com/problems/flatten-2d-vector/ Implement an iterator to flatten a 2d vector...
  • jmspan
  • jmspan
  • 2016年04月07日 14:13
  • 631

vector<Point2d> 详解

vector 详解

Vector2D.as

  • 2016年05月03日 16:54
  • 7KB
  • 下载

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

用不着,叉乘、点乘,暂时写这么多,之前自己写的一个比较详细的,放在家了,目前要用这个简化版的: package model { /** * 简化版向量类 * @author Jav...

Vector3,cocos2d-x版

  • 2015年07月09日 17:54
  • 6KB
  • 下载

Vector3——简单的3D向量类

  • 2015年05月08日 11:07
  • 3KB
  • 下载

unity3d问题集 <2> 对Vector3.Lerp 插值的理解

unity3d问题集 对Vector3.Lerp 插值的理解  有时,我们在做游戏时会发现有些跟随动作不够圆滑或者需要一个缓冲的效果,这时,一般会考虑到插值。所以对插值的理解是必需的。(比如摄...

JAVA与AS2制作一个2D多人游戏(3)

要进行信息交互,首先要建立一对互相连接的Socket 客户端代码如下 public function Client() { Socket = new XMLSocket(); Sock...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:AS3 Vector2D 详细类
举报原因:
原因补充:

(最多只允许输入30个字)