Class: Geometry::CenteredRectangle

Inherits:
Rectangle
  • Object
show all
Defined in:
lib/geometry/rectangle.rb

Instance Attribute Summary collapse

Attributes inherited from Rectangle

#options, #origin

Accessors collapse

Instance Method Summary collapse

Methods inherited from Rectangle

#bounds, #inset, #minmax, new

Methods included from ClusterFactory

included

Constructor Details

#new(width, height) ⇒ CenteredRectangle #new(size) ⇒ CenteredRectangle #new(center, size) ⇒ CenteredRectangle

Returns a new instance of CenteredRectangle.

Overloads:



234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/geometry/rectangle.rb', line 234

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 = Geometry::Size[options[:size]]
    elsif options.has_key?(:height) and options.has_key?(:width)
  @size = Geometry::Size[options[:width], options[:height]]
    else
  raise ArgumentError, "Bad arguments to CenteredRectangle#new"
    end
end

Instance Attribute Details

#centerPoint

Returns The Rectangle‘s center.

Returns:



217
218
219
# File 'lib/geometry/rectangle.rb', line 217

def center
  @center
end

#sizeSize

Returns The Size of the Rectangle.

Returns:



219
220
221
# File 'lib/geometry/rectangle.rb', line 219

def size
  @size
end

Instance Method Details

#edgesArray<Edge>

Returns The Rectangle‘s four edges.

Returns:



256
257
258
259
260
261
262
263
264
265
# File 'lib/geometry/rectangle.rb', line 256

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) ⇒ Boolean Also known as: ==

Returns:

  • (Boolean)


249
250
251
# File 'lib/geometry/rectangle.rb', line 249

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

#heightObject



286
287
288
# File 'lib/geometry/rectangle.rb', line 286

def height
    @size.height
end

#maxPoint

Returns The upper right corner of the bounding Rectangle.

Returns:



268
269
270
# File 'lib/geometry/rectangle.rb', line 268

def max
    @center + @size/2.0
end

#minPoint

Returns The lower left corner of the bounding Rectangle.

Returns:



273
274
275
# File 'lib/geometry/rectangle.rb', line 273

def min
    @center - @size/2.0
end

#pointsArray<Point>

Returns The Rectangle‘s four points (clockwise).

Returns:



278
279
280
281
282
283
284
# File 'lib/geometry/rectangle.rb', line 278

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

#widthObject



290
291
292
# File 'lib/geometry/rectangle.rb', line 290

def width
    @size.width
end