Class: Waxy::Geometry::Layout

Inherits:
Object
  • Object
show all
Defined in:
lib/waxy/geometry/layout.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(_orientation, _size, _origin, _padding = 0.0) ⇒ Layout

Returns a new instance of Layout.



22
23
24
25
26
27
28
# File 'lib/waxy/geometry/layout.rb', line 22

def initialize(_orientation, _size, _origin, _padding = 0.0) 
  @orientation = _orientation
  @size = _size
  @origin = _origin 
  @padding = _padding
  reset_padding
end

Instance Attribute Details

#orientationObject

Waxy::Geometry::Orientation



7
8
9
# File 'lib/waxy/geometry/layout.rb', line 7

def orientation
  @orientation
end

#originObject

Waxy::Geometry::Point



13
14
15
# File 'lib/waxy/geometry/layout.rb', line 13

def origin
  @origin
end

#pad_h_offsetObject (readonly)

Returns the value of attribute pad_h_offset.



20
21
22
# File 'lib/waxy/geometry/layout.rb', line 20

def pad_h_offset
  @pad_h_offset
end

#pad_w_offsetObject (readonly)

Returns the value of attribute pad_w_offset.



20
21
22
# File 'lib/waxy/geometry/layout.rb', line 20

def pad_w_offset
  @pad_w_offset
end

#paddingObject

pixels between hexes !! Padding is applied at render time, not geometry compute time, i.e. using this will currently !! break things like pixel_to_hex



18
19
20
# File 'lib/waxy/geometry/layout.rb', line 18

def padding
  @padding
end

#sizeObject

Waxy::Geometry::Point



10
11
12
# File 'lib/waxy/geometry/layout.rb', line 10

def size
  @size
end

Instance Method Details

#hex_corner_angle(corner) ⇒ Object

Parameters:

  • corner (Integer)


56
57
58
# File 'lib/waxy/geometry/layout.rb', line 56

def hex_corner_angle(corner)
  2.0 * Math::PI * (orientation.start_angle + corner) / 6
end

#hex_corner_offset(corner, s = nil) ⇒ Object

Parameters:

  • corner (Integer)


49
50
51
52
53
# File 'lib/waxy/geometry/layout.rb', line 49

def hex_corner_offset(corner, s = nil)
  s ||= size
  angle = hex_corner_angle(corner)
  Waxy::Geometry::Point.new(s.x * Math.cos(angle), s.y * Math.sin(angle))
end

#hex_to_pixel(hex) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/waxy/geometry/layout.rb', line 30

def hex_to_pixel(hex)
  m = orientation
  x = (m.f0 * hex.q + m.f1 * hex.r) * size.x
  y = (m.f2 * hex.q + m.f3 * hex.r) * size.y

  Waxy::Geometry::Point.new(x + origin.x,  y + origin.y)
end

#increase_h_paddingObject



118
119
120
# File 'lib/waxy/geometry/layout.rb', line 118

def increase_h_padding
  @pad_h_offset += padding
end

#increase_w_paddingObject



122
123
124
# File 'lib/waxy/geometry/layout.rb', line 122

def increase_w_padding
  @pad_w_offset += padding
end

#pixel_to_hex(p) ⇒ Object



38
39
40
41
42
43
44
45
46
# File 'lib/waxy/geometry/layout.rb', line 38

def pixel_to_hex(p)
  m = orientation
  pt = Waxy::Geometry::Point.new((p.x - origin.x) / size.x,
                                 (p.y - origin.y) / size.y)
  q = m.b0 * pt.x + m.b1 * pt.y
  r = m.b2 * pt.x + m.b3 * pt.y

  Waxy::Geometry::FractionalHex.new(q, r, q * -1.0,  r * -1.0)
end

#point(hex, i, scale = 1.0) ⇒ Object

Departing from Redblob here



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/waxy/geometry/layout.rb', line 76

def point(hex, i, scale = 1.0 )
  center = hex_to_pixel(hex)

  resized = size.dup
  resized.x = resized.x * scale
  resized.y = resized.y * scale 

  offset = hex_corner_offset(i, resized)

  Waxy::Geometry::Point.new( center.x + offset.x, center.y + offset.y  ) 
end

#polygon_corners(h) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/waxy/geometry/layout.rb', line 61

def polygon_corners(h)
  corners = []
  center = hex_to_pixel(h)
  (0..5).each do |i|
    offset = hex_corner_offset(i) 

    corners.push Waxy::Geometry::Point.new(
      center.x + offset.x,
      center.y + offset.y)
  end
  corners
end

#reset_h_paddingObject



110
111
112
# File 'lib/waxy/geometry/layout.rb', line 110

def reset_h_padding
  @pad_h_offset = 0.0
end

#reset_paddingObject

Padding methods are not part of Amit’s original code. They are not integrated into geometry compute.



105
106
107
108
# File 'lib/waxy/geometry/layout.rb', line 105

def reset_padding
  reset_h_padding
  reset_w_padding
end

#reset_w_paddingObject



114
115
116
# File 'lib/waxy/geometry/layout.rb', line 114

def reset_w_padding
  @pad_w_offset = 0.0
end

#triangle(hex, i) ⇒ Object



88
89
90
91
92
93
94
95
# File 'lib/waxy/geometry/layout.rb', line 88

def triangle(hex, i)
  scale = hex.size[i]

  points = [ hex_to_pixel(hex) ] 
  points.push point(hex, i, scale)
  points.push point(hex, (i + 1 == 6 ? 0 : i + 1), scale) 
  points
end

#triangles(hex) ⇒ Object



97
98
99
# File 'lib/waxy/geometry/layout.rb', line 97

def triangles(hex)
  (0..5).each_with_index.collect{|t,i| triangle(hex, i) }
end