Class: Geometry::Square

Inherits:
Object
  • Object
show all
Defined in:
lib/aurora-geometry/square.rb

Overview

The Square class cluster is like the Rectangle class cluster, but not longer in one direction.

Constructors

square = Square.new from:[1,2], to:[2,3]	# Using two corners
square = Square.new origin:[3,4], size:5	# Using an origin point and a size

Direct Known Subclasses

CenteredSquare, SizedSquare

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Square

Creates a Geometry::Square given two Points

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :from (Point)

    A corner (ie. bottom-left)

  • :to (Point)

    The other corner (ie. top-right)

  • :origin (Point)

    The lower left corner

  • :size (Number)

    Bigness

Raises:

  • (ArgumentError)


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/aurora-geometry/square.rb', line 22

def initialize(options={})
    origin = options[:from] || options[:origin]
    origin = origin ? Point[origin] : PointZero.new

    if options.has_key? :to
	point1 = options[:to]
    elsif options.has_key? :size
	point1 = origin + options[:size]
    end

    point1 = Point[point1]
    raise(ArgumentError, "Point sizes must match (#{origin.size} != #{point1.size})") unless origin.is_a?(PointZero) || (origin.size == point1.size)

    # Reorder the points to get lower-left and upper-right
    minx, maxx = [origin.x, point1.x].minmax
    miny, maxy = [origin.y, point1.y].minmax
    @points = [Point[minx, miny], Point[maxx, maxy]]

    raise(NotSquareError) if height != width
end

Instance Attribute Details

#originPoint (readonly)

Returns The lower left corner.

Returns:

  • (Point)

    The lower left corner



46
47
48
# File 'lib/aurora-geometry/square.rb', line 46

def origin
  @origin
end

Instance Method Details

#heightObject



50
51
52
53
# File 'lib/aurora-geometry/square.rb', line 50

def height
    min, max = @points.minmax {|a,b| a.y <=> b.y}
    max.y - min.y
end

#widthObject



55
56
57
58
# File 'lib/aurora-geometry/square.rb', line 55

def width
    min, max = @points.minmax {|a,b| a.x <=> b.x}
    max.x - min.x
end