class Euclidean::CenteredRectangle
Attributes
center[RW]
@return [Point] The {Rectangle}‘s center
size[RW]
@return [Size] The {Size} of the {Rectangle}
Public Class Methods
new(*args)
click to toggle source
@overload new(width, height)
Creates a {Rectangle} of the given width and height, centered on the origin @param [Number] height Height @param [Number] width Width @return [CenteredRectangle]
@overload new(size)
Creates a {Rectangle} of the given {Size} centered on the origin @param [Size] size Width and height @return [CenteredRectangle]
@overload new(center, size)
Creates a {Rectangle} with the given center point and size @param [Point] center @param [Size] size
# File lib/euclidean/rectangle.rb, line 227 def initialize(*args) options, args = args.partition {|a| a.is_a? Hash} options = options.reduce({}, :merge) @center = options[:center] ? Point[options[:center]] : PointZero.new if options.has_key?(:size) @size = Euclidean::Size[options[:size]] elsif options.has_key?(:height) and options.has_key?(:width) @size = Euclidean::Size[options[:width], options[:height]] else raise ArgumentError, "Bad arguments to CenteredRectangle#new" end end
Public Instance Methods
edges()
click to toggle source
@group Accessors @return [Array<Edge>] The {Rectangle}‘s four edges
# File lib/euclidean/rectangle.rb, line 249 def edges point0 = @center - @size/2.0 point2 = @center + @size/2.0 point1 = Point[point0.x,point2.y] point3 = Point[point2.x, point0.y] [Edge.new(point0, point1), Edge.new(point1, point2), Edge.new(point2, point3), Edge.new(point3, point0)] end
eql?(other)
click to toggle source
# File lib/euclidean/rectangle.rb, line 242 def eql?(other) (self.center == other.center) && (self.size == other.size) end
Also aliased as: ==
height()
click to toggle source
# File lib/euclidean/rectangle.rb, line 260 def height @size.height end
max()
click to toggle source
@return [Point] The upper right corner of the bounding {Rectangle}
# File lib/euclidean/rectangle.rb, line 265 def max @center + @size/2.0 end
min()
click to toggle source
@return [Point] The lower left corner of the bounding {Rectangle}
# File lib/euclidean/rectangle.rb, line 270 def min @center - @size/2.0 end
points()
click to toggle source
@return [Array<Point>] The {Rectangle}‘s four points (clockwise)
# File lib/euclidean/rectangle.rb, line 275 def points point0 = @center - @size/2.0 point2 = @center + @size/2.0 point1 = Point[point0.x,point2.y] point3 = Point[point2.x, point0.y] [point0, point1, point2, point3] end
width()
click to toggle source
# File lib/euclidean/rectangle.rb, line 283 def width @size.width end