Class: Sevgi::Sundries::Interval

Inherits:
Object
  • Object
show all
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.

Examples:

Interval geometry

interval = Sevgi::Sundries::Interval[3, 4]
interval.u # => 3.0
interval.n # => 4
interval.d # => 12.0

Query major and halfway distances

interval = Sevgi::Sundries::Interval[3, 4]
interval.ds # => [0.0, 3.0, 6.0, 9.0, 12.0]
interval.hs # => [1.5, 4.5, 7.5, 10.5]

Direct Known Subclasses

Ruler

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(e, n) ⇒ void

Creates an interval.

Parameters:

  • e (Numeric, #length)

    unit length or an object exposing length

  • n (Integer)

    non-negative interval count

Raises:

  • (Sevgi::ArgumentError)

    when count is not a non-negative integer

  • (Sevgi::ArgumentError)

    when the unit object does not expose length

  • (Sevgi::ArgumentError)

    when the measured unit length is not numeric

  • (Sevgi::ArgumentError)

    when the measured unit length is not finite

  • (Sevgi::ArgumentError)

    when the measured unit length is not positive



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

#nInteger (readonly)

Returns the interval count.

Returns:

  • (Integer)


36
37
38
# File 'lib/sevgi/sundries/ruler.rb', line 36

def n
  @n
end

#uFloat (readonly)

Returns the unit length.

Returns:

  • (Float)


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.

Parameters:

  • e (Numeric, #length)

    unit length or an object exposing length

  • n (Integer)

    non-negative interval count

Returns:

Raises:

  • (Sevgi::ArgumentError)

    when count is not a non-negative integer

  • (Sevgi::ArgumentError)

    when the unit object does not expose length

  • (Sevgi::ArgumentError)

    when the measured unit length is not numeric

  • (Sevgi::ArgumentError)

    when the measured unit length is not finite

  • (Sevgi::ArgumentError)

    when the measured unit length is not positive



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.

Parameters:

  • i (Integer)

    tick index

Returns:

  • (Float, nil)

    distance from the interval origin, or nil when out of range



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.

Parameters:

  • length (Numeric)

    candidate length

Returns:

  • (Integer)

Raises:

  • (Sevgi::ArgumentError)

    when length is not numeric

  • (Sevgi::ArgumentError)

    when length is not finite

  • (Sevgi::ArgumentError)

    when length is not positive



67
# File 'lib/sevgi/sundries/ruler.rb', line 67

def count(length) = (d / positive_number(length, "Interval count length")).to_i

#dFloat Also known as: length

Returns the total interval distance.

Returns:

  • (Float)


71
# File 'lib/sevgi/sundries/ruler.rb', line 71

def d = @d ||= n * u

#dsArray<Float>

Returns major tick distances, including both endpoints. The memoized collection is frozen and must be treated as immutable.

Returns:

  • (Array<Float>)

    frozen major tick distances



76
# File 'lib/sevgi/sundries/ruler.rb', line 76

def ds = @ds ||= Array.new(n + 1) { |i| i * u }.freeze

#hFloat

Returns the midpoint distance.

Returns:

  • (Float)


80
# File 'lib/sevgi/sundries/ruler.rb', line 80

def h = @h ||= d / 2.0

#hsArray<Float>

Returns midpoint tick distances for each interval segment. The memoized collection is frozen and must be treated as immutable.

Returns:

  • (Array<Float>)

    frozen midpoint tick distances



85
# File 'lib/sevgi/sundries/ruler.rb', line 85

def hs = @hs ||= Array.new(n) { |i| u * (0.5 + i) }.freeze

#ndsInteger

Returns the last major tick index.

Returns:

  • (Integer)


89
# File 'lib/sevgi/sundries/ruler.rb', line 89

def nds = @nds ||= ds.size - 1

#nhsInteger

Returns the last midpoint tick index.

Returns:

  • (Integer)


93
# File 'lib/sevgi/sundries/ruler.rb', line 93

def nhs = @nhs ||= hs.size - 1