Debian Codegraph @

closure-library/closure/goog/math/range.js

raw
1 // Copyright 2006 The Closure Library Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS-IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 /**
16 * @fileoverview A utility class for representing a numeric range.
17 */
18
19
20 goog.provide('goog.math.Range');
21
22 goog.require('goog.asserts');
23
24
25
26 /**
27 * A number range.
28 * @param {number} a One end of the range.
29 * @param {number} b The other end of the range.
30 * @struct
31 * @constructor
32 */
33 goog.math.Range = function(a, b) {
34 /**
35 * The lowest value in the range.
36 * @type {number}
37 */
38 this.start = a < b ? a : b;
39
40 /**
41 * The highest value in the range.
42 * @type {number}
43 */
44 this.end = a < b ? b : a;
45 };
46
47
48 /**
49 * Creates a goog.math.Range from an array of two numbers.
50 * @param {!Array<number>} pair
51 * @return {!goog.math.Range}
52 */
53 goog.math.Range.fromPair = function(pair) {
54 goog.asserts.assert(pair.length == 2);
55 return new goog.math.Range(pair[0], pair[1]);
56 };
57
58
59 /**
60 * @return {!goog.math.Range} A clone of this Range.
61 */
62 goog.math.Range.prototype.clone = function() {
63 return new goog.math.Range(this.start, this.end);
64 };
65
66
67 /**
68 * @return {number} Length of the range.
69 */
70 goog.math.Range.prototype.getLength = function() {
71 return this.end - this.start;
72 };
73
74
75 /**
76 * Extends this range to include the given point.
77 * @param {number} point
78 */
79 goog.math.Range.prototype.includePoint = function(point) {
80 this.start = Math.min(this.start, point);
81 this.end = Math.max(this.end, point);
82 };
83
84
85 /**
86 * Extends this range to include the given range.
87 * @param {!goog.math.Range} range
88 */
89 goog.math.Range.prototype.includeRange = function(range) {
90 this.start = Math.min(this.start, range.start);
91 this.end = Math.max(this.end, range.end);
92 };
93
94
95 if (goog.DEBUG) {
96 /**
97 * Returns a string representing the range.
98 * @return {string} In the form [-3.5, 8.13].
99 * @override
100 */
101 goog.math.Range.prototype.toString = function() {
102 return '[' + this.start + ', ' + this.end + ']';
103 };
104 }
105
106
107 /**
108 * Compares ranges for equality.
109 * @param {goog.math.Range} a A Range.
110 * @param {goog.math.Range} b A Range.
111 * @return {boolean} True iff both the starts and the ends of the ranges are
112 * equal, or if both ranges are null.
113 */
114 goog.math.Range.equals = function(a, b) {
115 if (a == b) {
116 return true;
117 }
118 if (!a || !b) {
119 return false;
120 }
121 return a.start == b.start && a.end == b.end;
122 };
123
124
125 /**
126 * Given two ranges on the same dimension, this method returns the intersection
127 * of those ranges.
128 * @param {goog.math.Range} a A Range.
129 * @param {goog.math.Range} b A Range.
130 * @return {goog.math.Range} A new Range representing the intersection of two
131 * ranges, or null if there is no intersection. Ranges are assumed to
132 * include their end points, and the intersection can be a point.
133 */
134 goog.math.Range.intersection = function(a, b) {
135 var c0 = Math.max(a.start, b.start);
136 var c1 = Math.min(a.end, b.end);
137 return (c0 <= c1) ? new goog.math.Range(c0, c1) : null;
138 };
139
140
141 /**
142 * Given two ranges on the same dimension, determines whether they intersect.
143 * @param {goog.math.Range} a A Range.
144 * @param {goog.math.Range} b A Range.
145 * @return {boolean} Whether they intersect.
146 */
147 goog.math.Range.hasIntersection = function(a, b) {
148 return Math.max(a.start, b.start) <= Math.min(a.end, b.end);
149 };
150
151
152 /**
153 * Given two ranges on the same dimension, this returns a range that covers
154 * both ranges.
155 * @param {goog.math.Range} a A Range.
156 * @param {goog.math.Range} b A Range.
157 * @return {!goog.math.Range} A new Range representing the bounding
158 * range.
159 */
160 goog.math.Range.boundingRange = function(a, b) {
161 return new goog.math.Range(
162 Math.min(a.start, b.start), Math.max(a.end, b.end));
163 };
164
165
166 /**
167 * Given two ranges, returns true if the first range completely overlaps the
168 * second.
169 * @param {goog.math.Range} a The first Range.
170 * @param {goog.math.Range} b The second Range.
171 * @return {boolean} True if b is contained inside a, false otherwise.
172 */
173 goog.math.Range.contains = function(a, b) {
174 return a.start <= b.start && a.end >= b.end;
175 };
176
177
178 /**
179 * Given a range and a point, returns true if the range contains the point.
180 * @param {goog.math.Range} range The range.
181 * @param {number} p The point.
182 * @return {boolean} True if p is contained inside range, false otherwise.
183 */
184 goog.math.Range.containsPoint = function(range, p) {
185 return range.start <= p && range.end >= p;
186 };