Class: Geometry::Circle

Inherits:
Object
  • Object
show all
Includes:
ClusterFactory
Defined in:
lib/geometry/circle.rb

Overview

Circles come in all shapes and sizes, but they’re usually round.

Usage

circle = Geometry::Circle.new [1,2], 3
circle = Geometry::Circle.new center:[1,2], radius:3
circle = Geometry::Circle.new center:[1,2], diameter:6
circle = Geometry::Circle.new diameter:6

Direct Known Subclasses

CenterDiameterCircle

Accessors collapse

Instance Attribute Summary collapse

Accessors collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ClusterFactory

included

Constructor Details

#initialize(center, radius) ⇒ Circle

Construct a new Geometry::Circle from a centerpoint and radius

Parameters:



58
59
60
61
# File 'lib/geometry/circle.rb', line 58

def initialize(center, radius)
    @center = Point[center]
    @radius = radius
end

Instance Attribute Details

#centerPoint (readonly)

Returns The Geometry::Circle‘s center point.

Returns:



20
21
22
# File 'lib/geometry/circle.rb', line 20

def center
  @center
end

#diameterObject (readonly)



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

def diameter
    @radius*2
end

#radiusNumber (readonly)

Returns The Geometry::Circle‘s radius.

Returns:



23
24
25
# File 'lib/geometry/circle.rb', line 23

def radius
  @radius
end

Class Method Details

.new(center, radius) ⇒ Object .new(center, radius) ⇒ Object .new(center, diameter) ⇒ Object

Overloads:

  • .new(center, radius) ⇒ Object

    Construct a Geometry::Circle using a centerpoint and radius

    Parameters:

  • .new(center, radius) ⇒ Object

    Construct a circle using named center and radius parameters

  • .new(center, diameter) ⇒ Object

    Construct a circle using named center and diameter parameters



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/geometry/circle.rb', line 37

def self.new(*args, &block)
    options, args = args.partition {|a| a.is_a? Hash}
    options = options.reduce({}, :merge)
    center, radius = args[0..1]

    center ||= (options[:center] || PointZero.new)
    radius ||= options[:radius]

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

Instance Method Details

#boundsRectangle

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

Returns:



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

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

#eql?(other) ⇒ Boolean Also known as: ==

Returns:

  • (Boolean)


63
64
65
# File 'lib/geometry/circle.rb', line 63

def eql?(other)
    (self.center == other.center) && (self.radius == other.radius)
end

#maxPoint

Returns The upper right corner of the bounding Rectangle.

Returns:



81
82
83
# File 'lib/geometry/circle.rb', line 81

def max
    @center+radius
end

#minPoint

Returns The lower left corner of the bounding Rectangle.

Returns:



86
87
88
# File 'lib/geometry/circle.rb', line 86

def min
    @center-radius
end

#minmaxArray<Point>

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

Returns:

  • (Array<Point>)

    The lower left and upper right corners of the bounding Rectangle



91
92
93
# File 'lib/geometry/circle.rb', line 91

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