range.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. * @fileoverview A utility class for representing a numeric range.
  16. */
  17. goog.provide('goog.math.Range');
  18. goog.require('goog.asserts');
  19. /**
  20. * A number range.
  21. * @param {number} a One end of the range.
  22. * @param {number} b The other end of the range.
  23. * @struct
  24. * @constructor
  25. */
  26. goog.math.Range = function(a, b) {
  27. /**
  28. * The lowest value in the range.
  29. * @type {number}
  30. */
  31. this.start = a < b ? a : b;
  32. /**
  33. * The highest value in the range.
  34. * @type {number}
  35. */
  36. this.end = a < b ? b : a;
  37. };
  38. /**
  39. * Creates a goog.math.Range from an array of two numbers.
  40. * @param {!Array<number>} pair
  41. * @return {!goog.math.Range}
  42. */
  43. goog.math.Range.fromPair = function(pair) {
  44. goog.asserts.assert(pair.length == 2);
  45. return new goog.math.Range(pair[0], pair[1]);
  46. };
  47. /**
  48. * @return {!goog.math.Range} A clone of this Range.
  49. */
  50. goog.math.Range.prototype.clone = function() {
  51. return new goog.math.Range(this.start, this.end);
  52. };
  53. /**
  54. * @return {number} Length of the range.
  55. */
  56. goog.math.Range.prototype.getLength = function() {
  57. return this.end - this.start;
  58. };
  59. /**
  60. * Extends this range to include the given point.
  61. * @param {number} point
  62. */
  63. goog.math.Range.prototype.includePoint = function(point) {
  64. this.start = Math.min(this.start, point);
  65. this.end = Math.max(this.end, point);
  66. };
  67. /**
  68. * Extends this range to include the given range.
  69. * @param {!goog.math.Range} range
  70. */
  71. goog.math.Range.prototype.includeRange = function(range) {
  72. this.start = Math.min(this.start, range.start);
  73. this.end = Math.max(this.end, range.end);
  74. };
  75. if (goog.DEBUG) {
  76. /**
  77. * Returns a string representing the range.
  78. * @return {string} In the form [-3.5, 8.13].
  79. * @override
  80. */
  81. goog.math.Range.prototype.toString = function() {
  82. return '[' + this.start + ', ' + this.end + ']';
  83. };
  84. }
  85. /**
  86. * Compares ranges for equality.
  87. * @param {goog.math.Range} a A Range.
  88. * @param {goog.math.Range} b A Range.
  89. * @return {boolean} True iff both the starts and the ends of the ranges are
  90. * equal, or if both ranges are null.
  91. */
  92. goog.math.Range.equals = function(a, b) {
  93. if (a == b) {
  94. return true;
  95. }
  96. if (!a || !b) {
  97. return false;
  98. }
  99. return a.start == b.start && a.end == b.end;
  100. };
  101. /**
  102. * Given two ranges on the same dimension, this method returns the intersection
  103. * of those ranges.
  104. * @param {goog.math.Range} a A Range.
  105. * @param {goog.math.Range} b A Range.
  106. * @return {goog.math.Range} A new Range representing the intersection of two
  107. * ranges, or null if there is no intersection. Ranges are assumed to
  108. * include their end points, and the intersection can be a point.
  109. */
  110. goog.math.Range.intersection = function(a, b) {
  111. var c0 = Math.max(a.start, b.start);
  112. var c1 = Math.min(a.end, b.end);
  113. return (c0 <= c1) ? new goog.math.Range(c0, c1) : null;
  114. };
  115. /**
  116. * Given two ranges on the same dimension, determines whether they intersect.
  117. * @param {goog.math.Range} a A Range.
  118. * @param {goog.math.Range} b A Range.
  119. * @return {boolean} Whether they intersect.
  120. */
  121. goog.math.Range.hasIntersection = function(a, b) {
  122. return Math.max(a.start, b.start) <= Math.min(a.end, b.end);
  123. };
  124. /**
  125. * Given two ranges on the same dimension, this returns a range that covers
  126. * both 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 bounding
  130. * range.
  131. */
  132. goog.math.Range.boundingRange = function(a, b) {
  133. return new goog.math.Range(
  134. Math.min(a.start, b.start), Math.max(a.end, b.end));
  135. };
  136. /**
  137. * Given two ranges, returns true if the first range completely overlaps the
  138. * second.
  139. * @param {goog.math.Range} a The first Range.
  140. * @param {goog.math.Range} b The second Range.
  141. * @return {boolean} True if b is contained inside a, false otherwise.
  142. */
  143. goog.math.Range.contains = function(a, b) {
  144. return a.start <= b.start && a.end >= b.end;
  145. };
  146. /**
  147. * Given a range and a point, returns true if the range contains the point.
  148. * @param {goog.math.Range} range The range.
  149. * @param {number} p The point.
  150. * @return {boolean} True if p is contained inside range, false otherwise.
  151. */
  152. goog.math.Range.containsPoint = function(range, p) {
  153. return range.start <= p && range.end >= p;
  154. };