Skip to content

Instantly share code, notes, and snippets.

@leegrey
Created June 22, 2011 08:43
Show Gist options
  • Save leegrey/1039712 to your computer and use it in GitHub Desktop.
Save leegrey/1039712 to your computer and use it in GitHub Desktop.
LGVector2D.as - A 2D Vector Class in AS3
//CLASS: com.lgrey.vectors.LGVector2D
//created by Lee Grey
package com.lgrey.vectors {
import flash.geom.Point;
public class LGVector2D {
public var x:Number = 0;
public var y:Number = 0;
public function LGVector2D(dx:Number = 0, dy:Number = 0){
x = dx;
y = dy;
}
public function initFromPoint( p:Point ):LGVector2D
{
x = p.x;
y = p.y;
return this;
}
public function reset():LGVector2D {
x = 0;
y = 0;
return this;
}
//
public function add(ov:*):LGVector2D {
x += ov.x;
y += ov.y;
return this;
}
//
public function subtract(ov:*):LGVector2D {
x -= ov.x;
y -= ov.y;
return this;
}
public function multiply(ov:*):LGVector2D {
x *= ov.x;
y *= ov.y;
return this;
}
//apply scalars
public function multiplyLength(o:*):LGVector2D {
x *= o;
y *= o;
return this;
}
public function divideLength(o:*):LGVector2D {
x /= o;
y /= o;
return this;
}
//give() gives the x,y values of this instance to another
public function give(ov:*):LGVector2D {
ov.x = x;
ov.y = y;
return this;
}
//copy() copies the x,y values of another instance to this
public function copy(ov:*):LGVector2D {
x = ov.x;
y = ov.y;
return this;
}
public function set angle(n:Number):void {
x = Math.cos(n)*length;
y = Math.sin(n)*length;
}
public function set angleDeg(n:Number):void {
n *= 0.0174532925;
x = Math.cos(n)*length;
y = Math.sin(n)*length;
}
public function setAngle(n:Number):LGVector2D {
x = Math.cos(n)*length;
y = Math.sin(n) * length;
return this;
}
public function setAngleDeg(n:Number):LGVector2D {
n *= 0.0174532925;
x = Math.cos(n)*length;
y = Math.sin(n) * length;
return this;
}
public function rotateBy(n:Number):LGVector2D {
var angle:Number = getAngle();
var length:Number = Math.sqrt(x*x+y*y);
x = Math.cos(n+angle)*length;
y = Math.sin(n + angle) * length;
return this;
}
public function rotateByDeg(n:Number):LGVector2D {
n *= 0.0174532925;
rotateBy(n);
return this;
}
public function normalise( n:Number = 1.0 ):LGVector2D {
normalize(n);
return this;
}
public function normalize( n:Number = 1.0 ):LGVector2D {
var length:Number = Math.sqrt(x*x+y*y);
x = (x/length) * n;
y = (y / length) * n;
return this;
}
public function get length():Number {
return (Math.sqrt(x*x+y*y));
}
public function getLength():Number {
return ( Math.sqrt(x*x+y*y) );
}
public function set length( newlength:Number ):void {
normalize(1);
x *= newlength;
y *= newlength;
}
public function setLength(newlength:Number):LGVector2D {
normalize(1);
x *= newlength;
y *= newlength;
return this;
}
//
public function getAngle():Number {
return (Math.atan2(y,x));
}
public function getAngleDeg():Number {
return (Math.atan2(y,x) * 57.2957 );
}
//
public function dot(ov:*):Number {
return (x*ov.x+y*ov.y);
}
public function clone():LGVector2D
{
return new LGVector2D(x, y)
}
public function zero():LGVector2D
{
x = 0;
y = 0;
return this;
}
public function lookAt( ov:* ):LGVector2D
{
var vectorToTarget:LGVector2D = new LGVector2D( ov.x - x, ov.y - y );
setAngle( vectorToTarget.getAngle() );
return this;
}
//operations returning new Vectors
public function minus(ov:*):LGVector2D {
return new LGVector2D( x -= ov.x, y -= ov.y );
}
//public function times(ov:*):void {
//return new LGVector2D( x * ov.x, y * ov.y );
//}
public function times(scalar:Number):LGVector2D {
return new LGVector2D( x * scalar, y * scalar );
}
public function plus(ov:*):LGVector2D {
return new LGVector2D( x -= ov.x, y -= ov.y );
}
} // end class
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment