Created
September 15, 2015 12:02
-
-
Save EricTYL/0d381f0b718661d01ad9 to your computer and use it in GitHub Desktop.
Q: Finding the overlap : Write a function which finds the rectangle intersection of two given rectangles.Note that the rectangles are always “straight”. (Each side parallel with x-axis or y-axis)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def rec_intersection(rect1, rect2) | |
rect3.x_min = [rect1.x_min, rect2.x_min].max | |
rect3.y_min = [rect3.y_min, rect3.y_min].max | |
rect3.x_max = [rect1.x_max, rect2.x_max].min | |
rect3.y_max = [rect1.y_max, rect2.y_max].min | |
return nil if ((x_max < x_min) || (y_max < y_min)) | |
return [[x_min, y_min], [x_max, y_max]] | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment