Module: Waxy::Geometry

Defined in:
lib/waxy/geometry.rb,
lib/waxy/geometry/hex.rb,
lib/waxy/geometry/point.rb,
lib/waxy/geometry/layout.rb,
lib/waxy/geometry/orientation.rb,
lib/waxy/geometry/fractional_hex.rb

Overview

This is an implmentation of www.redblobgames.com/grids/hexagons/implementation.html. Thank you for that wonderful tutorial.

Defined Under Namespace

Classes: FractionalHex, Hex, Layout, Orientation, Point

Constant Summary collapse

SIZE =
10.0
HEX_DIRECTIONS_FLAT =
[
  h.new( 1, 0,  -1),
  h.new( 0, 1, -1),
  h.new( -1, 1, 0),
  h.new(-1, 0,  1),
  h.new(0, -1,  1),
  h.new( 1, -1, 0)
]
HEX_DIRECTIONS_POINTY =
[
  h.new(0, 1, -1),
  h.new(-1, 1, 0),
  h.new(-1, 0, 1),
  h.new(0, -1, 1),
  h.new(1, -1, 0),
  h.new(1, 0, -1)
]

Class Method Summary collapse

Class Method Details

.hexagon(center, size) ⇒ Object



45
46
47
48
49
# File 'lib/waxy/geometry.rb', line 45

def self.hexagon(center, size)
  Hex.new(   
          outer_coords: (1..6).collect{|i| point(center, size, i)} 
         ) 
end

.point(center, size, i) ⇒ Object

TODO(mjy) fix



70
71
72
73
74
# File 'lib/waxy/geometry/hex.rb', line 70

def self.point(center, size, i)
  angle_deg = 60.0 * i
  angle_rad = Math::PI / 180 * angle_deg
  return ((center.x + size) * Math.cos(angle_rad)), ((center.y + size ) * Math.sin(angle_rad))
end

.triangle(center, size, i) ⇒ Object



51
52
53
54
55
56
# File 'lib/waxy/geometry.rb', line 51

def self.triangle(center, size, i)
  points = [[0,0]]
  points.push point(center, size, i)
  points.push point(center, size, (i + 1 == 7 ? 1 : i + 1)) 
  points
end