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="/base.html">
8 <link rel="import" href="/base/gl_matrix.html">
9 <link rel="import" href="/base/rect.html">
15 * @fileoverview 2D bounding box computations.
17 tv.exportTo('tv', function() {
20 * Tracks a 2D bounding box.
25 this.min_ = undefined;
26 this.max_ = undefined;
30 __proto__: Object.prototype,
34 this.min_ = undefined;
35 this.max_ = undefined;
42 addBBox2: function(bbox2) {
45 this.addVec2(bbox2.min_);
46 this.addVec2(bbox2.max_);
50 var bbox = new BBox2();
56 * Adds x, y to the range.
58 addXY: function(x, y) {
60 this.max_ = vec2.create();
61 this.min_ = vec2.create();
62 vec2.set(this.max_, x, y);
63 vec2.set(this.min_, x, y);
64 this.isEmpty_ = false;
67 this.max_[0] = Math.max(this.max_[0], x);
68 this.max_[1] = Math.max(this.max_[1], y);
69 this.min_[0] = Math.min(this.min_[0], x);
70 this.min_[1] = Math.min(this.min_[1], y);
74 * Adds value_x, value_y in the form [value_x,value_y] to the range.
76 addVec2: function(value) {
78 this.max_ = vec2.create();
79 this.min_ = vec2.create();
80 vec2.set(this.max_, value[0], value[1]);
81 vec2.set(this.min_, value[0], value[1]);
82 this.isEmpty_ = false;
85 this.max_[0] = Math.max(this.max_[0], value[0]);
86 this.max_[1] = Math.max(this.max_[1], value[1]);
87 this.min_[0] = Math.min(this.min_[0], value[0]);
88 this.min_[1] = Math.min(this.min_[1], value[1]);
91 addQuad: function(quad) {
92 this.addVec2(quad.p1);
93 this.addVec2(quad.p2);
94 this.addVec2(quad.p3);
95 this.addVec2(quad.p4);
112 throw new Error('Empty BBox2 has no size');
113 var size = vec2.create();
114 vec2.subtract(size, this.max_, this.min_);
120 throw new Error('Empty BBox2 has no size');
121 return {width: this.max_[0] - this.min_[0],
122 height: this.max_[1] - this.min_[1]};
127 throw new Error('Empty BBox2 has no width');
128 return this.max_[0] - this.min_[0];
133 throw new Error('Empty BBox2 has no width');
134 return this.max_[1] - this.min_[1];
137 toString: function() {
140 return 'min=(' + this.min_[0] + ',' + this.min_[1] + ') ' +
141 'max=(' + this.max_[0] + ',' + this.max_[1] + ')';
145 return tv.Rect.fromXYWH(
148 this.max_[0] - this.min_[0],
149 this.max_[1] - this.min_[1]);