Class: Geometry::RegularPolygon
- 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
polygon = Geometry::RegularPolygon.new sides:4, center:[1,2], inradius:3
polygon = Geometry::RegularPolygon.new sides:6, center:[1,2], indiameter:6
Accessors collapse
- #diameter ⇒ Object (also: #circumdiameter) readonly
- #indiameter ⇒ Object
- #inradius ⇒ Object (also: #apothem)
-
#radius ⇒ Number
(also: #circumradius)
readonly
The RegularPolygon‘s radius.
- #side_length ⇒ Object readonly
Instance Attribute Summary collapse
-
#center ⇒ Point
readonly
The RegularPolygon‘s center point.
-
#edge_count ⇒ Number
readonly
The RegularPolygon‘s number of sides.
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
(also: #points)
!@attribute [r] vertices @return [Array].
Instance Method Summary collapse
-
#closed? ⇒ True
Check to see if the Polygon is closed (always true).
- #eql?(other) ⇒ Boolean (also: #==)
-
#initialize(edge_count: nil, sides: nil, center: nil, radius: nil, diameter: nil, indiameter: nil, inradius: nil) ⇒ RegularPolygon
constructor
A new RegularPolygon object.
Methods inherited from Polygon
#<=>, #clockwise?, #close, #convex, #outset, #outset_bisectors, #reverse, #reverse!, #spokes, #union, #wrap
Methods inherited from Polyline
#bisectors, #close, #close!, #left_bisectors, #offset, #reverse, #reverse!, #right_bisectors, #rightset, #spokes
Constructor Details
#new(sides, center, radius) ⇒ RegularPolygon #new(sides, center, inradius) ⇒ RegularPolygon #new(sides, center, diameter) ⇒ RegularPolygon #new(sides, center, indiameter) ⇒ RegularPolygon
Returns A new Geometry::RegularPolygon object.
45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/geometry/regular_polygon.rb', line 45 def initialize(edge_count:nil, sides:nil, center:nil, radius:nil, diameter:nil, indiameter:nil, inradius:nil) @edge_count = edge_count || sides raise ArgumentError, "RegularPolygon requires an edge count" unless @edge_count raise ArgumentError, "RegularPolygon.new requires a radius or a diameter" unless diameter || indiameter || inradius || radius @center = center ? Point[center] : Point.zero @diameter = diameter @indiameter = indiameter @inradius = inradius @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) Also known as: circumdiameter
77 78 79 |
# File 'lib/geometry/regular_polygon.rb', line 77 def diameter @diameter || (@radius && 2*@radius) || (@indiameter && @indiameter/cosine_half_angle) 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 |
#indiameter ⇒ Object
112 113 114 |
# File 'lib/geometry/regular_polygon.rb', line 112 def indiameter @indiameter || (@inradius && 2*@inradius) || (@diameter && (@diameter * cosine_half_angle)) || (@radius && (2 * @radius * cosine_half_angle)) end |
#inradius ⇒ Object Also known as: apothem
118 119 120 |
# File 'lib/geometry/regular_polygon.rb', line 118 def inradius @inradius || (@indiameter && @indiameter/2) || (@radius && (@radius * cosine_half_angle)) end |
#radius ⇒ Number (readonly) Also known as: circumradius
Returns The Geometry::RegularPolygon‘s radius.
125 126 127 |
# File 'lib/geometry/regular_polygon.rb', line 125 def radius @radius || (@diameter && @diameter/2) || (@inradius && (@inradius / cosine_half_angle)) || (@indiameter && @indiameter/cosine_half_angle/2) end |
#side_length ⇒ Object (readonly)
132 133 134 |
# File 'lib/geometry/regular_polygon.rb', line 132 def side_length 2 * circumradius * Math.sin(Math::PI/edge_count) end |
Instance Method Details
#bounds ⇒ Rectangle
Returns The smallest axis-aligned Geometry::Rectangle that bounds the receiver.
71 72 73 |
# File 'lib/geometry/regular_polygon.rb', line 71 def bounds return Rectangle.new(self.min, self.max) end |
#closed? ⇒ True
Check to see if the Polygon is closed (always true)
65 66 67 |
# File 'lib/geometry/regular_polygon.rb', line 65 def closed? true end |
#edges ⇒ Object
!@attribute [r] edges
83 84 85 86 |
# File 'lib/geometry/regular_polygon.rb', line 83 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: ==
58 59 60 |
# File 'lib/geometry/regular_polygon.rb', line 58 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.
96 97 98 |
# File 'lib/geometry/regular_polygon.rb', line 96 def max @center+Point[radius, radius] end |
#min ⇒ Point
Returns The lower left corner of the bounding Geometry::Rectangle.
101 102 103 |
# File 'lib/geometry/regular_polygon.rb', line 101 def min @center-Point[radius, radius] end |
#minmax ⇒ Array<Point>
Returns The lower left and upper right corners of the bounding Geometry::Rectangle.
106 107 108 |
# File 'lib/geometry/regular_polygon.rb', line 106 def minmax [self.min, self.max] end |
#vertices ⇒ Object Also known as: points
!@attribute [r] vertices
@return [Array]
90 91 92 |
# File 'lib/geometry/regular_polygon.rb', line 90 def vertices (0...2*Math::PI).step(2*Math::PI/edge_count).map {|angle| center + Point[Math::cos(angle), Math::sin(angle)]*radius } end |