module Hexagonly::Hexagon::Methods
Adds Hexagon
methods to an object.
@example
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
Attributes
Public Class Methods
# 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
Public Instance Methods
Checks whether the given point lies within the hexagon.
@return [true|false]
Hexagonly::Polygon::Methods#contains?
# File lib/hexagonly/hexagon.rb, line 113 def contains?(point) loosely_contains?(point) ? super : false end
Returns an array of the 6 points defining the corners of the hexagon.
@return [Array<HexagonTiling::Point>] an array of points, coresponding to
the 6 corners of the hexagon
# 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
The distance from the center of the hexagon to the top / bottom edges.
@return [Float]
# 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
Checks whether the given point lies within the bounding box of the hexagon.
@return [true|false]
# 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
Returns a hexagon to the north-east with the same radius.
# 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
Initializes the hexagon.
@param hex_center
[Hexagonly::Point] the center of the hexagon @param hex_size
[Float] the size of the hexagon (horizontal width / 2)
# File lib/hexagonly/hexagon.rb, line 97 def setup_hex(hex_center, hex_size) @hex_center, @hex_size = hex_center, hex_size end
Returns a hexagon to the south-east with the same radius.
# 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
Returns a hexagon to the south with the same radius.
# 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
Private Instance Methods
# File lib/hexagonly/hexagon.rb, line 185 def hex_point_class @hex_center.nil? ? Hexagonly::Point : @hex_center.class end