-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrectangle_intersection.rb
More file actions
29 lines (25 loc) · 901 Bytes
/
Copy pathrectangle_intersection.rb
File metadata and controls
29 lines (25 loc) · 901 Bytes
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
def find_range_overalp(point_1, length_1, point_2, length_2)
highest_starting_point = [point_1, point_2].max
lowest_end_point = [point_1 + length_1, point_2 + length_2].min
return (nil, nil) if highest_starting_point >= lowest_end_point
overlap_length = lowest_end_point - highest_starting_point
(highest_starting_point, overlap_length)
end
def find_rectangular_overlap(rect_1, rect_2)
x_overlap_point, overlap_width = find_range_overlap(rect_1['x'], rect_1['width'], rect_2['x'], rect_2['width'])
y_overlap_point, overlap_height = find_range_overlap(rect_1['y'], rect_1['height'], rect_2['y'], rect_2['height'])
if not overlap_height or not overlap_width
return {
'x': nil,
'y': nil,
'width': nil,
'height': nil
}
end
return {
'x': x_overlap_point,
'y': y_overlap_point,
'width': overlap_width,
'height': overlap_height
}
end