CVector2.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. function CVector2(iX,iY){
  2. var x;
  3. var y;
  4. this._init = function(iX,iY){
  5. x = iX;
  6. y = iY;
  7. };
  8. this.add = function( vx, vy ){
  9. x += vx;
  10. y += vy;
  11. };
  12. this.addV = function( v ){
  13. x += v.getX();
  14. y += v.getY();
  15. };
  16. this.scalarDivision = function( n ) {
  17. x /= n;
  18. y /= n;
  19. };
  20. this.subV = function( v ){
  21. x -= v.getX();
  22. y -= v.getY();
  23. };
  24. this.scalarProduct = function( n ){
  25. x*=n;
  26. y*=n;
  27. };
  28. this.invert = function(){
  29. x*=-1;
  30. y*=-1;
  31. };
  32. this.dotProduct = function( v){
  33. return ( x*v.getX()+ y*v.getY() );
  34. };
  35. this.set = function( fx, fy ){
  36. x = fx;
  37. y = fy;
  38. };
  39. this.setV = function( v ){
  40. x = v.getX();
  41. y = v.getY();
  42. };
  43. this.length = function(){
  44. return Math.sqrt( x*x+y*y );
  45. };
  46. this.length2 = function(){
  47. return x*x+y*y;
  48. };
  49. this.normalize = function(){
  50. var len = this.length();
  51. if (len > 0 ){
  52. x/= len; y/=len;
  53. }
  54. };
  55. this.getNormalize = function( outV ) {
  56. var len = this.length();
  57. outV.set(x,y);
  58. outV.normalize();
  59. };
  60. this.rot90CCW = function(){
  61. var a = x;
  62. x = -y;
  63. y = a;
  64. };
  65. this.rot90CW = function(){
  66. var a = x;
  67. x = y;
  68. y = -a;
  69. };
  70. this.getRotCCW = function( outV ) {
  71. outV.set( x, y );
  72. outV.rot90CCW();
  73. };
  74. this.getRotCW = function( outV ) {
  75. outV.set( x, y );
  76. outV.rot90CW();
  77. };
  78. this.ceil = function(){
  79. x = Math.ceil( x );
  80. y = Math.ceil( y );
  81. };
  82. this.round = function(){
  83. x = Math.round( x );
  84. y = Math.round( y );
  85. };
  86. this.toString = function(){
  87. return "Vector2: " + x + ", " + y;
  88. };
  89. this.print = function(){
  90. trace( "Vector2: " + x + ", " + y + "" );
  91. };
  92. this.getX = function(){
  93. return x;
  94. };
  95. this.getY = function(){
  96. return y;
  97. };
  98. this._init(iX,iY);
  99. }