Class: Geometry::Square

Inherits:
Object
  • Object
show all
Includes:
ClusterFactory
Defined in:
lib/geometry/square.rb

Overview

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

Constructors

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

Direct Known Subclasses

CenteredSquare, SizedSquare

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ClusterFactory

included

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

Raises:

  • (ArgumentError)


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

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

Instance Attribute Details

#closed?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/geometry/square.rb', line 74

def closed?
    true
end

#originPoint

Returns The lower left corner.

Returns:

  • (Point)

    The lower left corner



21
22
23
# File 'lib/geometry/square.rb', line 21

def origin
  @origin
end

Class Method Details

.new(: origin, : size) ⇒ CenteredSquare

Creates a Geometry::Square with the given origin and size

Returns:



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/geometry/square.rb', line 28

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

    if options.key?(:size)
	unless options[:size].is_a? Numeric
	    raise NotSquareError, 'Size must be a square' unless options[:size].all? {|a| a == options[:size].first}
	    options[:size] = options[:size].first
	end

	if options.key? :origin
	    SizedSquare.new(options[:origin], options[:size])
	else
	    CenteredSquare.new(options[:center] || PointZero.new, options[:size])
	end
    elsif options.key?(:from) and options.key?(:to)
	original_new(from: options[:from], to: options[:to])
    end
end

Instance Method Details

#heightObject



99
100
101
102
# File 'lib/geometry/square.rb', line 99

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

#maxPoint

Returns The upper right corner of the bounding Rectangle.

Returns:



79
80
81
# File 'lib/geometry/square.rb', line 79

def max
    @points.last
end

#minPoint

Returns The lower left corner of the bounding Rectangle.

Returns:



84
85
86
# File 'lib/geometry/square.rb', line 84

def min
    @points.first
end

#minmaxArray<Point>

Returns The lower left and upper right corners of the bounding Rectangle.

Returns:

  • (Array<Point>)

    The lower left and upper right corners of the bounding Rectangle



89
90
91
# File 'lib/geometry/square.rb', line 89

def minmax
    [self.min, self.max]
end

#widthObject



104
105
106
107
# File 'lib/geometry/square.rb', line 104

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