Homework 8

Due Tuesday 30-March, at 10pm


To start

  1. Create a folder named ‘hw8’
  2. Create a new file, hw8.py, in that folder.
  3. Edit hw8.py and add the functions, classes, and some testcases as required.
  4. When you have completed and fully tested hw8, submit hw8.py to Gradescope.

For this hw, you may submit up to 999 times, but only your last submission counts.

Some important notes

  1. This homework is solo. You may not collaborate or discuss it with anyone outside of the course, and your options for discussing with other students currently taking the course are limited. See the academic honesty policy for more details.
  2. This assignment will be manually graded. That means that CAs will run your code and score it appropriately. (So the autograder will not give you an instant score.)
  3. Remember the course’s academic integrity policy. Solving the homework yourself is your best preparation for exams and quizzes; cheating or short-cutting your learning process in order to improve your homework score will actually hurt your course grade long-term.
  4. Your code will be graded for style. Check the style notes on the website for details.

Problems

  1. drawStar(canvas, centerX, centerY, diameter, numPoints, color) [20 pts, manually graded]
    Write the function drawStar which takes a canvas and the star's center coordinates, diameter, number of points, and color, and produces a star based on that specification. To draw a star, we need to identify where to place each of the inner and outer points, then draw them all together as a polygon.

    The outer points of the star should be evenly placed on a circle based on the specified diameter, with the first point at a 90 degree angle. The inner points should then be placed on a circle 3/8 the size of the first circle, halfway between the pairs of outer points. (We use this ratio to make a nice-looking five-pointed star. Actually, the best inner circle would be about 38.2% the size of the outer circle; a little trigonometry and problem-solving will tell you why! But 3/8 is close enough.) An example of how these circles work is shown below.




    For example, this call:
       drawStar(canvas, 250, 250, 500, 5, "gold")
    produces this result:

    And this call:
       drawStar(canvas, 300, 400, 100, 4, "blue")
    produces this result:

    And if we add a few more points:
       drawStar(canvas, 300, 200, 300, 9, "red")
    we get this result:



  2. drawUnitedStatesFlag(canvas, width=950, height=500)

    Write the function drawUnitedStatesFlag which draws the US flag in the provided dimensions. You can assume that the height:width ratio will be 10:19, as is the case with the actual US flag.

    You can find much useful information about the flag’s dimensions on Wikipedia:
    https://en.wikipedia.org/wiki/Flag_of_the_United_States, but we do not expect you to match the actual US flag design perfectly; you should instead seek to create a reasonable approximation of the flag. However, your flag must meet the following requirements:

    1. The flag should start from the upper left-hand corner of the window.

    2. The flag should have the correct number of stripes, alternating red and white in the correct order.

    3. The blue field in the upper left corner should cover exactly seven stripes and have a reasonably correct width.

    4. The flag should have the correct number of stars in the correct configuration, with the star size and spacing reasonably close to the actual flag.

    5. The colors should be the correct shades of red and blue. (Check Wikipedia)

    For an example of what reasonable flags might look like, here is an example:

    image

    Note: You should almost certainly call drawStar as part of solving this problem.


  3. drawSudokuBoard(canvas, board, width=800, height=800): [40 pts, manually graded]
    Write the function drawSudokuBoard which draws a Sudoku board based on the items in board. Your board should be a square of appropriate width and height based on the arguments width and height. (It should be largest possible square that can fit in the provided dimensions.)

    Recall that the board is stored in a 2D list of integers, with 0 representing a blank space. Here is a sample for a Sudoku board where N=3:
    board = [
      [ 5, 3, 0, 0, 7, 0, 0, 0, 0 ],
      [ 6, 0, 0, 1, 9, 5, 0, 0, 0 ],
      [ 0, 9, 8, 0, 0, 0, 0, 6, 0 ],
      [ 8, 0, 0, 0, 6, 0, 0, 0, 3 ],
      [ 4, 0, 0, 8, 0, 3, 0, 0, 1 ],
      [ 7, 0, 0, 0, 2, 0, 0, 0, 6 ],
      [ 0, 6, 0, 0, 0, 0, 2, 8, 0 ],
      [ 0, 0, 0, 4, 1, 9, 0, 0, 5 ],
      [ 0, 0, 0, 0, 8, 0, 0, 7, 9 ]
    ]
    The Sudoko board you draw should look, approximately, as follows:


    Remember that not all Sudoku boards have dimensions based on N=3. Your function should also work for boards based on other values of N. Here are some examples:
    An N=2 board:
    board=[
      [1, 2, 0, 4],
      [4, 0, 2, 0],
      [2, 0, 0, 3],
      [0, 1, 4, 0]
    ]
    An N=4 board:
    board = [
      [0, 15, 0, 1, 0, 2, 10, 14, 12, 0, 0, 0, 0, 0, 0, 0],
      [0, 6, 3, 16, 12, 0, 8, 4, 14, 15, 1, 0, 2, 0, 0, 0],
      [14, 0, 9, 7, 11, 3, 15, 0, 0, 0, 0, 0, 0, 0, 0, 0],
      [4, 13, 2, 12, 0, 0, 0, 0, 6, 0, 0, 0, 0, 15, 0, 0],
      [0, 0, 0, 0, 14, 1, 11, 7, 3, 5, 10, 0, 0, 8, 0, 12],
      [3, 16, 0, 0, 2, 4, 0, 0, 0, 14, 7, 13, 0, 0, 5, 15],
      [11, 0, 5, 0, 0, 0, 0, 0, 0, 9, 4, 0, 0, 6, 0, 0],
      [0, 0, 0, 0, 13, 0, 16, 5, 15, 0, 0, 12, 0, 0, 0, 0],
      [0, 0, 0, 0, 9, 0, 1, 12, 0, 8, 3, 10, 11, 0, 15, 0],
      [2, 12, 0, 11, 0, 0, 14, 3, 5, 4, 0, 0, 0, 0, 9, 0],
      [6, 3, 0, 4, 0, 0, 13, 0, 0, 11, 9, 1, 0, 12, 16, 2],
      [0, 0, 10, 9, 0, 0, 0, 0, 0, 0, 12, 0, 8, 0, 6, 7],
      [12, 8, 0, 0, 16, 0, 0, 10, 0, 13, 0, 0, 0, 5, 0, 0],
      [5, 0, 0, 0, 3, 0, 4, 6, 0, 1, 15, 0, 0, 0, 0, 0],
      [0, 9, 1, 6, 0, 14, 0, 11, 0, 0, 2, 0, 0, 0, 10, 8],
      [0, 14, 0, 0, 0, 13, 9, 0, 4, 12, 11, 8, 0, 0, 2, 0]
    ]