All pastes #960993 Raw Edit

Someone

public text v1 · immutable
#960993 ·published 2008-03-28 16:30 UTC
rendered paste body
/**
* ...
* @author Andreas Rønning
* @version 0.1
*/

package no.doomsday.aronning.math {
	import flash.display.Shape;

	public class Vector2 {
		public var x:Number;
		public var y:Number;
		/**
		 * Creates a new 2D vector
		 * @param	x
		 * @param	y
		 */
		function Vector2(x:Number = 0,y:Number = 0){
			this.x = x;
			this.y = y;
		}
		/**
		 * Set both components to 0
		 */
		public function zero():void{
			x = y = 0;
		}
		/**
		 * Reverses both components
		 */
		public function inverse():void { //legacy
			x = ~x + 1;
			x = ~y + 1;
		}
		/**
		 * Reverses both components
		 */
		public function reverse():void {
			x = ~x + 1;
			x = ~y + 1;
		}
		public function get magnitude():Number{
			return Math.sqrt(x*x+y*y);
		}
		/**
		 * Retrieve this vector's unit vector
		 * @return
		 * A new Vector2 instance
		 */
		public function getUnit():Vector2{
			return new Vector2(x/magnitude, y/magnitude);
		}
		/**
		 * Retrieve this vector's normal vector
		 * @return
		 * A new Vector2 instance
		 */
		public function getNormal():Vector2{
			return new Vector2(-y,x);
		}
		/**
		 * Truncates the vector to a maximum magnitude
		 * Positive values also count to truncate negative values
		 * @param	max
		 * The maximum magnitude of this vector
		 */
		public function truncate(max:Number):void {
			var mag:Number = Math.sqrt(x*x+y*y);
			if((mag ^ (mag >> 31)) - (mag >> 31) > (max ^ (max >> 31)) - (max >> 31)){
				var v:Vector2 = getUnit();
				x = v.x*max;
				y = v.y*max;
			}
		}
		/**
		 * Clone this vector
		 * @return
		 * A duplicate Vector2
		 */
		public function clone():Vector2{
			return new Vector2(x,y);
		}
		/**
		 * Multiply both vector components by a scalar
		 * @param	scalar
		 */
		public function scale(scalar:Number){
			x*=scalar;
			y*=scalar;
		}
		/**
		 * Get a string representation of this vector
		 * @return
		 * A string
		 */
		public function toString():String{
			return "Vector2 x:"+x+" y:"+y;
		}
		/**
		 * Get a shape representation of this vector
		 * @return
		 * A shape instance
		 */
		public function render():Shape{
			var shape:Shape = new Shape();
			shape.graphics.lineStyle(1,0);
			//direction
			shape.graphics.lineTo(x,y);
			//normal
			shape.graphics.moveTo(0,0);
			shape.graphics.lineStyle(1,0xFF0000);
			var n:Vector2 = getNormal();
			shape.graphics.lineTo(n.x,n.y);
			return shape;
		}
		private static function getComponent(vector:Vector2,directionVector:Vector2):Number{
			var alpha:Number = Math.atan2(directionVector.y,directionVector.x);
			var theta:Number = Math.atan2(vector.y,vector.x);
			var mag:Number = vector.magnitude;
			var a:Number = mag*Math.cos(theta-alpha);
			return a;
		}
		/**
		 * Get a component vector from a source vector and a direction
		 * @param	vector
		 * The source Vector2
		 * @param	directionVector
		 * A Vector2 representing a direction
		 * @return
		 */
		public static function getComponentVector(vector:Vector2,directionVector:Vector2):Vector2{
			var v:Vector2 = directionVector.getUnit();
			var component:Number = getComponent(vector,directionVector);
			var vec:Vector2 = new Vector2();
			vec.x = v.x*component;
			vec.y = v.y*component;
			return vec;
		}
		/**
		 * Subtract one Vector2 from another
		 * @param	v1
		 * A Vector2 instance
		 * @param	v2
		 * A Vector2 instance
		 * @return
		 * A new Vector2 instance
		 */
		public static function subtract(v1:Vector2,v2:Vector2):Vector2{
			//v-u = -u+v
			var v:Vector2 = v1.clone();
			var u:Vector2 = v2.clone();
			u.inverse();
			return new Vector2(u.x + v.x, u.y + v.y);
		}
		/**
		 * Add two Vector2 instances together
		 * @param	v1
		 * A Vector2 instance
		 * @param	v2
		 * A Vector2 instance
		 * @return
		 * A new Vector2 instance
		 */
		public static function add(v1:Vector2,v2:Vector2):Vector2{
			return new Vector2(v1.x+v2.x,v1.y+v2.y);
		}
		/**
		 * Get the dot product of two vectors
		 * @param	v1
		 * A Vector2 instance
		 * @param	v2
		 * A Vector2 instance
		 * @return
		 * The dot product as a number
		 */
		public static function dot(v1:Vector2,v2:Vector2):Number{
			var n1:Vector2 = v1.getUnit();
			var n2:Vector2 = v2.getUnit();
			return n1.x*n2.x+n1.y*n2.y;
		}
		

	}
}