1<!DOCTYPE html>
2<!--
3Copyright (c) 2014 The Chromium Authors. All rights reserved.
4Use of this source code is governed by a BSD-style license that can be
5found in the LICENSE file.
6-->
7<link rel="import" href="/tracing/base/base.html">
8<link rel="import" href="/tracing/base/math.html">
9<script>
10'use strict';
11
12tr.exportTo('tr.b', 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    toArray: function() {
68      return [this.x, this.y, this.width, this.height];
69    },
70
71    clone: function() {
72      var rect = new Rect();
73      rect.x = this.x;
74      rect.y = this.y;
75      rect.width = this.width;
76      rect.height = this.height;
77      return rect;
78    },
79
80    enlarge: function(pad) {
81      var rect = new Rect();
82      this.enlargeFast(rect, pad);
83      return rect;
84    },
85
86    enlargeFast: function(out, pad) {
87      out.x = this.x - pad;
88      out.y = this.y - pad;
89      out.width = this.width + 2 * pad;
90      out.height = this.height + 2 * pad;
91      return out;
92    },
93
94    size: function() {
95      return {width: this.width, height: this.height};
96    },
97
98    scale: function(s) {
99      var rect = new Rect();
100      this.scaleFast(rect, s);
101      return rect;
102    },
103
104    scaleSize: function(s) {
105      return Rect.fromXYWH(this.x, this.y, this.width * s, this.height * s);
106    },
107
108    scaleFast: function(out, s) {
109      out.x = this.x * s;
110      out.y = this.y * s;
111      out.width = this.width * s;
112      out.height = this.height * s;
113      return out;
114    },
115
116    translate: function(v) {
117      var rect = new Rect();
118      this.translateFast(rect, v);
119      return rect;
120    },
121
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;
127      return out;
128    },
129
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;
136      return rect;
137    },
138
139    intersects: function(that) {
140      var ok = true;
141      ok &= this.x < that.right;
142      ok &= this.right > that.x;
143      ok &= this.y < that.bottom;
144      ok &= this.bottom > that.y;
145      return ok;
146    },
147
148    equalTo: function(rect) {
149      return rect &&
150             (this.x === rect.x) &&
151             (this.y === rect.y) &&
152             (this.width === rect.width) &&
153             (this.height === rect.height);
154    }
155  };
156
157  return {
158    Rect: Rect
159  };
160
161});
162</script>
163