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 bounding box computations.
10 base.require('base.gl_matrix');
11 base.require('base.rect');
13 base.exportTo('base', function() {
16 * Tracks a 2D bounding box.
21 this.min_ = undefined;
22 this.max_ = undefined;
26 __proto__: Object.prototype,
30 this.min_ = undefined;
31 this.max_ = undefined;
38 addBBox2: function(bbox2) {
41 this.addVec2(bbox2.min_);
42 this.addVec2(bbox2.max_);
46 var bbox = new BBox2();
52 * Adds x, y to the range.
54 addXY: function(x, y) {
56 this.max_ = vec2.create();
57 this.min_ = vec2.create();
58 vec2.set(this.max_, x, y);
59 vec2.set(this.min_, x, y);
60 this.isEmpty_ = false;
63 this.max_[0] = Math.max(this.max_[0], x);
64 this.max_[1] = Math.max(this.max_[1], y);
65 this.min_[0] = Math.min(this.min_[0], x);
66 this.min_[1] = Math.min(this.min_[1], y);
70 * Adds value_x, value_y in the form [value_x,value_y] to the range.
72 addVec2: function(value) {
74 this.max_ = vec2.create();
75 this.min_ = vec2.create();
76 vec2.set(this.max_, value[0], value[1]);
77 vec2.set(this.min_, value[0], value[1]);
78 this.isEmpty_ = false;
81 this.max_[0] = Math.max(this.max_[0], value[0]);
82 this.max_[1] = Math.max(this.max_[1], value[1]);
83 this.min_[0] = Math.min(this.min_[0], value[0]);
84 this.min_[1] = Math.min(this.min_[1], value[1]);
87 addQuad: function(quad) {
88 this.addVec2(quad.p1);
89 this.addVec2(quad.p2);
90 this.addVec2(quad.p3);
91 this.addVec2(quad.p4);
108 throw new Error('Empty BBox2 has no size');
109 var size = vec2.create();
110 vec2.subtract(size, this.max_, this.min_);
116 throw new Error('Empty BBox2 has no size');
117 return {width: this.max_[0] - this.min_[0],
118 height: this.max_[1] - this.min_[1]};
123 throw new Error('Empty BBox2 has no width');
124 return this.max_[0] - this.min_[0];
129 throw new Error('Empty BBox2 has no width');
130 return this.max_[1] - this.min_[1];
133 toString: function() {
136 return 'min=(' + this.min_[0] + ',' + this.min_[1] + ') ' +
137 'max=(' + this.max_[0] + ',' + this.max_[1] + ')';
141 return base.Rect.fromXYWH(
144 this.max_[0] - this.min_[0],
145 this.max_[1] - this.min_[1]);