Skip to content

Instantly share code, notes, and snippets.

@EricTYL
Created September 15, 2015 12:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EricTYL/0d381f0b718661d01ad9 to your computer and use it in GitHub Desktop.
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)
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