3 Copyright (c) 2014 The Chromium Authors. All rights reserved.
4 Use of this source code is governed by a BSD-style license that can be
5 found in the LICENSE file.
7 <link rel="import" href="/tvcm.html">
8 <link rel="import" href="/tvcm/gl_matrix.html">
13 * @fileoverview 2D Rectangle math.
16 tvcm.exportTo('tvcm', function() {
19 * Tracks a 2D bounding box.
28 Rect.fromXYWH = function(x, y, w, h) {
29 var rect = new Rect();
36 Rect.fromArray = function(ary) {
38 throw new Error('ary.length must be 4');
39 var rect = new Rect();
48 __proto__: Object.prototype,
59 return this.x + this.width;
63 return this.y + this.height;
66 toString: function() {
67 return 'Rect(' + this.x + ', ' + this.y + ', ' +
68 this.width + ', ' + this.height + ')';
72 return [this.x, this.y, this.width, this.height];
76 var rect = new Rect();
79 rect.width = this.width;
80 rect.height = this.height;
84 enlarge: function(pad) {
85 var rect = new Rect();
86 this.enlargeFast(rect, pad);
90 enlargeFast: function(out, pad) {
93 out.width = this.width + 2 * pad;
94 out.height = this.height + 2 * pad;
99 return {width: this.width, height: this.height};
103 var rect = new Rect();
104 this.scaleFast(rect, s);
108 scaleSize: function(s) {
109 return Rect.fromXYWH(this.x, this.y, this.width * s, this.height * s);
112 scaleFast: function(out, s) {
115 out.width = this.width * s;
116 out.height = this.height * s;
120 translate: function(v) {
121 var rect = new Rect();
122 this.translateFast(rect, v);
126 translateFast: function(out, v) {
127 out.x = this.x + v[0];
128 out.y = this.x + v[1];
129 out.width = this.width;
130 out.height = this.height;
134 asUVRectInside: function(containingRect) {
135 var rect = new Rect();
136 rect.x = (this.x - containingRect.x) / containingRect.width;
137 rect.y = (this.y - containingRect.y) / containingRect.height;
138 rect.width = this.width / containingRect.width;
139 rect.height = this.height / containingRect.height;
143 intersects: function(that) {
145 ok &= this.x < that.right;
146 ok &= this.right > that.x;
147 ok &= this.y < that.bottom;
148 ok &= this.bottom > that.y;
152 equalTo: function(rect) {
154 (this.x === rect.x) &&
155 (this.y === rect.y) &&
156 (this.width === rect.width) &&
157 (this.height === rect.height);