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, d is total distance, and h is the midpoint distance.

Examples:

Interval geometry

# <---------------- d = n x u ---------------->
# |---------+---------+---------+---------|
#           <--- u --->
interval = Sevgi::Sundries::Interval[3, 4]
interval.d # => 12.0

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 length cannot be measured



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

#nInteger (readonly)

Returns interval count.

Returns:

  • (Integer)

    interval count



30
31
32
# File 'lib/sevgi/sundries/ruler.rb', line 30

def n
  @n
end

#uObject (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.

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 length cannot be measured



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.

Parameters:

  • i (Integer)

    tick index

Returns:

  • (Float, nil)

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



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.

Parameters:

  • length (Numeric)

    candidate length

Returns:

  • (Integer)


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

def count(length) = (d / length.to_f).to_i

#dFloat Also known as: length

Returns the total interval distance.

Returns:

  • (Float)


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

def d = @d ||= n * u

#dsArray<Float>

Returns major tick distances, including both endpoints.

Returns:

  • (Array<Float>)


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

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

#hFloat

Returns the midpoint distance.

Returns:

  • (Float)


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

def h = @h ||= d / 2.0

#hsArray<Float>

Returns midpoint tick distances for each interval segment.

Returns:

  • (Array<Float>)


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

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

#ndsInteger

Returns the last major tick index.

Returns:

  • (Integer)


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

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

#nhsInteger

Returns the last midpoint tick index.

Returns:

  • (Integer)


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

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