home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 July / PCpro_2004_07.ISO / docs / maxblast / maxblast3_files / thing_004.js < prev    next >
Encoding:
JavaScript  |  2004-03-07  |  2.0 KB  |  71 lines

  1. /********************************************************************************
  2.  
  3.     Point Objects
  4.  
  5. ********************************************************************************/
  6. function Point( /* x,y or p */ ){
  7.     if( Point.isPoint(arguments[0]) ){
  8.         this.x = arguments[0].x;
  9.         this.y = arguments[0].y;
  10.     }
  11.     else{
  12.         this.x = arguments[0];
  13.         this.y = arguments[1];
  14.     }
  15. }
  16. Point.isPoint = function(what){
  17.     return ( what.constructor == Point );
  18. }
  19. Point.prototype.getArgs = function( /* [[x,y]] or [[p]] */ ){
  20.     return new Point( arguments[0][0], arguments[0][1] );
  21. }
  22.  
  23. Point.prototype.add = function( /* x,y or p */ ){
  24.     var that = this.getArgs( arguments );
  25.     return new Point( this.x + that.x, this.y + that.y );
  26. }
  27. Point.prototype.sub = function( /* x,y or p */ ){
  28.     var that = this.getArgs( arguments );
  29.     return new Point( this.x - that.x, this.y - that.y );
  30. }
  31. Point.prototype.mult = function( mult ){
  32.     return new Point( mult * this.x, mult * this.y);
  33. }
  34. Point.prototype.perp = function(){
  35.     return new Point( - this.y, this.x );
  36. }
  37. Point.prototype.distTo = function( /* x,y or p */ ){
  38.     var that = this.getArgs( arguments );
  39.     var dx = this.x - that.x;
  40.     var dy = this.y - that.y;
  41.     if( dx == 0 ) return Math.abs(dy);
  42.     if( dy == 0 ) return Math.abs(dx);
  43.     return Math.sqrt( dx*dx + dy*dy );
  44. }
  45. Point.prototype.size = function(){
  46.     var p = new Point(0,0);
  47.     return this.distTo( p );
  48. }
  49. Point.prototype.toString = function(){
  50.     return '(' + this.x + ', ' + this.y + ')';
  51. }
  52. Point.prototype.isInRect = function( t, r, b, l ){
  53.     return ( this.y > t && this.x < r && this.y < b && this.x > l );
  54. }
  55.  
  56. /********************************************************************************
  57.  
  58.     Rect Class
  59.  
  60. ********************************************************************************/
  61. function Rect( t, r, b, l ){
  62.     this.t = t; 
  63.     this.r = r;
  64.     this.b = b;
  65.     this.l = l;
  66. }
  67.  
  68. Rect.prototype.toString = function(){
  69.     return "(" + this.t + ", " + this.r + ", " + this.b + ", " + this.l + ")";
  70. }
  71.