Class: Sumi::Sundries::Interval

Inherits:
Object
  • Object
show all
Defined in:
sundries/lib/sumi/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 = Sumi::Sundries::Interval[3, 4]
interval.u # => 3.0
interval.n # => 4
interval.d # => 12.0

Query major and halfway distances

interval = Sumi::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:



51
52
53
54
# File 'sundries/lib/sumi/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.

Returns:

  • (Integer)


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

def n
  @n
end

#u ⇒ Float (readonly)

Returns the unit length.

Returns:

  • (Float)


40
41
42
# File 'sundries/lib/sumi/sundries/ruler.rb', line 40

def u
  @u
end

Class Method Details

.[](e, n) ⇒ Sumi::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:



32
# File 'sundries/lib/sumi/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 'sundries/lib/sumi/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:



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

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

#d ⇒ Float Also known as: length

Returns the total interval distance.

Returns:

  • (Float)


71
# File 'sundries/lib/sumi/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.

Returns:

  • (Array<Float>) —

    frozen major tick distances



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

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

#h ⇒ Float

Returns the midpoint distance.

Returns:

  • (Float)


80
# File 'sundries/lib/sumi/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.

Returns:

  • (Array<Float>) —

    frozen midpoint tick distances



85
# File 'sundries/lib/sumi/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.

Returns:

  • (Integer)


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

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

#nhs ⇒ Integer

Returns the last midpoint tick index.

Returns:

  • (Integer)


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

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