Class: Geometry::SizedRectangle

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

Instance Attribute Summary collapse

Accessors collapse

Instance Method Summary collapse

Methods inherited from Rectangle

#bounds, #minmax, new

Methods included from ClusterFactory

included

Constructor Details

#new(width, height) ⇒ Object #new(size) ⇒ Object #new(origin, size) ⇒ Object

Returns a new instance of SizedRectangle.

Overloads:

  • #new(width, height) ⇒ Object

    Creates a Rectangle of the given width and height with its origin at [0,0]

  • #new(size) ⇒ Object

    Creates a Rectangle of the given Geometry::Size with its origin at [0,0]

  • #new(origin, size) ⇒ Object

    Creates a Rectangle with the given origin point and size



277
278
279
280
281
282
283
284
285
286
287
288
289
290
# File 'lib/geometry/rectangle.rb', line 277

def initialize(*args)
    options, args = args.partition {|a| a.is_a? Hash}
    options = options.reduce({}, :merge)

    @origin = options[:origin] ? Point[options[:origin]] : 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 SizeRectangle#new"
    end
end

Instance Attribute Details

#originPoint



259
260
261
# File 'lib/geometry/rectangle.rb', line 259

def origin
  @origin
end

#sizeSize



261
262
263
# File 'lib/geometry/rectangle.rb', line 261

def size
  @size
end

Instance Method Details

#centerPoint



299
300
301
# File 'lib/geometry/rectangle.rb', line 299

def center
    @origin + @size/2
end

#edgesArray<Edge>



304
305
306
307
308
309
310
311
312
313
# File 'lib/geometry/rectangle.rb', line 304

def edges
    point0 = @origin
    point2 = @origin + @size
    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: ==



292
293
294
# File 'lib/geometry/rectangle.rb', line 292

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

#heightObject



334
335
336
# File 'lib/geometry/rectangle.rb', line 334

def height
    @size.height
end

#maxPoint



316
317
318
# File 'lib/geometry/rectangle.rb', line 316

def max
    @origin + @size
end

#minPoint



321
322
323
# File 'lib/geometry/rectangle.rb', line 321

def min
    @origin
end

#pointsArray<Point>



326
327
328
329
330
331
332
# File 'lib/geometry/rectangle.rb', line 326

def points
    point0 = @origin
    point2 = @origin + @size
    point1 = Point[point0.x,point2.y]
    point3 = Point[point2.x, point0.y]
    [point0, point1, point2, point3]
end

#widthObject



338
339
340
# File 'lib/geometry/rectangle.rb', line 338

def width
    @size.width
end