Class: Knot::Cell

Inherits:
Object
  • Object
show all
Defined in:
lib/knot/cell.rb

Overview

Each cell within the grid contains several Cartesian points stored in hashes. These points are pre-calculated so that minimal calculations are required when pathing.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(c, inner, stroke) ⇒ Cell

Returns a new instance of Cell.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/knot/cell.rb', line 10

def initialize(c, inner, stroke)
  outer = inner + stroke

  @c = c

  pos = {
    n: [0, -1], ne: [0.5, -0.5], e: [1, 0], se: [0.5, 0.5],
    s: [0, 1], sw: [-0.5, 0.5], w: [-1, 0], nw: [-0.5, -0.5]
  }

  @i = pos.each_with_object({}) do |(k, v), a|
    a[k] = Point[c.x + v[0] * inner,
                 c.y + v[1] * inner]
  end

  @o = pos.each_with_object({}) do |(k, v), a|
    a[k] = Point[c.x + v[0] * outer,
                 c.y + v[1] * outer]
  end

  subpos = [
    [:n, :sw], [:n, :se],
    [:e, :sw], [:e, :nw],
    [:s, :nw], [:s, :ne],
    [:w, :ne], [:w, :se],
  ]
  subpos.each do |v|
    sym = (v[0].to_s << '_' << v[1].to_s).to_sym
    @o[sym] = Point[@o[v[0]].x + stroke * pos[v[1]][0],
                    @o[v[0]].y + stroke * pos[v[1]][1]]
  end
end

Instance Attribute Details

#cObject (readonly)

Returns the value of attribute c.



8
9
10
# File 'lib/knot/cell.rb', line 8

def c
  @c
end

#iObject (readonly)

Returns the value of attribute i.



8
9
10
# File 'lib/knot/cell.rb', line 8

def i
  @i
end

#oObject (readonly)

Returns the value of attribute o.



8
9
10
# File 'lib/knot/cell.rb', line 8

def o
  @o
end