Method: Geometry::Square#initialize

Defined in:
lib/geometry/square.rb

#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

Raises:

  • (ArgumentError)


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/geometry/square.rb', line 53

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

    if options.has_key? :to
	point1 = options[:to]
    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