Options
All
  • Public
  • Public/Protected
  • All
Menu

Class IntervalSorted

A special Interval where T0 is always the smallest value and T1 is always the largest. As a result, isIncreasing will always return true.

Example

import { IntervalSorted } from 'shapetypes'

// Create sorted interval
const interval = new IntervalSorted(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

Hierarchy

Index

Constructors

constructor

  • Creates a sorted interval from two values. The values don't need to be provided in order as they are sorted when the interval is constructed. This means that the T0 value may be reassigned to T1 if it's the largest value, and vice versa.

    Parameters

    • T0: number

      One end of the interval. This value may be assigned to T1 if it is the largest parameter.

    • T1: number

      The other end of the interval. This value may be assigned to T0 if it is the smallest parameter.

    Returns IntervalSorted

Accessors

T0

  • get T0(): number

T1

  • get T1(): number

isDecreasing

  • get isDecreasing(): boolean

isIncreasing

  • get isIncreasing(): boolean

isSingleton

  • get isSingleton(): 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 fromCenter

  • Creates an interval of a given length, centered on a value.

    Parameters

    • center: number

      The middle value of the interval.

    • width: number

      The length of the interval.

    Returns IntervalSorted

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

inflate

  • Moves the min and max apart by a set amount. Returns the enlarged interval.

    Example

    const interval = new IntervalSorted(5, 10);
    const expanded = interval.inflate(2);
    console.log(expanded.min);
    // => 3
    console.log(expanded.max);
    // => 12

    Parameters

    • amount: number

      The distance to move the min and max values. If positive, the overall length of the interval will grow. If negative, the overall length will shrink.

    Returns IntervalSorted

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

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.

withMax

  • Creates a copy of this interval with a different max value.

    note

    Throws an error if the new maximum is less than the current minimum.

    Parameters

    • newMax: number

      New max value for the new interval

    Returns IntervalSorted

withMin

  • Creates a copy of this interval with a different min value.

    note

    Throws an error if the new minimum is greater than the current maximum.

    Parameters

    • newMin: number

      New min value for the new interval

    Returns IntervalSorted

withT0

withT1

Generated using TypeDoc