//
// "bounding box" functions
//

case class Rectangle(y:Int, x:Int, w:Int, h:Int)

//
// function to compute bounding box of two rectangles
//
// rectangles are represented using the "graphics convention":
//   (y, x, width, height)
// where (y, x) is the top left corner in the graphics coordinate system,
//   in which the y axis is horizontal and runs left to right 
//   and the x axis is vertical and runs top to bottom
// 

def bbox(rect1:Rectangle, rect2:Rectangle) : Rectangle = {
  // find top left corner of bounding box
  val y = rect1.y min rect2.y
  val x = rect1.x min rect2.x

  // find bottom right corner of bounding box
  val yBR = (rect1.y + rect1.w) max (rect2.y + rect2.w)
  val xBR = (rect1.x + rect1.h) max (rect2.x + rect2.h)

  // find width, height
  val w = yBR - y
  val h = xBR - x

  // return bounding box as rectangle
  Rectangle(y, x, w, h)
}

//
// utility function to display elements of rectangle in more-readable form
//
def showRectangle(r:Rectangle) {
  println("top left corner y (horizontal) " + r.y)
  println("top left corner x (vertical) " + r.x)
  println("width " + r.w)
  println("height " + r.h)
  println("bottom right corner y (horizontal) " + (r.y+r.w))
  println("bottom right corner x (vertical) " + (r.x+r.h))
}