Path: blob/trunk/third_party/closure/goog/math/range.js
2868 views
// Copyright 2006 The Closure Library Authors. All Rights Reserved.1//2// Licensed under the Apache License, Version 2.0 (the "License");3// you may not use this file except in compliance with the License.4// You may obtain a copy of the License at5//6// http://www.apache.org/licenses/LICENSE-2.07//8// Unless required by applicable law or agreed to in writing, software9// distributed under the License is distributed on an "AS-IS" BASIS,10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11// See the License for the specific language governing permissions and12// limitations under the License.1314/**15* @fileoverview A utility class for representing a numeric range.16*/171819goog.provide('goog.math.Range');2021goog.require('goog.asserts');22232425/**26* A number range.27* @param {number} a One end of the range.28* @param {number} b The other end of the range.29* @struct30* @constructor31*/32goog.math.Range = function(a, b) {33/**34* The lowest value in the range.35* @type {number}36*/37this.start = a < b ? a : b;3839/**40* The highest value in the range.41* @type {number}42*/43this.end = a < b ? b : a;44};454647/**48* Creates a goog.math.Range from an array of two numbers.49* @param {!Array<number>} pair50* @return {!goog.math.Range}51*/52goog.math.Range.fromPair = function(pair) {53goog.asserts.assert(pair.length == 2);54return new goog.math.Range(pair[0], pair[1]);55};565758/**59* @return {!goog.math.Range} A clone of this Range.60*/61goog.math.Range.prototype.clone = function() {62return new goog.math.Range(this.start, this.end);63};646566/**67* @return {number} Length of the range.68*/69goog.math.Range.prototype.getLength = function() {70return this.end - this.start;71};727374/**75* Extends this range to include the given point.76* @param {number} point77*/78goog.math.Range.prototype.includePoint = function(point) {79this.start = Math.min(this.start, point);80this.end = Math.max(this.end, point);81};828384/**85* Extends this range to include the given range.86* @param {!goog.math.Range} range87*/88goog.math.Range.prototype.includeRange = function(range) {89this.start = Math.min(this.start, range.start);90this.end = Math.max(this.end, range.end);91};929394if (goog.DEBUG) {95/**96* Returns a string representing the range.97* @return {string} In the form [-3.5, 8.13].98* @override99*/100goog.math.Range.prototype.toString = function() {101return '[' + this.start + ', ' + this.end + ']';102};103}104105106/**107* Compares ranges for equality.108* @param {goog.math.Range} a A Range.109* @param {goog.math.Range} b A Range.110* @return {boolean} True iff both the starts and the ends of the ranges are111* equal, or if both ranges are null.112*/113goog.math.Range.equals = function(a, b) {114if (a == b) {115return true;116}117if (!a || !b) {118return false;119}120return a.start == b.start && a.end == b.end;121};122123124/**125* Given two ranges on the same dimension, this method returns the intersection126* of those ranges.127* @param {goog.math.Range} a A Range.128* @param {goog.math.Range} b A Range.129* @return {goog.math.Range} A new Range representing the intersection of two130* ranges, or null if there is no intersection. Ranges are assumed to131* include their end points, and the intersection can be a point.132*/133goog.math.Range.intersection = function(a, b) {134var c0 = Math.max(a.start, b.start);135var c1 = Math.min(a.end, b.end);136return (c0 <= c1) ? new goog.math.Range(c0, c1) : null;137};138139140/**141* Given two ranges on the same dimension, determines whether they intersect.142* @param {goog.math.Range} a A Range.143* @param {goog.math.Range} b A Range.144* @return {boolean} Whether they intersect.145*/146goog.math.Range.hasIntersection = function(a, b) {147return Math.max(a.start, b.start) <= Math.min(a.end, b.end);148};149150151/**152* Given two ranges on the same dimension, this returns a range that covers153* both ranges.154* @param {goog.math.Range} a A Range.155* @param {goog.math.Range} b A Range.156* @return {!goog.math.Range} A new Range representing the bounding157* range.158*/159goog.math.Range.boundingRange = function(a, b) {160return new goog.math.Range(161Math.min(a.start, b.start), Math.max(a.end, b.end));162};163164165/**166* Given two ranges, returns true if the first range completely overlaps the167* second.168* @param {goog.math.Range} a The first Range.169* @param {goog.math.Range} b The second Range.170* @return {boolean} True if b is contained inside a, false otherwise.171*/172goog.math.Range.contains = function(a, b) {173return a.start <= b.start && a.end >= b.end;174};175176177/**178* Given a range and a point, returns true if the range contains the point.179* @param {goog.math.Range} range The range.180* @param {number} p The point.181* @return {boolean} True if p is contained inside range, false otherwise.182*/183goog.math.Range.containsPoint = function(range, p) {184return range.start <= p && range.end >= p;185};186187188