home *** CD-ROM | disk | FTP | other *** search
/ 404 Jogos / CLJG.iso / Acao / fwg_knight.swf / scripts / __Packages / math / Vector.as
Encoding:
Text File  |  2008-08-28  |  4.8 KB  |  199 lines

  1. class math.Vector extends flash.geom.Point
  2. {
  3.    function Vector(x1, y1)
  4.    {
  5.       super();
  6.       this.x = x1;
  7.       this.y = y1;
  8.       if(isNaN(this.x))
  9.       {
  10.          this.x = 0;
  11.       }
  12.       if(isNaN(this.y))
  13.       {
  14.          this.y = 0;
  15.       }
  16.    }
  17.    function sum()
  18.    {
  19.       return this.x + this.y;
  20.    }
  21.    function setTo(px, py)
  22.    {
  23.       this.x = px;
  24.       this.y = py;
  25.    }
  26.    function copy(v)
  27.    {
  28.       this.x = v.x;
  29.       this.y = v.y;
  30.    }
  31.    function dot(v)
  32.    {
  33.       return this.x * v.x + this.y * v.y;
  34.    }
  35.    function cross(v)
  36.    {
  37.       return this.x * v.y - this.y * v.x;
  38.    }
  39.    function plus(v)
  40.    {
  41.       this.x += v.x;
  42.       this.y += v.y;
  43.    }
  44.    function minus(v)
  45.    {
  46.       this.x -= v.x;
  47.       this.y -= v.y;
  48.    }
  49.    function mult(s)
  50.    {
  51.       this.x *= s;
  52.       this.y *= s;
  53.    }
  54.    function plusNew(v)
  55.    {
  56.       return new math.Vector(this.x + v.x,this.y + v.y);
  57.    }
  58.    function minusNew(v)
  59.    {
  60.       return new math.Vector(this.x - v.x,this.y - v.y);
  61.    }
  62.    function multNew(s)
  63.    {
  64.       return new math.Vector(this.x * s,this.y * s);
  65.    }
  66.    static function equal(v1, v2)
  67.    {
  68.       return v1.x == v2.x && v1.y == v2.y;
  69.    }
  70.    function clone()
  71.    {
  72.       return new math.Vector(this.x,this.y);
  73.    }
  74.    function add(p)
  75.    {
  76.       return new math.Vector(this.x + p.x,this.y + p.y);
  77.    }
  78.    function subtract(p)
  79.    {
  80.       return new math.Vector(this.x - p.x,this.y - p.y);
  81.    }
  82.    function get len_2()
  83.    {
  84.       return this.x * this.x + this.y * this.y;
  85.    }
  86.    function simple(u)
  87.    {
  88.       this.x = Math.round(this.x / u) * u;
  89.       this.y = Math.round(this.y / u) * u;
  90.    }
  91.    function rotate(a)
  92.    {
  93.       var _loc2_ = Math.cos(a);
  94.       var _loc3_ = Math.sin(a);
  95.       var _loc5_ = this.x;
  96.       var _loc4_ = this.y;
  97.       this.x = _loc5_ * _loc2_ - _loc4_ * _loc3_;
  98.       this.y = _loc5_ * _loc3_ + _loc4_ * _loc2_;
  99.    }
  100.    function transform(m)
  101.    {
  102.       var _loc4_ = this.x;
  103.       var _loc3_ = this.y;
  104.       this.x = _loc4_ * m.a + _loc3_ * m.c + m.tx;
  105.       this.y = _loc4_ * m.b + _loc3_ * m.d + m.ty;
  106.    }
  107.    function isRight(p)
  108.    {
  109.       return math.Vector.crossProduct(this,p) < 0;
  110.    }
  111.    function isInside(pArr)
  112.    {
  113.       var _loc4_ = pArr.length;
  114.       var _loc2_ = 0;
  115.       while(_loc2_ < _loc4_)
  116.       {
  117.          var _loc3_ = pArr[_loc2_];
  118.          if(!this.subtract(_loc3_).isRight(pArr[_loc2_ != _loc4_ - 1 ? _loc2_ + 1 : 0].subtract(_loc3_)))
  119.          {
  120.             return false;
  121.          }
  122.          _loc2_ = _loc2_ + 1;
  123.       }
  124.       return true;
  125.    }
  126.    function getMp()
  127.    {
  128.       var _loc2_ = new math.Vector(this.y,- this.x);
  129.       _loc2_.normalize(1);
  130.       return _loc2_;
  131.    }
  132.    static function polar(len, angle)
  133.    {
  134.       var _loc1_ = flash.geom.Point.polar(len,angle);
  135.       return new math.Vector(_loc1_.x,_loc1_.y);
  136.    }
  137.    static function interpolate(pt1, pt2, f)
  138.    {
  139.       var _loc1_ = flash.geom.Point.interpolate(pt1,pt2,f);
  140.       return new math.Vector(_loc1_.x,_loc1_.y);
  141.    }
  142.    static function getK(p, m, n)
  143.    {
  144.       return math.Vector.crossProduct(p,n) / math.Vector.crossProduct(m,n);
  145.    }
  146.    static function getHorizontal(p, p0)
  147.    {
  148.       var _loc1_ = math.Vector.dotProduct(p,p0) / p0.len_2;
  149.       return new math.Vector(p0.x * _loc1_,p0.y * _loc1_);
  150.    }
  151.    static function getVertical(p, p0)
  152.    {
  153.       var _loc2_ = p0.len_2;
  154.       return new math.Vector(p0.y * math.Vector.crossProduct(p,p0) / _loc2_,p0.x * math.Vector.crossProduct(p0,p) / _loc2_);
  155.    }
  156.    static function dotProduct(p1, p2)
  157.    {
  158.       return p1.x * p2.x + p1.y * p2.y;
  159.    }
  160.    static function crossProduct(p1, p2)
  161.    {
  162.       return p1.x * p2.y - p2.x * p1.y;
  163.    }
  164.    static function intersects(p1, p2, p3, p4)
  165.    {
  166.       var _loc6_ = p3.subtract(p1);
  167.       var _loc5_ = p2.subtract(p3);
  168.       var _loc8_ = math.Vector.crossProduct(_loc6_,_loc5_);
  169.       var _loc3_ = p4.subtract(p2);
  170.       var _loc4_ = math.Vector.crossProduct(_loc5_,_loc3_);
  171.       if(_loc8_ * _loc4_ < 0)
  172.       {
  173.          return false;
  174.       }
  175.       var _loc1_ = p1.subtract(p4);
  176.       var _loc2_ = math.Vector.crossProduct(_loc3_,_loc1_);
  177.       if(_loc4_ * _loc2_ < 0)
  178.       {
  179.          return false;
  180.       }
  181.       var _loc7_ = math.Vector.crossProduct(_loc1_,_loc6_);
  182.       if(_loc2_ * _loc7_ < 0)
  183.       {
  184.          return false;
  185.       }
  186.       return true;
  187.    }
  188.    static function intersection(p1, p2, p3, p4)
  189.    {
  190.       var _loc2_ = math.Vector.crossProduct(p1,p3);
  191.       var _loc5_ = math.Vector.crossProduct(p2,p4);
  192.       var _loc7_ = math.Vector.crossProduct(p3,p2);
  193.       var _loc1_ = math.Vector.crossProduct(p4,p1);
  194.       var _loc3_ = math.Vector.crossProduct(p3,p4);
  195.       var _loc10_ = (_loc2_ + _loc3_ + _loc1_) / (_loc2_ + _loc5_ + _loc7_ + _loc1_);
  196.       return math.Vector.interpolate(p2,p1,_loc10_);
  197.    }
  198. }
  199.