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