Class: Geometry::RegularPolygon
- Includes:
- ClusterFactory
- Defined in:
- lib/aurora-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.
Accessors collapse
-
#bounds ⇒ Rectangle
The smallest axis-aligned Rectangle that bounds the receiver.
-
#edges ⇒ Object
!@attribute [r] edges.
-
#max ⇒ Point
The upper right corner of the bounding Rectangle.
-
#min ⇒ Point
The lower left corner of the bounding Rectangle.
-
#minmax ⇒ Array<Point>
The lower left and upper right corners of the bounding Rectangle.
-
#vertices ⇒ Object
!@attribute [r] vertices @return [Array].
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?, #close, #convex, #outset, #outset_bisectors, #reverse, #reverse!, #spokes, #union, #wrap
Methods inherited from Polyline
#bisectors, #close, #close!, #closed?, #left_bisectors, #offset, #reverse, #reverse!, #right_bisectors, #rightset, #spokes
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/aurora-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/aurora-geometry/regular_polygon.rb', line 19 def center @center end |
#diameter ⇒ Object (readonly)
76 77 78 |
# File 'lib/aurora-geometry/regular_polygon.rb', line 76 def diameter @radius*2 end |
#edge_count ⇒ Number (readonly)
Returns The Geometry::RegularPolygon‘s number of sides.
22 23 24 |
# File 'lib/aurora-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/aurora-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/aurora-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] : Point.zero 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
#bounds ⇒ Rectangle
Returns The smallest axis-aligned Geometry::Rectangle that bounds the receiver.
70 71 72 |
# File 'lib/aurora-geometry/regular_polygon.rb', line 70 def bounds return Rectangle.new(self.min, self.max) end |
#edges ⇒ Object
!@attribute [r] edges
81 82 83 84 |
# File 'lib/aurora-geometry/regular_polygon.rb', line 81 def edges points = self.vertices points.each_cons(2).map {|p1,p2| Edge.new(p1,p2) } + [Edge.new(points.last, points.first)] end |
#eql?(other) ⇒ Boolean Also known as: ==
63 64 65 |
# File 'lib/aurora-geometry/regular_polygon.rb', line 63 def eql?(other) (self.center == other.center) && (self.edge_count == other.edge_count) && (self.radius == other.radius) end |
#max ⇒ Point
Returns The upper right corner of the bounding Geometry::Rectangle.
93 94 95 |
# File 'lib/aurora-geometry/regular_polygon.rb', line 93 def max @center+Point[radius, radius] end |
#min ⇒ Point
Returns The lower left corner of the bounding Geometry::Rectangle.
98 99 100 |
# File 'lib/aurora-geometry/regular_polygon.rb', line 98 def min @center-Point[radius, radius] end |
#minmax ⇒ Array<Point>
Returns The lower left and upper right corners of the bounding Geometry::Rectangle.
103 104 105 |
# File 'lib/aurora-geometry/regular_polygon.rb', line 103 def minmax [self.min, self.max] end |
#vertices ⇒ Object
!@attribute [r] vertices
@return [Array]
88 89 90 |
# File 'lib/aurora-geometry/regular_polygon.rb', line 88 def vertices (0...2*Math::PI).step(2*Math::PI/edge_count).map {|angle| center + Point[Math::cos(angle), Math::sin(angle)]*radius } end |