Class: Geometry::RegularPolygon

Inherits:
Polygon show all
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

DiameterRegularPolygon

Accessors collapse

Instance Attribute Summary collapse

Accessors collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ClusterFactory

included

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

Parameters:

  • edge_count (Number)

    The number of edges

  • center (Point)

    The center point of the Circle

  • radius (Number)

    The radius of the Circle



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

#centerPoint (readonly)

Returns The Geometry::RegularPolygon‘s center point.

Returns:



19
20
21
# File 'lib/geometry/regular_polygon.rb', line 19

def center
  @center
end

#diameterObject (readonly)



76
77
78
# File 'lib/geometry/regular_polygon.rb', line 76

def diameter
    @radius*2
end

#edge_countNumber (readonly)

Returns The Geometry::RegularPolygon‘s number of sides.

Returns:



22
23
24
# File 'lib/geometry/regular_polygon.rb', line 22

def edge_count
  @edge_count
end

#radiusNumber (readonly)

Returns The Geometry::RegularPolygon‘s radius.

Returns:



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

Overloads:

Raises:

  • (ArgumentError)


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(options={}, &block)
    raise ArgumentError, "RegularPolygon requires an edge count" unless options[:sides]

    center = options[:center]
    center = center ? Point[center] : Point.zero

    if options.has_key?(:radius)
	self.allocate.tap {|polygon| polygon.send :initialize, options[:sides], center, options[:radius], &block }
    elsif options.has_key?(:diameter)
	DiameterRegularPolygon.new options[:sides], center, options[:diameter], &block
    else
	raise ArgumentError, "RegularPolygon.new requires a radius or a diameter"
    end
end

Instance Method Details

#boundsRectangle

Returns The smallest axis-aligned Geometry::Rectangle that bounds the receiver.

Returns:



70
71
72
# File 'lib/geometry/regular_polygon.rb', line 70

def bounds
    return Rectangle.new(self.min, self.max)
end

#edgesObject

!@attribute [r] edges



81
82
83
84
# File 'lib/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: ==

Returns:

  • (Boolean)


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

#maxPoint

Returns The upper right corner of the bounding Geometry::Rectangle.

Returns:



93
94
95
# File 'lib/geometry/regular_polygon.rb', line 93

def max
    @center+Point[radius, radius]
end

#minPoint

Returns The lower left corner of the bounding Geometry::Rectangle.

Returns:



98
99
100
# File 'lib/geometry/regular_polygon.rb', line 98

def min
    @center-Point[radius, radius]
end

#minmaxArray<Point>

Returns The lower left and upper right corners of the bounding Geometry::Rectangle.

Returns:



103
104
105
# File 'lib/geometry/regular_polygon.rb', line 103

def minmax
    [self.min, self.max]
end

#verticesObject

!@attribute [r] vertices

@return [Array]


88
89
90
# File 'lib/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