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, and d is total
distance. ds includes both endpoints, while hs contains one halfway
distance per interval. These compact names are intended to read as
formulas in layout code rather than as general-purpose collection names.
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.
51 52 53 54 |
# File 'lib/sevgi/sundries/ruler.rb', line 51 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.
36 37 38 |
# File 'lib/sevgi/sundries/ruler.rb', line 36 def n @n end |
#u ⇒ Float (readonly)
Returns the unit length.
40 41 42 |
# File 'lib/sevgi/sundries/ruler.rb', line 40 def u @u end |
Class Method Details
.[](e, n) ⇒ Sevgi::Sundries::Interval
Builds an interval using bracket syntax.
32 |
# File 'lib/sevgi/sundries/ruler.rb', line 32 def self.[](e, n) = new(e, n) |
Instance Method Details
#[](i) ⇒ Float?
Returns a major tick distance by index.
59 |
# File 'lib/sevgi/sundries/ruler.rb', line 59 def [](i) = ds[i] |
#count(length) ⇒ Integer
Counts how many whole lengths fit into this interval.
67 |
# File 'lib/sevgi/sundries/ruler.rb', line 67 def count(length) = (d / positive_number(length, "Interval count length")).to_i |
#d ⇒ Float Also known as: length
Returns the total interval distance.
71 |
# File 'lib/sevgi/sundries/ruler.rb', line 71 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.
76 |
# File 'lib/sevgi/sundries/ruler.rb', line 76 def ds = @ds ||= Array.new(n + 1) { |i| i * u }.freeze |
#h ⇒ Float
Returns the midpoint distance.
80 |
# File 'lib/sevgi/sundries/ruler.rb', line 80 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.
85 |
# File 'lib/sevgi/sundries/ruler.rb', line 85 def hs = @hs ||= Array.new(n) { |i| u * (0.5 + i) }.freeze |
#nds ⇒ Integer
Returns the last major tick index.
89 |
# File 'lib/sevgi/sundries/ruler.rb', line 89 def nds = @nds ||= ds.size - 1 |
#nhs ⇒ Integer
Returns the last midpoint tick index.
93 |
# File 'lib/sevgi/sundries/ruler.rb', line 93 def nhs = @nhs ||= hs.size - 1 |