AS2 – Vector Class

Today I check my rss feeder and I've seen that michael has posted a Vector class here. Which reminds me that I have done one as well. So I'm posting it here.

Hope you'll like it :)

Actionscript:
  1. /**
  2. * @class net.webbymx.geom.Vector
  3. * @author Xavier MARTIN
  4. * @version 0.2
  5. * @description Vector Class to hold most of all the operation you can do with
  6. * @usage myVector:Vector = new Vector(x, y);
  7. * x and y are optional (default 0)
  8. **/
  9.  
  10. /*
  11. * Class written by Xavier MARTIN (xxlm or zeflasher)
  12. * http://dev.webbymx.net
  13. * http://www.webbymx.net
  14. * If you are using this class, I will be glad to receive a postcard of your place :)
  15. * To do so please visit http://dev.webbymx.net and go in the about page to get my details...
  16. */
  17.  
  18. import flash.geom.Point
  19. import flash.geom.Matrix;
  20.  
  21. class net.webbymx.geom.Vector {
  22.  
  23. /* ****************************************************************************
  24. * STATIC FUNCTION
  25. **************************************************************************** */
  26. /**
  27. * Returns a normalized vector of the one passed in the arguments
  28. * @param    v
  29. * @return
  30. */
  31.     public static function Normalize( v : Vector ) : Vector {
  32.         var vr : Vector = new Vector();
  33.         var l : Number = v.length();
  34.         if ( l != 0 ) {
  35.             vr.vx = v.vx / l;
  36.             vr.vy = v.vy / l;
  37.         }
  38.         return vr;
  39.     }
  40.  
  41.    
  42. /**
  43. * Returns the distance between two vectors passed in arguments
  44. * @param    v1
  45. * @param    v2
  46. * @return
  47. */
  48.     public static function Distance ( v1 : Vector, v2 : Vector ) : Number {
  49.         var ySeparation:Number = v2.vy - v1.vy;
  50.         var xSeparation:Number = v2.vx - v1._vx;
  51.         return ( Math.sqrt( ( ySeparation * ySeparation + xSeparation * xSeparation ), 2 ) );
  52.     }
  53.  
  54.    
  55. /**
  56. * Return the sqaured distance between two vector passed in arguments
  57. * @param    v1
  58. * @param    v2
  59. * @return
  60. */
  61.     public static function DistanceSq ( v1 : Vector, v2 : Vector ) : Number {
  62.         var ySeparation:Number = v2.vy - v1.vy;
  63.         var xSeparation:Number = v2.vx - v1.vx;
  64.         return ( ySeparation * ySeparation + xSeparation * xSeparation );
  65.     }
  66.  
  67.    
  68. /**
  69. * Returns the length of a vector passed in argument
  70. * @param    v
  71. * @return
  72. */
  73.     public static function Length ( v : Vector ) : Number {
  74.         return ( Math.sqrt( ( v.vx * v.vx + v.vy * v.vy ), 2 ) );
  75.     }
  76.  
  77.    
  78. /**
  79. * Convert a Point passed in argument to a Vector and returns the new vector
  80. * @param    p
  81. * @return
  82. */
  83.     public static function PointToVector( p : Point ) : Vector {
  84.         var v:Vector = new Vector( p.x, p.y );
  85.         return v;
  86.     }
  87.  
  88.    
  89. /**
  90. * Convert a Vector passed in argument to a Point and returns the new Point
  91. * @param    v
  92. * @return
  93. */
  94.     public static function VectorToPoint( v : Vector ) : Point {
  95.         var p:Point = new Point( v.vx, v.vy );
  96.         return p;
  97.     }
  98.  
  99.    
  100. /**
  101. * Rotate a Vector passed in argument around it's origin and returns the new Vector
  102. * @param    v
  103. * @param    ang
  104. * @return
  105. */
  106.     public static function RotateAroundOrigin( v : Vector, ang : Number ) : Vector {
  107.     //  create a transformation matrix
  108.         var mtx:Matrix = new Matrix();
  109.  
  110.     //  rotate
  111.         mtx.rotate(ang);
  112.  
  113.     //  now transform the object's vertices
  114.         var oldPt:Point = Vector.VectorToPoint(v);
  115.         var newPt:Point = mtx.transformPoint(oldPt);
  116.         v = Vector.PointToVector(newPt);
  117.  
  118.         return v;
  119.     }
  120.  
  121.    
  122. /**
  123. * Returns the intersection point
  124. * @param    v1
  125. * @param    v2
  126. * @param    v3
  127. * @param    v4
  128. * @return
  129. */
  130.     static public function GetIntersectionPoint(v1:Vector, v2:Vector, v3:Vector, v4:Vector ) : Point {
  131.         var factor:Number = ((v4.vy-v3.vy)*(v2.vx-v1.vx)) - ((v4.vx-v3.vx)*(v2.vy-v1.vy));
  132.         var ua:Number = ( ((v4.vx-v3.vx)*(v1.vy-v3.vy)) - ((v4.vy-v3.vy)*(v1.vx-v3.vx)) ) / factor;
  133.         var ub:Number = ( ((v2.vx-v1.vx)*(v1.vy-v3.vy)) - ((v2.vy-v1.vy)*(v1.vx-v3.vx)) ) / factor;
  134.         var ox:Number = v1.vx + (ua*(v2.vx-v1.vx));
  135.         var oy:Number = v1.vy + (ua*(v2.vy-v1.vy));
  136.         return new Point( ox, oy );
  137.     }
  138.    
  139. /* ****************************************************************************
  140. * VARIABLES
  141. **************************************************************************** */
  142. /**
  143. * @property (Number) vx vector ion X axis
  144. * @property (Number) vy vector ion Y axis
  145. **/
  146.     private var _vx:Number;
  147.     private var _vy:Number;
  148.  
  149.  
  150. /* ****************************************************************************
  151. * CONSTRUCTOR
  152. **************************************************************************** */
  153.     public function Vector(x, y) {
  154.     //  init the vector to 0
  155.         if (x==0 && y==0 || (x==undefined || y==undefined)) this.zero();
  156.         else {
  157.             _vx = x;
  158.             _vy = y;
  159.         }
  160.  
  161.     }
  162.  
  163.  
  164. /* ****************************************************************************
  165. * PUBLIC FUNCTION
  166. **************************************************************************** */
  167. /**
  168. * Returns the new vector after addition
  169. * @param    v
  170. * @return
  171. */
  172.     public function add ( v : Vector ) : Vector {
  173.         var nv : Vector = new Vector( _vx + v.vx, _vy + v.vy );
  174.         return ( nv );
  175.     }
  176.  
  177. /**
  178. * Returns the angl in degree or radian between the two vector
  179. * @param    v
  180. * @return
  181. */
  182.     public function angl ( v : Vector ) : Number {
  183.         var cosA:Number;
  184.        
  185.         //if ( this.isZero() && v.isZero() ) return NaN;
  186.         if ( this.isZero() && !v.isZero() ) cosA = v.vx;
  187.         else if ( !this.isZero() && v.isZero() ) cosA = this.vx;
  188.         else cosA = this.dot( v ) / ( this.length() * v.length() );
  189.        
  190.         var A : Number = Math.acos( cosA );
  191.     //  if we want it in degree
  192.         if ( arguments[1] == true ) {
  193.             var degrees = A / Math.PI * 180;
  194.             return degrees;
  195.         }
  196.  
  197.         return A;
  198.     }
  199.  
  200.  
  201. /**
  202. * Returns the new vector after substraction
  203. * @param    v
  204. * @return
  205. */
  206.     public function sub ( v : Vector ) : Vector {
  207.         var nv : Vector = new Vector( _vx - v.vx, _vy - v.vy );
  208.         return ( nv );
  209.     }
  210.  
  211.  
  212. /**
  213. * Returns the new vector after addition
  214. * @param    n
  215. * @return
  216. */
  217.     public function mult ( n : Number ) : Vector {
  218.         var nv : Vector = new Vector( _vx * n, _vy * n );
  219.         return ( nv );
  220.     }
  221.  
  222.  
  223. /**
  224. * Returns the new vector after devision
  225. * @param    n
  226. * @return
  227. */
  228.     public function div ( n : Number ) : Vector {
  229.         var nv : Vector = new Vector( _vx / n, _vy / n );
  230.         return ( nv );
  231.     }
  232.  
  233.  
  234. /**
  235. * Returns the length of the vector
  236. * @param    Void
  237. * @return
  238. */
  239.     public function length ( Void ) : Number {
  240.         return ( Math.sqrt( ( _vx * _vx + _vy * _vy ), 2 ) );
  241.     }
  242.  
  243.  
  244. /**
  245. * Returns the length of the vector
  246. * @param    Void
  247. * @return
  248. */
  249.     public function lengthSq ( Void ) : Number {
  250.         return ( _vx * _vx + _vy * _vy );
  251.     }
  252.  
  253. /**
  254. * Calculate the dot product
  255. * @param    v
  256. * @return
  257. */
  258.     public function dot ( v : Vector ) : Number {
  259.         return ( _vx * v.vx + _vy * v.vy );
  260.     }
  261.  
  262. /**
  263. * Returns positive if v2 is clockwise of this vector,
  264. * minus if anticlockwise (Y axis pointing down, X axis to right)
  265. * @param    v
  266. * @return
  267. */
  268.     public function sign ( v : Vector ) : Number {
  269.         if( _vy * v.vx> _vx * v.vy ) return -1;
  270.         else return 1;
  271.     }
  272.  
  273.  
  274. /**
  275. * Returns a vector perpendicular to this vector
  276. * @param    Void
  277. * @return
  278. */
  279.     public function perp ( Void ) : Vector {
  280.         var nv : Vector = new Vector( -_vy, _vx );
  281.         return nv;
  282.     }
  283.  
  284.  
  285. /**
  286. * Returns the distance between this vector and the one passed in params
  287. * @param    v
  288. * @return
  289. */
  290.     public function distance ( v : Vector ) : Number {
  291.         var ySeparation:Number = v.vy - _vy;
  292.         var xSeparation:Number = v.vx - _vx;
  293.         return ( Math.sqrt( ( ySeparation * ySeparation + xSeparation * xSeparation ), 2 ) );
  294.     }
  295.  
  296.  
  297. /**
  298. * Returns the squared distance between this vector and the one passed in params
  299. * @param    v
  300. * @return
  301. */
  302.     public function distanceSq ( v : Vector ) : Number {
  303.         var ySeparation:Number = v.vy - _vy;
  304.         var xSeparation:Number = v.vx - _vx;
  305.         return ( ySeparation * ySeparation + xSeparation * xSeparation );
  306.     }
  307.  
  308.    
  309. /**
  310. * Truncates a vector so that its length does not exceed max
  311. * @param    m
  312. */
  313.     public function truncate ( m : Number ) : Void {
  314.         if ( this.length()> m ) {
  315.             this.normalize();
  316.             _vx *= m;
  317.             _vy *= m;
  318.         }
  319.     }
  320.  
  321.  
  322. /**
  323. * given a normalized vector this method reflects the vector it is operating upon.
  324. * (like the path of a ball bouncing off a wall)
  325. * @param    vn
  326. * @return
  327. */
  328.     public function reflect ( v : Vector) : Vector {
  329.         var vn : Vector = new Vector();
  330.         vn.vy = _vy + 2.0 * dot( v ) * v.getReverse().vy;
  331.         vn.vx = _vx + 2.0 * dot( v ) * v.getReverse().vx;
  332.         return vn;
  333.     }
  334.  
  335.  
  336. /**
  337. * Return the opposite Vector;
  338. * @param    Void
  339. * @return
  340. */
  341.     public function getReverse ( Void ) : Vector {
  342.         var vn : Vector = new Vector( -_vx, -_vy );
  343.         return vn;
  344.     }
  345.  
  346.    
  347. /**
  348. * Normalizes a 2D Vector
  349. * @param    Void
  350. */
  351.     public function normalize ( Void ) : Void {
  352.         var l : Number = this.length();
  353.         if ( l != 0 ) {
  354.             _vx /= l;
  355.             _vy /= l;
  356.         }
  357.     }
  358.  
  359.  
  360. /**
  361. * Init the vector to (0,0)
  362. * @param    Void
  363. */
  364.     public function zero ( Void ) : Void {
  365.         _vx = _vy = 0;
  366.     }
  367.  
  368.  
  369. /**
  370. * Check if the vector is null
  371. * @param    Void
  372. * @return
  373. */
  374.     public function isZero ( Void ) : Boolean {
  375.         if ( _vx == 0 && _vy == 0 ) return true;
  376.         else return false;
  377.     }
  378.  
  379.  
  380. /* ****************************************************************************
  381. * GET && SET
  382. **************************************************************************** */
  383.     public function get vx():Number {
  384.         return _vx;
  385.     }
  386.     public function get vy():Number {
  387.         return _vy;
  388.     }
  389.     public function set vy(n:Number) {
  390.         _vy = n;
  391.     }
  392.     public function set vx(n:Number) {
  393.         _vx = n;
  394.     }
  395. }

