Class: Geometry::RegularPolygon
- Includes:
- ClusterFactory
- Defined in:
- lib/geometry/regular_polygon.rb
Overview
A RegularPolygon is a lot like a Polygon, but more regular.
http://en.wikipedia.org/wiki/Regular_polygon
Usage
polygon = Geometry::RegularPolygon.new sides:4, center:[1,2], radius:3
polygon = Geometry::RegularPolygon.new sides:6, center:[1,2], diameter:6
Direct Known Subclasses
Accessors collapse
- #diameter ⇒ Object readonly
Instance Attribute Summary collapse
-
#center ⇒ Point
readonly
The RegularPolygon‘s center point.
-
#edge_count ⇒ Number
readonly
The RegularPolygon‘s number of sides.
-
#radius ⇒ Number
readonly
The RegularPolygon‘s radius.
Attributes inherited from Polyline
Class Method Summary collapse
Instance Method Summary collapse
- #eql?(other) ⇒ Boolean (also: #==)
-
#initialize(edge_count, center, radius) ⇒ RegularPolygon
constructor
Construct a new RegularPolygon from a centerpoint and radius.
Methods included from ClusterFactory
Methods inherited from Polygon
#<=>, #clockwise?, #convex, #offset_bisectors, #outset, #reverse, #union, #wrap
Methods inherited from Polyline
Constructor Details
#initialize(edge_count, center, radius) ⇒ RegularPolygon
Construct a new Geometry::RegularPolygon from a centerpoint and radius
57 58 59 60 61 |
# File 'lib/geometry/regular_polygon.rb', line 57 def initialize(edge_count, center, radius) @center = Point[center] @edge_count = edge_count @radius = radius end |
Instance Attribute Details
#center ⇒ Point (readonly)
Returns The Geometry::RegularPolygon‘s center point.
19 20 21 |
# File 'lib/geometry/regular_polygon.rb', line 19 def center @center end |
#diameter ⇒ Object (readonly)
71 72 73 |
# File 'lib/geometry/regular_polygon.rb', line 71 def diameter @radius*2 end |
#edge_count ⇒ Number (readonly)
Returns The Geometry::RegularPolygon‘s number of sides.
22 23 24 |
# File 'lib/geometry/regular_polygon.rb', line 22 def edge_count @edge_count end |
#radius ⇒ Number (readonly)
Returns The Geometry::RegularPolygon‘s radius.
25 26 27 |
# File 'lib/geometry/regular_polygon.rb', line 25 def radius @radius end |
Class Method Details
.new(sides, center, radius) ⇒ Object .new(sides, center, diameter) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/geometry/regular_polygon.rb', line 37 def self.new(={}, &block) raise ArgumentError, "RegularPolygon requires an edge count" unless [:sides] center = [:center] center = center ? Point[center] : nil if .has_key?(:radius) self.allocate.tap {|polygon| polygon.send :initialize, [:sides], center, [:radius], &block } elsif .has_key?(:diameter) DiameterRegularPolygon.new [:sides], center, [:diameter], &block else raise ArgumentError, "RegularPolygon.new requires a radius or a diameter" end end |
Instance Method Details
#eql?(other) ⇒ Boolean Also known as: ==
63 64 65 |
# File 'lib/geometry/regular_polygon.rb', line 63 def eql?(other) (self.center == other.center) && (self.edge_count == other.edge_count) && (self.radius == other.radius) end |