Module: Hexagonly::Hexagon::Methods
Overview
Adds Hexagon methods to an object.
Defined Under Namespace
Modules: ClassMethods
Instance Attribute Summary collapse
-
#hex_center ⇒ Object
Returns the value of attribute hex_center.
-
#hex_size ⇒ Object
Returns the value of attribute hex_size.
Attributes included from Polygon::Methods
#collected_points, #rejected_points
Class Method Summary collapse
Instance Method Summary collapse
-
#contains?(point) ⇒ true|false
Checks whether the given point lies within the hexagon.
-
#hex_corners ⇒ Array<HexagonTiling::Point>
Returns an array of the 6 points defining the corners of the hexagon.
-
#hex_v_size ⇒ Float
The distance from the center of the hexagon to the top / bottom edges.
-
#loosely_contains?(point) ⇒ true|false
Checks whether the given point lies within the bounding box of the hexagon.
-
#north_east_hexagon(params = {}) ⇒ Object
Returns a hexagon to the north-east with the same radius.
-
#setup_hex(hex_center, hex_size) ⇒ Object
Initializes the hexagon.
-
#south_east_hexagon(params = {}) ⇒ Object
Returns a hexagon to the south-east with the same radius.
-
#south_hexagon(params = {}) ⇒ Object
Returns a hexagon to the south with the same radius.
- #to_geojson ⇒ Object
Methods included from Polygon::Methods
Instance Attribute Details
#hex_center ⇒ Object
Returns the value of attribute hex_center.
91 92 93 |
# File 'lib/hexagonly/hexagon.rb', line 91 def hex_center @hex_center end |
#hex_size ⇒ Object
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.
113 114 115 |
# File 'lib/hexagonly/hexagon.rb', line 113 def contains?(point) loosely_contains?(point) ? super : false end |
#hex_corners ⇒ Array<HexagonTiling::Point>
Returns an array of the 6 points defining the 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_size ⇒ Float
The distance from the center of the hexagon to the top / bottom edges.
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.
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.
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_geojson ⇒ Object
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 |