Class: Geometry::Square
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]
Square.new origin:[3,4], size:5
Square.new center:[5,5], size:5
Square.new size:5
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
included
Constructor Details
#initialize(options = {}) ⇒ Square
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)
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
75
76
77
|
# File 'lib/geometry/square.rb', line 75
def closed?
true
end
|
#edges ⇒ Object
81
82
83
|
# File 'lib/geometry/square.rb', line 81
def edges
(points + [points.first]).each_cons(2).map {|v1,v2| Edge.new v1, v2}
end
|
#origin ⇒ Object
102
103
104
|
# File 'lib/geometry/square.rb', line 102
def origin
@points.first
end
|
#points ⇒ Array<Point>
21
22
23
|
# File 'lib/geometry/square.rb', line 21
def points
@points
end
|
Class Method Details
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
# File 'lib/geometry/square.rb', line 29
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
#height ⇒ Object
112
113
114
115
|
# File 'lib/geometry/square.rb', line 112
def height
min, max = @points.minmax {|a,b| a.y <=> b.y}
max.y - min.y
end
|
86
87
88
|
# File 'lib/geometry/square.rb', line 86
def max
@points.last
end
|
91
92
93
|
# File 'lib/geometry/square.rb', line 91
def min
@points.first
end
|
#minmax ⇒ Array<Point>
96
97
98
|
# File 'lib/geometry/square.rb', line 96
def minmax
[self.min, self.max]
end
|
124
125
126
|
# File 'lib/geometry/square.rb', line 124
def path
Path.new(*self.points, self.points.first)
end
|
#vertices ⇒ Object
Returns the value of attribute points.
22
23
24
|
# File 'lib/geometry/square.rb', line 22
def points
@points
end
|
#width ⇒ Object
117
118
119
120
|
# File 'lib/geometry/square.rb', line 117
def width
min, max = @points.minmax {|a,b| a.x <=> b.x}
max.x - min.x
end
|