// // simple ascii-art program (mostly for testing bounding-box function) // // preliminary version from class, slightly tidied up // import scala.io.StdIn._ // 2D array of Char to use as display area val Rows = 12 val Columns = 40 val screen = Array.fill(Rows,Columns)(' ') // list of rectangles, in reverse order of addition var rectangles = List[(Int,Int,Int,Int)]() // main processing loop -- prompt, get command, and do what user says val prompt = """enter one of the following: r for new rectangle d to display q to quit""" var in = getInput() while (in != "q") { if (in == "r") { newRectangle() } else if (in == "d") { display() } else { println("error!") } in = getInput() } // prompt for and get next "command" def getInput() : String = { println(prompt) readLine } // prompt for and get information for rectangle, and add to "rectangles" list def newRectangle() { println("enter coordinate of top left, then width and height") val y = readInt val x = readInt val w = readInt val h = readInt rectangles = (y,x,w,h) :: rectangles } // "draw" rectangles, etc. def display() { // add rectangles to display (reversing order) rectangles.reverse.foreach(fillRectangle(_)) // compute bounding box var bb = (0,0,0,0) if (!rectangles.isEmpty) { bb = rectangles.head rectangles.tail.foreach(r => { bb = bbox(bb, r) } ) } println("bounding box "+bb) println("FIXME! draw bounding box") // print display for (r <- 0 until Rows) { for (c <-0 until Columns) { print(screen(r)(c)) } println() } } // add one rectangle to display def fillRectangle(r:(Int,Int,Int,Int)) { println("filling "+r) val (y,x,w,h) = r for (r <- x until x+h) { for (c <- y until y+w) { screen(r)(c) = '.' } } } // // function to compute bounding box of two rectangles // (copy-and-paste from bbox.scala) // // 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) }