opensolid/geometry/2.1.0

NOTE: this package is not compatible with Elm 0.19.1

OpenSolid.Interval

type alias Interval =
Interval

Represents a bounded interval with a minimum and maximum value, for example the interval from 3 to 5.

Constructors

{ minValue : Float
, maxValue : Float
}
-> Interval

Construct an interval with the given minimum and maximum values. They should be provided in the correct order but will be flipped if necessary to result in a valid interval.

exampleInterval =
    Interval.with { minValue = -1, maxValue = 5 }
singleton : Float -> Interval

Construct a zero-width interval containing a single value.

Interval.singleton 3
--> Interval.with { minValue = 3, maxValue = 3 }
hull : Interval -> Interval -> Interval

Construct an interval containing both of the given intervals.

firstInterval =
    Interval.with { minValue = 1, maxValue = 2 }


secondInterval =
    Interval.with { minValue = 3, maxValue = 6 }


Interval.hull firstInterval secondInterval
--> Interval.with { minValue = 1, maxValue = 6 }
intersection : Interval -> Interval -> Maybe Interval

Attempt to construct an interval containing all the values common to both given intervals. If the intervals do not intersect, returns Nothing.

firstInterval =
    Interval.with { minValue = 1, maxValue = 3 }


secondInterval =
    Interval.with { minValue = 2, maxValue = 5 }


thirdInterval =
    Interval.with { minValue = 4, maxValue = 7 }


Interval.intersection firstInterval secondInterval
--> Just (Interval.with { minValue = 2, maxValue = 3 })


Interval.intersection firstInterval thirdInterval
--> Nothing

If the two intervals just touch, a singleton interval will be returned:

Interval.intersection
    (Interval.with { minValue = 1, maxValue = 2 })
    (Interval.with { minValue = 2, maxValue = 3 })
--> Just (Interval.with { minValue = 2, maxValue = 2})
hullOf : List Interval -> Maybe Interval

Construct an interval containing all of the intervals in the given list. If the list is empty, returns Nothing.

Interval.hullOf
    [ Interval.singleton 2
    , Interval.with { minValue = 3, maxValue = 4 }
    ]
--> Just (Interval.with { minValue = 2, maxValue = 4 })


Interval.hullOf []
--> Nothing

Properties

Interval
-> { minValue : Float
, maxValue : Float
}

Get the minimum and maximum values of an interval as a single record. Useful when combined with destructuring:

{ minValue, maxValue } =
    Interval.extrema exampleInterval




--> minValue = -1
--> maxValue = 5
minValue : Interval -> Float

Get the minimum value of an interval.

Interval.minValue exampleInterval
--> -1
maxValue : Interval -> Float

Get the maximum value of an interval.

Interval.maxValue exampleInterval
--> 5
midpoint : Interval -> Float

Get the midpoint of an interval.

Interval.midpoint exampleInterval
--> 2
width : Interval -> Float

Get the width of an interval.

Interval.width exampleInterval
--> 6

Interpolation

interpolate : Float -> Interval -> Float

Interpolate an interval from its minimum to its maximum value; a value of 0.0 corresponds to the minimum value of the interval, a value of 0.5 corresponds to its midpoint and a value of 1.0 corresponds to its maximum value. Values less than 0.0 or greater than 1.0 can be used to extrapolate.

Interval.interpolate 0 exampleInterval
--> -1


Interval.interpolate 0.75 exampleInterval
--> 3.5


Interval.interpolate -0.5 exampleInterval
--> -4

Arithmetic

sin : Interval -> Interval

Get the image of sin(x) applied on the interval.

Interval.with { minValue = -pi / 2, maxValue = pi / 2}
    |> Interval.sin
--> Interval.with { minValue = -1, maxValue = 1 }
cos : Interval -> Interval

Get the image of cos(x) applied on the interval.

Interval.with { minValue = -pi / 2, maxValue = pi / 2}
    |> Interval.cos
--> Interval.with { minValue = 0, maxValue = 1 }

Queries

contains : Float -> Interval -> Bool

Check if an interval contains a given value.

Interval.contains 0 exampleInterval
--> True


Interval.contains 10 exampleInterval
--> False

The minimum and maximum values of an interval are considered to be contained in the interval, although if you are relying on this then there is a good chance your code is vulnerable to numerical roundoff!

Interval.contains -1 exampleInterval
--> True
intersects : Interval -> Interval -> Bool

Check if two intervals touch or overlap (have any values in common).

Interval.with { minValue = 0, maxValue = 10 }
    |> Interval.intersects exampleInterval
--> True


Interval.with { minValue = 10, maxValue = 20 }
    |> Interval.intersects exampleInterval
--> False

Intervals that just touch each other are considered to intersect (this is consistent with intersection which will return a zero-width interval for the intersection of two just-touching intervals):

Interval.with { minValue = -3, maxValue = -1 }
    |> Interval.intersects exampleInterval
--> True

As with contains, though, if you are relying on this then there is a good chance your code is vulnerable to numerical roundoff!

isContainedIn : Interval -> Interval -> Bool

Check if the second interval is fully contained in the first.

-- Note that here exampleInterval is actually the
-- *second* interval due to how |> works
exampleInterval
    |> Interval.isContainedIn
        (Interval.with
            { minValue = 0
            , maxValue = 10
            }
        )
--> False


exampleInterval
    |> Interval.isContainedIn
        (Interval.with
            { minValue = -10
            , maxValue = 10
            }
        )
--> True