Class: Sevgi::Sundries::Ruler

Inherits:
Interval
  • Object
show all
Defined in:
lib/sevgi/sundries/ruler.rb

Overview

Fits a repeated interval into a broader span with computed margins.

A ruler stores both the fitted major interval and the source subinterval. The compact reader names mirror Interval: brut is the full available span, sd/su/sn describe the subinterval, and waste is distributed as equal margins.

Examples:

Ruler geometry

# <--------- d = n x sd ---------><--- waste = 2 x margin --->
# <----- u = unit x multiple ----->
# <---------------- brut ---------------->
ruler = Sevgi::Sundries::Ruler.new(unit: 1, multiple: 10, brut: 150)
ruler.d # => 150.0

Direct Known Subclasses

Grid::Axis, RulerEven

Instance Attribute Summary collapse

Attributes inherited from Interval

#n, #u

Instance Method Summary collapse

Methods inherited from Interval

[], #[], #count, #d, #ds, #h, #hs, #nds, #nhs

Constructor Details

#initialize(brut:, unit:, multiple:, margin: 0.0) ⇒ void

Creates a ruler fitted into the given span.

Parameters:

  • brut (Numeric)

    full available span

  • unit (Numeric)

    subinterval unit length

  • multiple (Integer)

    number of subinterval units per major interval

  • margin (Numeric) (defaults to: 0.0)

    minimum margin on each side

Raises:

  • (Sevgi::ArgumentError)

    when unit or multiple is not positive

  • (Sevgi::ArgumentError)

    when margin is negative

  • (Sevgi::ArgumentError)

    when the fitting span is negative



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/sevgi/sundries/ruler.rb', line 122

def initialize(brut:, unit:, multiple:, margin: 0.0)
  ArgumentError.("Ruler unit must be positive") unless unit.to_f.positive?
  ArgumentError.("Ruler multiple must be positive") unless multiple.to_f.positive?
  ArgumentError.("Ruler margin must be non-negative") if margin.to_f.negative?

  @brut = brut.to_f
  margin = margin.to_f
  @sub = Interval.new(unit, multiple)

  n = divide(unit:, multiple:, brut: @brut, margin:)

  ArgumentError.("Ruler fitting span must not be negative") if n.negative?

  super(@sub, n)
end

Instance Attribute Details

#brutFloat (readonly)

Returns full available span before fitting.

Returns:

  • (Float)

    full available span before fitting



111
112
113
# File 'lib/sevgi/sundries/ruler.rb', line 111

def brut
  @brut
end

#subObject (readonly)

Returns the value of attribute sub.



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

attr_reader :brut, :sub

Instance Method Details

#expandSevgi::Sundries::Ruler

Returns a ruler where the source subinterval is flattened into units.



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

def expand = self.class.new(unit: sub.u, multiple: 1, brut: d + waste, margin:)

#marginFloat

Returns the computed margin after fitting.

Returns:

  • (Float)


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

def margin = @margin ||= waste / 2.0

#msArray<Float>

Returns minor tick distances across the fitted span.

Returns:

  • (Array<Float>)


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

def ms = @ms ||= expand.ds

#sdFloat

Returns the source subinterval distance.

Returns:

  • (Float)


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

def sd = @sub.d

#snInteger

Returns the source subinterval count.

Returns:

  • (Integer)


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

def sn = @sub.n

#suFloat

Returns the source subinterval unit length.

Returns:

  • (Float)


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

def su = @sub.u

#wasteFloat

Returns the unfitted distance distributed outside the fitted span.

Returns:

  • (Float)


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

def waste = @waste ||= brut - d