-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrectangle.rb
48 lines (43 loc) · 1.28 KB
/
rectangle.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
class Rectangle
attr_accessor :left_edge, :bottom_edge, :right_edge, :top_edge
# Public: init new rectangle with edges with passed coordinates.
#
# left_edge - x coordinate of left edge.
# bottom_edge - y coordinate of bottom edge.
# right_edge - x coordinate of right edge.
# top_edge - y coordinate of top edge.
def initialize(left_edge, bottom_edge, right_edge, top_edge)
@left_edge, @right_edge = left_edge, right_edge
@top_edge, @bottom_edge = top_edge, bottom_edge
end
# Public: width of a rectangle.
#
# Returns rectangle's width.
def width
@right_edge - @left_edge
end
# Public: height of rectangle.
#
# Returns rectangle's height.
def height
@bottom_edge - @top_edge
end
# Public: make an array consisting of rectangle's edges coordinates
# in following order: [left, bottom, right, top].
#
# Returns array of four elements.
def to_a
[@left_edge, @bottom_edge, @right_edge, @top_edge]
end
# Public: check whether two rectangles consisist the same sides.
#
# rect - rectangle to be checked against.
#
# Returns true if rectangles are equal.
def ==(rect)
@left_edge == rect.left_edge &&
@right_edge == rect.right_edge &&
@top_edge == rect.top_edge &&
@bottom_edge == rect.bottom_edge
end
end