Class: Sevgi::Sundries::Grid

Inherits:
Tile
  • Object
show all
Defined in:
lib/sevgi/sundries/grid.rb

Overview

Builds a tile-like grid from horizontal and vertical rulers.

Axis names describe line direction, not the coordinate used to place a line: grid.x produces horizontal lines whose y positions come from the vertical ruler; grid.y produces vertical lines whose x positions come from the horizontal ruler. Each query can return geometry lines, Point endpoint pairs, or plain coordinate pairs for different consumers.

Examples:

Query fitted horizontal and vertical lines

x = Sevgi::Sundries::Ruler.new(brut: 80, unit: 1, multiple: 10, margins: [5])
y = Sevgi::Sundries::Ruler.new(brut: 50, unit: 1, multiple: 10, margins: [5])
grid = Sevgi::Sundries::Grid[x, y]
grid.x.major.lines.size # => 5
grid.y.minor.lines.size # => 71

Preserve a source canvas while fitting axis margins

canvas = Sevgi::Graphics::Canvas.call(width: 80, height: 50)
x = Sevgi::Sundries::Ruler.new(brut: 80, unit: 1, multiple: 10, margins: [5, 7])
y = Sevgi::Sundries::Ruler.new(brut: 50, unit: 1, multiple: 10, margins: [3, 5])
grid = Sevgi::Sundries::Grid.new(x:, y:, canvas:)
grid.canvas.margin.to_a # => [4.0, 11.0, 6.0, 9.0]

Use lines, points, coordinate pairs, and inherited row boxes

x = Sevgi::Sundries::Ruler.new(brut: 30, unit: 1, multiple: 10)
y = Sevgi::Sundries::Ruler.new(brut: 20, unit: 1, multiple: 10)
grid = Sevgi::Sundries::Grid[x, y]
grid.x.major.lines.size  # => 3
grid.x.major.points.first.map(&:deconstruct) # => [[0.0, 0.0], [30.0, 0.0]]
grid.y.halve.xys.first   # => [[5.0, 0.0], [5.0, 20.0]]
grid.rowbox.approx.height # => 10.0

See Also:

  • Ruler
  • Graphics::Mixtures::Hatch

Defined Under Namespace

Classes: Axis, X, Y

Instance Attribute Summary collapse

Attributes inherited from Tile

#element, #nx, #ny, #position

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Tile

#[], #box, #cell, #col, #colbox, #cols, #each, #each_col, #row, #rowbox, #rows

Constructor Details

#initialize(x:, y:, canvas: nil) ⇒ void

Creates a grid from horizontal and vertical rulers.

Parameters:

Raises:

  • (Sevgi::ArgumentError)

    when either argument is not a ruler

  • (Sevgi::ArgumentError)

    when the source canvas does not match the ruler spans

  • (Sevgi::ArgumentError)

    when either ruler fits no interval



62
63
64
65
66
67
68
69
70
71
# File 'lib/sevgi/sundries/grid.rb', line 62

def initialize(x:, y:, canvas: nil)
  validate_axes(x, y)
  validate_canvas(canvas, x, y)

  @source = canvas
  @x = X.send(:new, x, y)
  @y = Y.send(:new, x, y)

  super(Geometry::Rect[@x.u, @y.u], nx: @x.n, ny: @y.n)
end

Instance Attribute Details

#xSevgi::Sundries::Grid::X (readonly)

Returns the horizontal-line axis and its ruler queries. Line positions are supplied by the vertical ruler.



47
48
49
# File 'lib/sevgi/sundries/grid.rb', line 47

def x
  @x
end

#ySevgi::Sundries::Grid::Y (readonly)

Returns the vertical-line axis and its ruler queries. Line positions are supplied by the horizontal ruler.



52
53
54
# File 'lib/sevgi/sundries/grid.rb', line 52

def y
  @y
end

Class Method Details

.[](x, y) ⇒ Sevgi::Sundries::Grid

Builds a grid using bracket syntax.

Parameters:

Returns:

Raises:

  • (Sevgi::ArgumentError)

    when either argument is not a ruler



42
# File 'lib/sevgi/sundries/grid.rb', line 42

def self.[](x, y) = new(x:, y:)

Instance Method Details

#canvasSevgi::Graphics::Canvas

Returns a graphics canvas matching the ruler spans and fitted margins. Horizontal ruler margins become the canvas left/right margins; vertical ruler margins become its top/bottom margins.

Examples:

Build a drawing with the fitted canvas

x = Sevgi::Sundries::Ruler.new(brut: 80, unit: 1, multiple: 10, margins: [5])
y = Sevgi::Sundries::Ruler.new(brut: 50, unit: 1, multiple: 10, margins: [5])
grid = Sevgi::Sundries::Grid[x, y]
drawing = Sevgi::Graphics.SVG(:inkscape, grid.canvas) do
  Draw grid.x.major.lines, class: %w[guide horizontal]
  Draw grid.y.major.lines, class: %w[guide vertical]
end
drawing.Render

Returns:

  • (Sevgi::Graphics::Canvas)


86
87
88
89
90
91
92
93
94
# File 'lib/sevgi/sundries/grid.rb', line 86

def canvas
  margins = [y.start, x.finish, y.finish, x.start]
  return @source.with(margins:) if @source

  Graphics::Canvas.new(
    **Graphics::Paper[x.brut, y.brut].to_h,
    margins:
  )
end

#heightFloat

Returns the fitted grid height.

Returns:

  • (Float)


98
# File 'lib/sevgi/sundries/grid.rb', line 98

def height = y.d

#widthFloat

Returns the fitted grid width.

Returns:

  • (Float)


102
# File 'lib/sevgi/sundries/grid.rb', line 102

def width = x.d