Class: Ra::Pattern::Checkers

Inherits:
Base
  • Object
show all
Defined in:
lib/ra/pattern/checkers.rb

Overview

A checkers pattern that alternates colors using:

colors[⌊u * rows⌋ + ⌊v * cols)⌋ % colors.count]

Constant Summary collapse

DEFAULT_ROWS =
2
DEFAULT_COLS =
2
DEFAULT_COLORS =
[
  Color.black,
  Color.white,
].freeze

Instance Method Summary collapse

Constructor Details

#initialize(cols: DEFAULT_COLS, rows: DEFAULT_ROWS, colors: DEFAULT_COLORS) ⇒ Checkers

Returns a new instance of Checkers.

Parameters:

  • rows (Integer) (defaults to: DEFAULT_ROWS)
  • cols (Integer) (defaults to: DEFAULT_COLS)
  • colors (Array<Ra::Color>) (defaults to: DEFAULT_COLORS)


19
20
21
22
23
24
# File 'lib/ra/pattern/checkers.rb', line 19

def initialize(cols: DEFAULT_COLS, rows: DEFAULT_ROWS, colors: DEFAULT_COLORS)
  super()
  @rows = rows
  @cols = cols
  @colors = colors
end

Instance Method Details

#color(point:) ⇒ Ra::Color

Parameters:

  • point (Vector)

    <u = 0.0..1.0, v = 0.0..1.0>

Returns:



28
29
30
31
32
33
34
# File 'lib/ra/pattern/checkers.rb', line 28

def color(point:)
  u = point[0]
  v = point[1]
  index = (u * @rows).floor + (v * @cols).floor

  @colors[index % @colors.count]
end