/* * Starting code for MCS-178 Assignment: Mondrian Art * * Generate and draw a Mondrian-style image using StdDraw. */ const val WIDTH = 1024 // width of the image being generated const val HEIGHT = 768 // height of the image being generated const val SPLIT_LIMIT = 120 // don't split if length < 120 val randGen = java.util.Random() /* * Select a color so that the odd for white is 3 to 1, * and the other colors being equally likely. */ fun randomColor() = when (randGen.nextInt(12)) { 0 -> StdDraw.RED 1 -> StdDraw.BOOK_LIGHT_BLUE 2 -> StdDraw.YELLOW else -> StdDraw.WHITE } /* Return a random integer between 33% and 66% of 'length' inclusive */ fun randomMiddleThird(length: Int) = ((33 + randGen.nextInt(34)) / 100.0 * length).toInt() /* * This function currently draws a filled rectangle in a random color * with black borders. You task is to replace the implementation * of this function so that it draws many small rectangles needed * for a piece of random Mondrian art. You may write additional * helper functions to accomplish this task. * * Parameters: * x, y: The upper left corner of the region * w, h: The width and height of the region */ fun mondrian(x: Int, y: Int, w: Int, h: Int) { // first draw the rectangle borders in black StdDraw.setPenColor(StdDraw.BLACK) StdDraw.rectangle(x+w/2.0, y+h/2.0, w/2.0, h/2.0) // now fill rectangle with a random color StdDraw.setPenColor(randomColor()) StdDraw.filledRectangle(x+w/2.0, y+h/2.0, w/2.0, h/2.0) } /* Generate and draw a "Mondrian" art. */ fun main(args: Array) { StdDraw.setXscale(0.0, WIDTH.toDouble()) StdDraw.setYscale(0.0, HEIGHT.toDouble()) StdDraw.setPenRadius(0.005) mondrian(0, 0, WIDTH, HEIGHT) }