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.
8 * @fileoverview 2D Rectangle math.
10 tvcm.require('tvcm.gl_matrix');
12 tvcm.exportTo('tvcm', function() {
15 * Tracks a 2D bounding box.
24 Rect.fromXYWH = function(x, y, w, h) {
25 var rect = new Rect();
32 Rect.fromArray = function(ary) {
34 throw new Error('ary.length must be 4');
35 var rect = new Rect();
44 __proto__: Object.prototype,
55 return this.x + this.width;
59 return this.y + this.height;
62 toString: function() {
63 return 'Rect(' + this.x + ', ' + this.y + ', ' +
64 this.width + ', ' + this.height + ')';
68 return [this.x, this.y, this.width, this.height];
72 var rect = new Rect();
75 rect.width = this.width;
76 rect.height = this.height;
80 enlarge: function(pad) {
81 var rect = new Rect();
82 this.enlargeFast(rect, pad);
86 enlargeFast: function(out, pad) {
89 out.width = this.width + 2 * pad;
90 out.height = this.height + 2 * pad;
95 return {width: this.width, height: this.height};
99 var rect = new Rect();
100 this.scaleFast(rect, s);
104 scaleSize: function(s) {
105 return Rect.fromXYWH(this.x, this.y, this.width * s, this.height * s);
108 scaleFast: function(out, s) {
111 out.width = this.width * s;
112 out.height = this.height * s;
116 translate: function(v) {
117 var rect = new Rect();
118 this.translateFast(rect, v);
122 translateFast: function(out, v) {
123 out.x = this.x + v[0];
124 out.y = this.x + v[1];
125 out.width = this.width;
126 out.height = this.height;
130 asUVRectInside: function(containingRect) {
131 var rect = new Rect();
132 rect.x = (this.x - containingRect.x) / containingRect.width;
133 rect.y = (this.y - containingRect.y) / containingRect.height;
134 rect.width = this.width / containingRect.width;
135 rect.height = this.height / containingRect.height;
139 intersects: function(that) {
141 ok &= this.x < that.right;
142 ok &= this.right > that.x;
143 ok &= this.y < that.bottom;
144 ok &= this.bottom > that.y;
148 equalTo: function(rect) {
150 (this.x === rect.x) &&
151 (this.y === rect.y) &&
152 (this.width === rect.width) &&
153 (this.height === rect.height);