Home | History | Annotate | Download | only in base
      1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 'use strict';
      6 
      7 /**
      8  * @fileoverview 2D Rectangle math.
      9  */
     10 base.require('base.gl_matrix');
     11 
     12 base.exportTo('base', function() {
     13 
     14   /**
     15    * Tracks a 2D bounding box.
     16    * @constructor
     17    */
     18   function Rect() {
     19     this.x = 0;
     20     this.y = 0;
     21     this.width = 0;
     22     this.height = 0;
     23   };
     24   Rect.FromXYWH = function(x, y, w, h) {
     25     var rect = new Rect();
     26     rect.x = x;
     27     rect.y = y;
     28     rect.width = w;
     29     rect.height = h;
     30     return rect;
     31   }
     32   Rect.FromArray = function(ary) {
     33     if (ary.length != 4)
     34       throw new Error('ary.length must be 4');
     35     var rect = new Rect();
     36     rect.x = ary[0];
     37     rect.y = ary[1];
     38     rect.width = ary[2];
     39     rect.height = ary[3];
     40     return rect;
     41   }
     42 
     43   Rect.prototype = {
     44     __proto__: Object.prototype,
     45 
     46     get left() {
     47       return this.x;
     48     },
     49 
     50     get top() {
     51       return this.y;
     52     },
     53 
     54     get right() {
     55       return this.x + this.width;
     56     },
     57 
     58     get bottom() {
     59       return this.y + this.height;
     60     },
     61 
     62     toString: function() {
     63       return 'Rect(' + this.x + ', ' + this.y + ', ' +
     64           this.width + ', ' + this.height + ')';
     65     },
     66 
     67     clone: function() {
     68       var rect = new Rect();
     69       rect.x = this.x;
     70       rect.y = this.y;
     71       rect.width = this.width;
     72       rect.height = this.height;
     73       return rect;
     74     },
     75 
     76     enlarge: function(pad) {
     77       var rect = new Rect();
     78       this.enlargeFast(rect, pad);
     79       return rect;
     80     },
     81 
     82     enlargeFast: function(out, pad) {
     83       out.x = this.x - pad;
     84       out.y = this.y - pad;
     85       out.width = this.width + 2 * pad;
     86       out.height = this.height + 2 * pad;
     87       return out;
     88     },
     89 
     90     size: function() {
     91       return {width: this.width, height: this.height};
     92     },
     93 
     94     scale: function(s) {
     95       var rect = new Rect();
     96       this.scaleFast(rect, s);
     97       return rect;
     98     },
     99 
    100     scaleFast: function(out, s) {
    101       out.x = this.x * s;
    102       out.y = this.y * s;
    103       out.width = this.width * s;
    104       out.height = this.height * s;
    105       return out;
    106     },
    107 
    108     translate: function(v) {
    109       var rect = new Rect();
    110       this.translateFast(rect, v);
    111       return rect;
    112     },
    113 
    114     translateFast: function(out, v) {
    115       out.x = this.x + v[0];
    116       out.y = this.x + v[1];
    117       out.width = this.width;
    118       out.height = this.height;
    119       return out;
    120     },
    121 
    122     asUVRectInside: function(containingRect) {
    123       var rect = new Rect();
    124       rect.x = (this.x - containingRect.x) / containingRect.width;
    125       rect.y = (this.y - containingRect.y) / containingRect.height;
    126       rect.width = this.width / containingRect.width;
    127       rect.height = this.height / containingRect.height;
    128       return rect;
    129     },
    130 
    131     intersects: function(that) {
    132       var ok = true;
    133       ok &= this.x < that.right;
    134       ok &= this.right > that.x;
    135       ok &= this.y < that.bottom;
    136       ok &= this.bottom > that.y;
    137       return ok;
    138     }
    139   };
    140 
    141   return {
    142     Rect: Rect
    143   };
    144 
    145 });
    146