Module: Hexagonly::Hexagon::Methods

Includes:
Polygon::Methods
Included in:
Hexagonly::Hexagon
Defined in:
lib/hexagonly/hexagon.rb

Overview

Adds Hexagon methods to an object.

Examples:

class MyHexagon

  include Hexagonly::Hexagon::Methods

  def initialize(center, size)
    setup_hex(center, size)
  end

end

hex = MyHexagon.new(Hexagonly::Point.new(1, 2), 0.5)
hex.hex_corners # => an array containing all 6 corners of the hexagon
hex.contains?(Hexagonly::Point.new(1, 2)) # => true
hex.contains?(Hexagonly::Point.new(3, 3)) # => false

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Attributes included from Polygon::Methods

#collected_points, #rejected_points

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Polygon::Methods

#grab, #poly_points

Instance Attribute Details

#hex_centerObject

Returns the value of attribute hex_center.



91
92
93
# File 'lib/hexagonly/hexagon.rb', line 91

def hex_center
  @hex_center
end

#hex_sizeObject

Returns the value of attribute hex_size.



91
92
93
# File 'lib/hexagonly/hexagon.rb', line 91

def hex_size
  @hex_size
end

Class Method Details

.included(base) ⇒ Object



25
26
27
28
29
30
# File 'lib/hexagonly/hexagon.rb', line 25

def self.included(base)
  base.extend(Hexagonly::Polygon::ClassMethods)
  base.poly_points_method :hex_corners

  base.extend(ClassMethods)
end

Instance Method Details

#contains?(point) ⇒ true|false

Checks whether the given point lies within the hexagon.

Returns:

  • (true|false)


113
114
115
# File 'lib/hexagonly/hexagon.rb', line 113

def contains?(point)
  loosely_contains?(point) ? super : false
end

#hex_cornersArray<HexagonTiling::Point>

Returns an array of the 6 points defining the corners of the hexagon.

Returns:

  • (Array<HexagonTiling::Point>)

    an array of points, coresponding to the 6 corners of the hexagon



169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/hexagonly/hexagon.rb', line 169

def hex_corners
  raise "Call #setup_hex first!" if @hex_center.nil? || @hex_size.nil?

  corners = []
  (0..5).each do |i|
    angle = 2 * Math::PI / 6 * i
    corner_x = @hex_center.x_coord + @hex_size * Math.cos(angle)
    corner_y = @hex_center.y_coord + @hex_size * Math.sin(angle)
    corners << hex_point_class.new(corner_x, corner_y)
  end

  corners
end

#hex_v_sizeFloat

The distance from the center of the hexagon to the top / bottom edges.

Returns:

  • (Float)


104
105
106
107
108
# File 'lib/hexagonly/hexagon.rb', line 104

def hex_v_size
  raise "hex_size not defined!" if @hex_size.nil?

  @hex_size * Math.cos(Math::PI / 6)
end

#loosely_contains?(point) ⇒ true|false

Checks whether the given point lies within the bounding box of the hexagon.

Returns:

  • (true|false)


120
121
122
123
124
# File 'lib/hexagonly/hexagon.rb', line 120

def loosely_contains?(point)
  raise "Call #setup_hex first!" if @hex_center.nil? || @hex_size.nil?

  ((point.x_coord - @hex_center.x_coord).abs <= @hex_size) && ((point.y_coord - @hex_center.y_coord).abs <= hex_v_size)
end

#north_east_hexagon(params = {}) ⇒ Object

Returns a hexagon to the north-east with the same radius.



127
128
129
130
131
132
133
134
135
136
137
# File 'lib/hexagonly/hexagon.rb', line 127

def north_east_hexagon(params = {})
  raise "Call #setup_hex first!" if @hex_center.nil? || @hex_size.nil?

  grab_points = params.fetch(:grab_points, false)

  north_east_center = hex_point_class.new(@hex_center.x_coord + @hex_size * 1.5, @hex_center.y_coord + hex_v_size)
  self.class.new.tap do |hex| 
    hex.setup_hex(north_east_center, @hex_size)
    hex.grab(@rejected_points) if grab_points
  end
end

#setup_hex(hex_center, hex_size) ⇒ Object

Initializes the hexagon.

Parameters:

  • hex_center (Hexagonly::Point)

    the center of the hexagon

  • hex_size (Float)

    the size of the hexagon (horizontal width / 2)



97
98
99
# File 'lib/hexagonly/hexagon.rb', line 97

def setup_hex(hex_center, hex_size)
  @hex_center, @hex_size = hex_center, hex_size
end

#south_east_hexagon(params = {}) ⇒ Object

Returns a hexagon to the south-east with the same radius.



140
141
142
143
144
145
146
147
148
149
150
# File 'lib/hexagonly/hexagon.rb', line 140

def south_east_hexagon(params = {})
  raise "Call #setup_hex first!" if @hex_center.nil? || @hex_size.nil?

  grab_points = params.fetch(:grab_points, false)

  south_east_center = hex_point_class.new(@hex_center.x_coord + @hex_size * 1.5, @hex_center.y_coord - hex_v_size)
  self.class.new.tap do |hex| 
    hex.setup_hex(south_east_center, @hex_size)
    hex.grab(@rejected_points) if grab_points
  end
end

#south_hexagon(params = {}) ⇒ Object

Returns a hexagon to the south with the same radius.



153
154
155
156
157
158
159
160
161
162
163
# File 'lib/hexagonly/hexagon.rb', line 153

def south_hexagon(params = {})
  raise "Call #setup_hex first!" if @hex_center.nil? || @hex_size.nil?

  grab_points = params.fetch(:grab_points, false)

  south_center = hex_point_class.new(@hex_center.x_coord, @hex_center.y_coord - hex_v_size * 2.0)
  self.class.new.tap do |hex| 
    hex.setup_hex(south_center, @hex_size)
    hex.grab(@rejected_points) if grab_points
  end
end

#to_geojsonObject



183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/hexagonly/hexagon.rb', line 183

def to_geojson
  corner_points = hex_corners.map{ |p| [p.x_coord, p.y_coord] }
  corner_points << corner_points.last
  {
    :type => "Feature",
    :geometry => {
      :type => "Polygon",
      :coordinates => [corner_points]
    },
    :properties => nil
  }
end