Class: Geometry::Square
- Inherits:
-
Object
show all
- 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 = Square.new from:[1,2], to:[2,3] square = Square.new origin:[3,4], size:5
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(options = {}) ⇒ Square
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
# File 'lib/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)
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
Returns The lower left corner.
46
47
48
|
# File 'lib/geometry/square.rb', line 46
def origin
@origin
end
|
Instance Method Details
#height ⇒ Object
50
51
52
53
|
# File 'lib/geometry/square.rb', line 50
def height
min, max = @points.minmax {|a,b| a.y <=> b.y}
max.y - min.y
end
|
#width ⇒ Object
55
56
57
58
|
# File 'lib/geometry/square.rb', line 55
def width
min, max = @points.minmax {|a,b| a.x <=> b.x}
max.x - min.x
end
|