Class: Sevgi::Sundries::Interval
- Inherits:
-
Object
- Object
- Sevgi::Sundries::Interval
- Defined in:
- lib/sevgi/sundries/ruler.rb
Overview
A one-dimensional interval divided into equal units.
The compact reader names are part of the domain vocabulary:
u is the unit length, n is the interval count, d is total
distance, and h is the midpoint distance.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#n ⇒ Integer
readonly
Returns the interval count.
-
#u ⇒ Float
readonly
Returns the unit length.
Class Method Summary collapse
-
.[](e, n) ⇒ Sevgi::Sundries::Interval
Builds an interval using bracket syntax.
Instance Method Summary collapse
-
#[](i) ⇒ Float?
Returns a major tick distance by index.
-
#count(length) ⇒ Integer
Counts how many whole lengths fit into this interval.
-
#d ⇒ Float
(also: #length)
Returns the total interval distance.
-
#ds ⇒ Array<Float>
Returns major tick distances, including both endpoints.
-
#h ⇒ Float
Returns the midpoint distance.
-
#hs ⇒ Array<Float>
Returns midpoint tick distances for each interval segment.
-
#initialize(e, n) ⇒ void
constructor
Creates an interval.
-
#nds ⇒ Integer
Returns the last major tick index.
-
#nhs ⇒ Integer
Returns the last midpoint tick index.
Constructor Details
#initialize(e, n) ⇒ void
Creates an interval.
46 47 48 49 |
# File 'lib/sevgi/sundries/ruler.rb', line 46 def initialize(e, n) @n = non_negative_integer(n, "Interval count") @u = measure(e) end |
Instance Attribute Details
#n ⇒ Integer (readonly)
Returns the interval count.
31 32 33 |
# File 'lib/sevgi/sundries/ruler.rb', line 31 def n @n end |
#u ⇒ Float (readonly)
Returns the unit length.
35 36 37 |
# File 'lib/sevgi/sundries/ruler.rb', line 35 def u @u end |
Class Method Details
.[](e, n) ⇒ Sevgi::Sundries::Interval
Builds an interval using bracket syntax.
27 |
# File 'lib/sevgi/sundries/ruler.rb', line 27 def self.[](e, n) = new(e, n) |
Instance Method Details
#[](i) ⇒ Float?
Returns a major tick distance by index.
54 |
# File 'lib/sevgi/sundries/ruler.rb', line 54 def [](i) = ds[i] |
#count(length) ⇒ Integer
Counts how many whole lengths fit into this interval.
62 |
# File 'lib/sevgi/sundries/ruler.rb', line 62 def count(length) = (d / positive_number(length, "Interval count length")).to_i |
#d ⇒ Float Also known as: length
Returns the total interval distance.
66 |
# File 'lib/sevgi/sundries/ruler.rb', line 66 def d = @d ||= n * u |
#ds ⇒ Array<Float>
Returns major tick distances, including both endpoints. The memoized collection is frozen and must be treated as immutable.
71 |
# File 'lib/sevgi/sundries/ruler.rb', line 71 def ds = @ds ||= Array.new(n + 1) { |i| i * u }.freeze |
#h ⇒ Float
Returns the midpoint distance.
75 |
# File 'lib/sevgi/sundries/ruler.rb', line 75 def h = @h ||= d / 2.0 |
#hs ⇒ Array<Float>
Returns midpoint tick distances for each interval segment. The memoized collection is frozen and must be treated as immutable.
80 |
# File 'lib/sevgi/sundries/ruler.rb', line 80 def hs = @hs ||= Array.new(n) { |i| u * (0.5 + i) }.freeze |
#nds ⇒ Integer
Returns the last major tick index.
84 |
# File 'lib/sevgi/sundries/ruler.rb', line 84 def nds = @nds ||= ds.size - 1 |
#nhs ⇒ Integer
Returns the last midpoint tick index.
88 |
# File 'lib/sevgi/sundries/ruler.rb', line 88 def nhs = @nhs ||= hs.size - 1 |