Vector Class Download the class Unknown

Related posts:

  1. MDM Zinc – Class FileSystemManager
  2. MDM Zinc – Class DateManager for ftp and filesystem

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)

Tags: , ,

View Comments

  1. Hey Man, yeah I like this...

    Mine is very unfinished. The only thing I'd say is that it seems a little convoluted. Would you mind if I took yours and adapted it to what I need, and then sent you the source?

    For no other reason then keeping it simple.

    The AI I'm working on is for a game (which I can't speak of at the moment) the class resulted from some Flocking scripts I was writing for pre-production examples.

    Take care.
    Michael

  2. Yes of course, you could dld it and share what you have done then...
    Otherwise I have done an engine (not yet fully finish but working for steering behavior AI) in AS2. Maybe you wold like to see some of the code ...

  3. Hi Xavier

    Your vector class has proved very useful for me in some little widgets & games I have worked on. It has helped me to understand vector maths better. I discovered it last year when I was still working with AS2 and have I've now ported it to AS3 -- no problem as it was nice, clean OO code to start with!

    I have one question though. At the moment I'm not using your native getIntersectionPoint() function because I don't understand why it would take 4 vectors as parameters? Surely one would use just two vectors to calculate an intersection point?

    It would be very useful if you could offer some advice on this!

    Many Thanks!

    -Nick

Leave a comment

blog comments powered by Disqus