Interval.js 972 B

12345678910111213141516171819202122232425262728293031323334353637
  1. import Assert from '../../util/Assert'
  2. export default class Interval {
  3. constructor() {
  4. Interval.constructor_.apply(this, arguments)
  5. }
  6. static constructor_() {
  7. this._min = null
  8. this._max = null
  9. if (arguments.length === 1) {
  10. const other = arguments[0]
  11. Interval.constructor_.call(this, other._min, other._max)
  12. } else if (arguments.length === 2) {
  13. const min = arguments[0], max = arguments[1]
  14. Assert.isTrue(min <= max)
  15. this._min = min
  16. this._max = max
  17. }
  18. }
  19. expandToInclude(other) {
  20. this._max = Math.max(this._max, other._max)
  21. this._min = Math.min(this._min, other._min)
  22. return this
  23. }
  24. getCentre() {
  25. return (this._min + this._max) / 2
  26. }
  27. intersects(other) {
  28. return !(other._min > this._max || other._max < this._min)
  29. }
  30. equals(o) {
  31. if (!(o instanceof Interval))
  32. return false
  33. const other = o
  34. return this._min === other._min && this._max === other._max
  35. }
  36. }