home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
404 Jogos
/
CLJG.iso
/
Aventura
/
aether.swf
/
scripts
/
Vector.as
< prev
next >
Wrap
Text File
|
2008-09-11
|
6KB
|
194 lines
package
{
import flash.display.MovieClip;
public class Vector
{
private static var convert:Number = Math.PI / 180;
public var x:Number;
public var y:Number;
public function Vector(param1:Number, param2:Number)
{
super();
x = param1;
y = param2;
}
public static function add(param1:Vector, param2:Vector) : Vector
{
return new Vector(param2.x + param1.x,param2.y + param1.y);
}
public static function subtract(param1:Vector, param2:Vector) : Vector
{
return new Vector(param1.x - param2.x,param1.y - param2.y);
}
public static function dot(param1:Vector, param2:Vector) : Number
{
return param2.x * param1.x + param2.y * param1.y;
}
public static function angle(param1:Vector, param2:Vector) : Number
{
if(param2.length * param1.length == 0)
{
return 0;
}
return Math.acos(dot(param1,param2) / (param2.length * param1.length)) / convert;
}
public static function comp(param1:Vector, param2:Vector) : Number
{
if(param1.length * param2.length == 0)
{
return 0;
}
return Math.abs(param1.x / param1.length - param2.x / param2.length) + Math.abs(param1.y / param1.length - param2.y / param2.length);
}
public static function angle2(param1:Vector, param2:Vector) : Number
{
var _loc3_:Number = NaN;
_loc3_ = angle(param1,param2);
if(_loc3_ == 0)
{
return 0;
}
if(comp(param1.rotateNew(_loc3_),param2) < 0.0001)
{
return _loc3_;
}
return -_loc3_;
}
public function rotate(param1:Number) : void
{
var _loc2_:Number = NaN;
var _loc3_:Number = NaN;
var _loc4_:Number = NaN;
var _loc5_:Number = NaN;
param1 *= convert;
_loc2_ = Math.sin(param1);
_loc3_ = Math.cos(param1);
_loc4_ = x * _loc3_ - y * _loc2_;
_loc5_ = x * _loc2_ + y * _loc3_;
x = _loc4_;
y = _loc5_;
}
public function rotateNew(param1:Number) : Vector
{
var _loc2_:Number = NaN;
var _loc3_:Number = NaN;
param1 *= convert;
_loc2_ = Math.sin(param1);
_loc3_ = Math.cos(param1);
return new Vector(x * _loc3_ - y * _loc2_,x * _loc2_ + y * _loc3_);
}
public function get length() : Number
{
return Math.sqrt(x * x + y * y);
}
public function perpendicular(param1:Number = -1) : Vector
{
return new Vector(param1 * y,-param1 * x);
}
public function set length(param1:Number) : void
{
norm();
scale(param1);
}
public function scale(param1:Number) : void
{
x *= param1;
y *= param1;
}
public function render(param1:Vector, param2:Number, param3:MovieClip) : void
{
var _loc4_:Vector = null;
var _loc5_:Vector = null;
param3.graphics.lineStyle(3,16777215,100);
param3.graphics.moveTo(param1.x,param1.y);
param3.graphics.lineTo(param1.x + x * param2,param1.y + y * param2);
(_loc4_ = new Vector(-x,-y)).norm();
_loc4_.scale(7);
_loc4_.rotate(-45);
param3.graphics.lineTo(param1.x + x * param2 + _loc4_.x,param1.y + y * param2 + _loc4_.y);
param3.graphics.moveTo(param1.x + x * param2,param1.y + y * param2);
_loc4_.rotate(90);
param3.graphics.lineTo(param1.x + x * param2 + _loc4_.x,param1.y + y * param2 + _loc4_.y);
param3.graphics.lineStyle(2,0,100);
param3.graphics.moveTo(param1.x,param1.y);
param3.graphics.lineTo(param1.x + x * param2,param1.y + y * param2);
(_loc5_ = new Vector(-x,-y)).norm();
_loc5_.scale(7);
_loc5_.rotate(-45);
param3.graphics.lineTo(param1.x + x * param2 + _loc5_.x,param1.y + y * param2 + _loc5_.y);
param3.graphics.moveTo(param1.x + x * param2,param1.y + y * param2);
_loc5_.rotate(90);
param3.graphics.lineTo(param1.x + x * param2 + _loc5_.x,param1.y + y * param2 + _loc5_.y);
}
public function constrain(param1:Number, param2:String = "Greater") : *
{
if(param2 == "Greater")
{
if(length > param1)
{
norm();
scale(param1);
}
}
if(param2 == "Always")
{
norm();
scale(param1);
}
if(param2 == "Less")
{
if(length < param1)
{
norm();
scale(param1);
}
}
}
public function get length2() : Number
{
return x * x + y * y;
}
public function norm() : void
{
var _loc1_:Number = NaN;
_loc1_ = length;
if(_loc1_ != 0)
{
x /= _loc1_;
y /= _loc1_;
}
}
public function reflect(param1:Vector) : Vector
{
var _loc2_:Vector = null;
_loc2_ = new Vector(param1.x,param1.y);
_loc2_.norm();
_loc2_.scale(2 * dot(this,_loc2_));
return subtract(this,_loc2_);
}
}
}