1 / 21

Checkerboard Examples

Checkerboard Examples. Top Down Design. The Problem. On a checkerboard where locations are numbered similar to java coordinates, draw a rectangle using checker pieces.

genera
Download Presentation

Checkerboard Examples

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Checkerboard Examples Top Down Design

  2. The Problem • On a checkerboard where locations are numbered similar to java coordinates, draw a rectangle using checker pieces. • You will be given the top left coordinate of the checkerboard, the size of individual squares, the location and dimensions of the rectangle to be drawn • Create disks with radius one third of the square widths

  3. Examples

  4. The Main Algorithm Given : (cbx, cby) : top left corner of checkerboard width : the width of one square rx, ry, rwidth, rheight : rectangle paremeters 1- Draw the checkerboard on screen 2- Put the pieces in a rectangular form (So here the big decision is to separate these two tasks. Instead of drawing 64 more complex pieces, we will draw 64 + rwidth * rheight simpler objects (hopefully))

  5. Observation • Here we have two different types of coordinates : coordinates in pixel locations, and coordinates in checkerboard positions. • In order to be able to swiftly go back and forth between these two different systems, let’s introduce conversion functions. • What type of functions do we need?

  6. Conversions • We need a function that, given a checkerboard coordinate, returns the pixel coordinate of the top left corner of that square. • Let’s call the function cb2pixels, cb being short for checkerboard. • What inputs are needed, and what will cb2pixels output?

  7. cb2Pixels function function [ px, py ] = cb2pixels( rx, ry, x, y, width) %CB2PIXELS Converts a checkerboard position to pixel positions % Given a checkerboard position (rx,ry), and board parameters, % returns x and y coordinates in pixels. px = x + width * (rx-1); py = y + width * (ry-1);

  8. Pixels2cb? • Do we need to convert from pixel positions to board locations? No need at this point. We could have needed that if the user could click on the board, and we want to know which location was clicked.

  9. 1-Draw the checkerboard For i= 1 to 8 do for j=1 to 8 do if (i+j) is even draw a colored square at (i,j) else draw a white square (i,j)

  10. Draw a White square? • Since the background is already white, just draw a boundary rectangle Given x, y, cbx, cby, and width [px py] = cb2pixels(x, y, cbx, cby, width) drawHorizontalLine(px, px + width, py, g) drawHorizontalLine(px, px + width, py + width, g) drawVerticalLine(py, py + width, px, g) drawVerticalLine(py, py + width, px+width, g)

  11. Draw a Colored Rectangle? • Homework question

  12. Draw a chessboard Rectangle? We already know how to do this using pixels Given rx, ry, rwidth, rheight , width for i=rx to rx + rwidth – 1 do for j=ry to ry + rheight – 1 do draw a disk to location (i, j)

  13. Draw a Piece? Given cbx, cby, width, x, y [px py] = cb2pixels(x, y, cbx, cby, width) cx = px + width/2 cy = py + width/2 cradius = width/3 Draw a disk centered at (cx, cy) with radius cradius

  14. Decomposition of our problem

  15. drawCheckerBoardRectangle.m function drawCheckerBoardRectangle(cbx, cby, width, rx, ry, rwidth, rheight, g) %DRAWCHECKERBOARDRECTANGLE Draws a rectangular pattern on checkerboard % cbx, cby being the top left corner of the checkerboard, this function % first draws it with squares of size width, and then draws a rectangle % using pieces. rx,ry is the top left corner of the rectangle, rwidth and % rheight are the dimensions of the rectangle drawCheckerBoard(cbx, cby, width, g); drawRectangularPieces(cbx, cby, width, rx, ry, rwidth, rheight, g);

  16. drawCheckerBoard.m function drawCheckerBoard(cbx, cby, width, g) for i= 1:8 for j=1:8 if (mod(i+j, 2) == 0) drawColoredSquare(i,j, cbx, cby, width, g) else drawWhiteSquare(i,j, cbx, cby, width, g) end end end

  17. drawWhiteSquare.m function drawWhiteSquare(x, y, cbx, cby, width, g) [px py] = cb2pixels(x, y, cbx, cby, width); drawHorizontalLine(px, px + width, py, g) drawHorizontalLine(px, px + width, py + width, g) drawVerticalLine(py, py + width, px, g) drawVerticalLine(py, py + width, px+width, g)

  18. drawColoredSquare.m function drawColoredSquare( x, y, cbx, cby, width, g ) [px py] = cb2pixels(x, y, cbx, cby, width); drawGreyRectangle(px, py, width, width, g)

  19. drawGreyRectangle.m function drawGreyRectangle(x,y, width, height, g) startsWithBlack = true; for cY=y:y + height isNextBlack = startsWithBlack; for cX = x:x + width if (isNextBlack) putPixel(cX, cY, g); end isNextBlack = ~isNextBlack; end startsWithBlack = ~startsWithBlack; end

  20. drawRectangularPieces.m function drawRectangularPieces(cbx, cby, width, rx, ry, rwidth, rheight, g) for i = rx : (rx + rwidth - 1) for j = ry : (ry + rheight - 1 ) drawBlackPiece(i, j, cbx, cby, width, g); end end

  21. drawBlackPiece.m function drawBlackPiece(x, y, cbx, cby, width, g) [px py] = cb2pixels(x, y, cbx, cby, width); cx = px + width/2; cy = py + width/2; cradius = width/3; drawDisk(cx, cy, cradius, g)

More Related