// // 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:(Int,Int,Int,Int), rect2:(Int,Int,Int,Int)) : (Int,Int,Int,Int) = { // pull out components of the two input rectangles val (y1, x1, w1, h1) = rect1 val (y2, x2, w2, h2) = rect2 // find top left corner of bounding box val y = y1 min y2 val x = x1 min x2 // find bottom right corner of bounding box val yBR = (y1 + w1) max (y2 + w2) val xBR = (x1 + h1) max (x2 + h2) // find width, height val w = yBR - y val h = xBR - x // return bounding box as rectangle (y, x, w, h) } // // utility function to display elements of rectangle in more-readable form // def showRectangle(r:(Int,Int,Int,Int)) { val (y,x,w,h) = r println("top left corner y (horizontal) " + y) println("top left corner x (vertical) " + x) println("width " + w) println("height " + h) println("bottom right corner y (horizontal) " + (y+w)) println("bottom right corner x (vertical) " + (x+h)) }