Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Interval

A number range between two values (T0 & T1). This range is increasing when T0 < T1 and decreasing when T0 > T1.

Example

import { Interval } from 'shapetypes'

// Create an interval
const interval = new Interval(5, 10);

// Get properties of the interval
console.log(interval.length);
// => 5
console.log(interval.mid);
// => 7.5
console.log(interval.contains(8));
// => True
console.log(interval.isIncreasing);
// => True

// Create the same interval but in reverse
const reversed = new Interval(10, 5);

// Get properties of the reversed interval
console.log(reversed.length);
// => -5
console.log(reversed.contains(8));
// => True
console.log(reversed.isIncreasing);
// => False

Hierarchy

Index

Constructors

constructor

  • new Interval(T0: number, T1: number): Interval
  • Creates an interval from two values.

    Parameters

    • T0: number

      The start of the interval. Could either be the interval's min or max. If the interval is increasing, this should be the min.

    • T1: number

      The end of the interval. Could either be the interval's min or max value. If the interval is increasing, this should be the max.

    Returns Interval

Accessors

T0

  • get T0(): number

T1

  • get T1(): number

isDecreasing

  • get isDecreasing(): boolean

isIncreasing

  • get isIncreasing(): boolean

isSingleton

  • get isSingleton(): boolean
  • Checks whether T0 and T1 are the same value. Returns true if they are.

    Returns boolean

length

  • get length(): number
  • Gets the signed distance between T0 and T1. It will be positive if the interval is increasing and negative if it is decreasing.

    Returns number

lengthAbs

  • get lengthAbs(): number
  • Gets the absolute distance between T0 and T1. It will be positive regardless of whether the interval is increasing or decreasing.

    Returns number

max

  • get max(): number

mid

  • get mid(): number

min

  • get min(): number

Create Methods

Static fromValues

  • fromValues(values: keyof number[]): Interval
  • Creates an interval that encompasses a set of values.

    Example

    const interval = Interval.fromValues([5, 3, 4]);
    console.log(interval.min);
    // => 3
    console.log(interval.max);
    // => 5

    Parameters

    • values: keyof number[]

      Values to contain within the interval.

    Returns Interval

Static intersection

  • Finds the overlapping portion of two intervals and returns the resulting interval.

    Parameters

    • a: Interval

      First interval to intersect

    • b: Interval

      Second interval to intersect

    Returns Interval | undefined

    An interval representing the overlap between these two intervals. If there is no overlap, returns undefined.

Static union

Other Methods

asSorted

contains

  • contains(value: number, strict?: boolean, tolerance?: number): boolean
  • Checks whether a value is within the interval.

    Parameters

    • value: number

      The value to check for containment.

    • Default value strict: boolean = false

      If true, the value must be entirely inside the interval and can't equal min or max. If false, the value can equal min or max.

    • Default value tolerance: number = 0

      The amount the value can be outside the interval and still be considered inside.

    Returns boolean

containsPoint

  • Checks whether the value is within the interval and returns the result using the PointContainment enum. The value will be considered coincident if it equals the interval's min or max.

    Parameters

    • value: number

      The value to check for containment.

    • Default value tolerance: number = 0

      The distance the value can be from the interval's min or max and still considered coincident.

    Returns PointContainment

equals

  • equals(otherInterval: Interval): boolean
  • Checks whether another interval has the same T0 and T1 values. Returns true if it does.

    Parameters

    • otherInterval: Interval

      The interval to compare against.

    Returns boolean

grow

  • Creates a copy of this interval expanded to contain a given value. The direction of the interval, whether it is increasing or decreasing, will be preserved. Depending on where the value falls in relation to the interval, either T0 or T1 will change to accommodate the value.

    Parameters

    • toInclude: number

      Value to contain within the new interval.

    Returns Interval

remapToInterval

  • remapToInterval(value: number): number
  • Remaps a value from the global number system into the normalized parameters of this interval. See valueAt to understand how the parameters are calculated.

    Parameters

    • value: number

      Value to remap to the parameter of the interval.

    Returns number

    The value as a normalized parameter for the interval.

reverse

swap

toString

  • toString(): string
  • Gets the interval as a string in the format: [T0,T1].

    Returns string

valueAt

  • valueAt(t: number): number
  • Remaps a value from normalized parameters of this interval into the global number system. The interval's parameters range from 0 to 1.

    A parameter of 0 is equal to the start of the interval (T0). A parameter of 0.5 is the mid point of the interval. And a parameter of 1 is the end of the interval (T1).

    Parameters

    • t: number

      The parameter to remap.

    Returns number

    The parameter remapped to the global number system.

withT0

withT1

Generated using TypeDoc