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
Interval count.
-
#u ⇒ Object
readonly
Returns the value of attribute u.
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.
38 39 40 41 42 43 |
# File 'lib/sevgi/sundries/ruler.rb', line 38 def initialize(e, n) ArgumentError.("Interval count must be a non-negative Integer") unless n.is_a?(::Integer) && !n.negative? @u = measure(e) @n = n end |
Instance Attribute Details
#n ⇒ Integer (readonly)
Returns interval count.
30 31 32 |
# File 'lib/sevgi/sundries/ruler.rb', line 30 def n @n end |
#u ⇒ Object (readonly)
Returns the value of attribute u.
30 |
# File 'lib/sevgi/sundries/ruler.rb', line 30 attr_reader :n, :u |
Class Method Details
.[](e, n) ⇒ Sevgi::Sundries::Interval
Builds an interval using bracket syntax.
24 |
# File 'lib/sevgi/sundries/ruler.rb', line 24 def self.[](e, n) = new(e, n) |
Instance Method Details
#[](i) ⇒ Float?
Returns a major tick distance by index.
48 |
# File 'lib/sevgi/sundries/ruler.rb', line 48 def [](i) = ds[i] |
#count(length) ⇒ Integer
Counts how many whole lengths fit into this interval.
53 |
# File 'lib/sevgi/sundries/ruler.rb', line 53 def count(length) = (d / length.to_f).to_i |
#d ⇒ Float Also known as: length
Returns the total interval distance.
57 |
# File 'lib/sevgi/sundries/ruler.rb', line 57 def d = @d ||= n * u |
#ds ⇒ Array<Float>
Returns major tick distances, including both endpoints.
61 |
# File 'lib/sevgi/sundries/ruler.rb', line 61 def ds = @ds ||= Array.new(n + 1) { |i| i * u } |
#h ⇒ Float
Returns the midpoint distance.
65 |
# File 'lib/sevgi/sundries/ruler.rb', line 65 def h = @h ||= d / 2.0 |
#hs ⇒ Array<Float>
Returns midpoint tick distances for each interval segment.
69 |
# File 'lib/sevgi/sundries/ruler.rb', line 69 def hs = @hs ||= Array.new(n) { |i| u * (0.5 + i) } |
#nds ⇒ Integer
Returns the last major tick index.
73 |
# File 'lib/sevgi/sundries/ruler.rb', line 73 def nds = @nds ||= ds.size - 1 |
#nhs ⇒ Integer
Returns the last midpoint tick index.
77 |
# File 'lib/sevgi/sundries/ruler.rb', line 77 def nhs = @nhs ||= hs.size - 1 |