diff options
author | Nat Lasseter <user@4574.co.uk> | 2024-06-24 16:50:36 +0100 |
---|---|---|
committer | Nat Lasseter <user@4574.co.uk> | 2024-06-24 16:50:36 +0100 |
commit | 2f025ebfc6c9e3146ef1c356f99045edc203c9ee (patch) | |
tree | db279ad3999be6a545d7ba80eef521df63592b66 /interval.go | |
parent | 63cee9aa6eb62afb2e8e726d2de16b76935c5f4c (diff) |
Chapter 8
Diffstat (limited to 'interval.go')
-rw-r--r-- | interval.go | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/interval.go b/interval.go index 5f44f51..763939a 100644 --- a/interval.go +++ b/interval.go @@ -1,5 +1,9 @@ package main +import ( + "math/rand" +) + type Interval struct { Min float64 Max float64 @@ -16,3 +20,17 @@ func (i Interval) Include(x float64) bool { func (i Interval) Surround(x float64) bool { return i.Min < x && x < i.Max; } + +func (i Interval) Clamp(x float64) float64 { + if x < i.Min { + return i.Min; + } + if x > i.Max { + return i.Max; + } + return x; +} + +func (i Interval) Sample() float64 { + return rand.Float64() * i.Size() + i.Min; +} |