class Euclidean::Circle

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

Usage

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

Attributes

center[R]

@return [Point] The {Circle}‘s center point

radius[R]

@return [Number] The {Circle}‘s radius

Public Class Methods

new(*args, &block) click to toggle source

@overload new(center, radius)

Construct a {Circle} using a centerpoint and radius
@param [Point]   center  The center point of the {Circle}
@param [Number]  radius   The radius of the {Circle}

@overload new(center, radius)

Construct a circle using named center and radius parameters
@option options [Point]  :center (PointZero)
@option options [Number] :radius

@overload new(center, diameter)

Construct a circle using named center and diameter parameters
@option options [Point]  :center (PointZero)
@option options [Number] :diameter
# File lib/euclidean/circle.rb, line 35
  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
new(center, radius) click to toggle source

Construct a new {Circle} from a centerpoint and radius @param [Point] center The center point of the {Circle} @param [Number] radius The radius of the {Circle} @return [Circle] A new {Circle} object

# File lib/euclidean/circle.rb, line 56
def initialize(center, radius)
    @center = Point[center]
    @radius = radius
end

Public Instance Methods

==(other)
Alias for: eql?
bounds() click to toggle source

@!group Accessors @return [Rectangle] The smallest axis-aligned {Rectangle} that bounds the receiver

# File lib/euclidean/circle.rb, line 68
def bounds
  return Euclidean::Rectangle.new(self.min, self.max)
end
diameter() click to toggle source

@!attribute [r] diameter

@return [Numeric] The diameter of the {Circle}
# File lib/euclidean/circle.rb, line 74
def diameter
  @radius*2
end
eql?(other) click to toggle source
# File lib/euclidean/circle.rb, line 61
def eql?(other)
  (self.center == other.center) && (self.radius == other.radius)
end
Also aliased as: ==
intersects_circle?(other) click to toggle source

Two circles intersect if, and only if, the distance between their centers is between the sum and the difference of their radii. Given two circles (x0,y0,R0) and (x1,y1,R1), the formula is as follows: (R0-R1)^2 <= (x0-x1)^2+(y0-y1)^2 <= (R0+R1)^2 @param [Circle] a {Circle} object @return [Boolean]

# File lib/euclidean/circle.rb, line 83
def intersects_circle?(other)
  raise TypeError, "#{other.class} must be a #{self.class}" unless other.kind_of? Euclidean::Circle
  ( (self.center.x - other.center.x)**2 + (self.center.y - other.center.y)**2  ).between?( ((self.radius - other.radius)**2), ((self.radius + other.radius)**2) )
end
max() click to toggle source

@return [Point] The upper right corner of the bounding {Rectangle}

# File lib/euclidean/circle.rb, line 89
def max
  @center+radius
end
min() click to toggle source

@return [Point] The lower left corner of the bounding {Rectangle}

# File lib/euclidean/circle.rb, line 94
def min
  @center-radius
end
minmax() click to toggle source

@return [Array<Point>] The lower left and upper right corners of the bounding {Rectangle}

# File lib/euclidean/circle.rb, line 99
def minmax
  [self.min, self.max]
end