-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrectangleJoin.rb
More file actions
72 lines (64 loc) · 1.49 KB
/
rectangleJoin.rb
File metadata and controls
72 lines (64 loc) · 1.49 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
class Point < Struct.new(:name, :x, :y)
def printData
puts [self.name, " x:", self.x, ", y:", self.y ].join('')
end
def getX
x
end
def getY
y
end
end
class Rectangle
def initialize(name, p1, p2)
@name = name
@bl = Point.new('@bl', [p1.x, p2.x].min, [p1.y, p2.y].min) # botomLeft
@tr = Point.new('@tr', [p1.x, p2.x].max, [p1.y, p2.y].max) #topRight
end
def printData
puts [@name, ':'].join()
@bl.printData()
@tr.printData()
puts "========================"
end
def getName
@name
end
def get_blx
@bl.getX
end
def get_bly
@bl.getY
end
def get_trx
@tr.getX
end
def get_try
@tr.getY
end
end
class RectJoin
def initialize(r1, r2)
@name = [r1.getName, r2.getName].join(' X ')
@bl = Point.new('@bl', [r1.get_blx, r2.get_blx].max, [r1.get_bly, r2.get_bly].max)
@tr = Point.new('@tr', [r1.get_trx, r2.get_trx].min, [r1.get_try, r2.get_try].min)
@width = @tr.getX() - @bl.getX()
@hight = @tr.getY() - @bl.getY()
@status = (@width <= 0 or @hight <=0) ? "INVALID" : "VALID"
end
def printData
puts [["rectJoin:", @name].join(' '), ':'].join()
puts ['width:', @width, 'hight:', @hight, 'status:', @status].join(' ')
end
end
# MAIN
p1 = Point.new('p1', -7, 8)
p2 = Point.new('p2', 5, -5)
r1 = Rectangle.new('rect1',p2, p1)
p3 = Point.new('p3', 3, 2)
p4 = Point.new('p4', 5, -7)
r2 = Rectangle.new('rect2', p3, p4)
r1.printData()
r2.printData()
join = RectJoin.new(r1, r2)
join.printData()