Homework 8

Due Tuesday 2-Nov, at 10:00pm


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

While you may submit to Gradescope as often as you like for this assignment, there is no autograded portion, so you will be responsible for testing your code and making sure it meets the problem requirements. As stated in the style guide, you do not have to write test cases for interactive, random, graphics, data initialization or event functions. Instead, you should test by visually inspecting your code’s behavior as you complete steps of each problem, where reasonably possible. This will make debugging your code much easier.


  1. drawBarChart(canvas, w, h, L) [25 pts, manually graded]

    Write a program that draws a bar chart that shows the distribution of values in L, where L is a list of strings. For instance,
    L = ['bananas', 'oranges', 'oranges', 'bananas', 'apples', \ 'oranges', 'apples', 'pineapple', 'apples', 'apples' ]
    will produce the following bar chart:
    Example

    In this example, L has 10 elements: 4 apples, 2 bananas, 3 oranges, 1 pineapple. In other words, a 0.4 fraction of the items in L are apples, 0.2 bananas, 0.3 oranges, and 0.1 pineapple.

    Here are some details on the parameters of the chart.
    1. The chart must have 11, equally-spaced y-ticks on the y-axis with labels 0.0, 0.1, ..., 1.0
    2. Draw dashed lines from left to right as shown in the example, aligned with each y-tick.
    3. The chart must have n equally spaced x-ticks on the x-axis, where n is the number of unique values in L. Each tick should be labeled with the corresponding word in a vertical orientation.
    4. There should a reasonable separation between the tick labels and the axes.
    5. All bars should be next to each other, without gaps in between.
    6. The height of each bar should be proportional to the frequency of the item in the list: it should represent the fraction value in the plot.
    7. Do not worry about rounding when calculating the height.
    8. You can choose the colors however you would like, but you should make sure you can support at least 6 different colors and no adjacent bars should have the same color.

  2. drawFractalSun(canvas, xc, yc, r, level) [30 pts, manually graded]
    Write a program that draws a majestic fractal sun. The fractal sun is composed of a circle of radius r, and 8 rays of length 2*r originating from the center of the circle and radially equally spaced. The external tip of each ray is also the origin of a recursively downsized fractal sun with radius equal to 1/4 of the radius of the sun at the previous level. Also, the suns originating at the tip of the rays will have different colors, i.e., the color of a sun is a function of the recursion level of the fractal sun itself. You can invent the coloring function you prefer, just make sure that your sun will look good no matter what the maximum level of recursion is going to be. Your fractal sun will be generated by a function drawFractalSun(canvas, xc, yc, r, level) which you will write. Your function will take as parameter a canvas to draw on, the (xc, yc) coordinates of the center of the sun, the radius r of the sun's circle, and an integer level representing the maximum depth of recursion of your fractal sun. The following picture shows a fractal sun with a maximum recursion depth of 5, with colors fading from yellow to red.
    Example 1
    Your program should include some test code that draws a fractal sun of depth 5 on a canvas of your choice.

  3. drawChessBoard(canvas, width, height, board, color, margin) [45 pts, manually graded]
    Write a program that draws a chess board as follows:
    Example 1
    Here are some details on the parameters:
    1. board is a string of 64 characters. You may assume that it contains only spaces and characters (p,P,b,B,h,H,r,R,q,Q,k,K) representing the 6 pieces of Chess More information about Chess pieces.
    2. Lower-case letters in the string board represent white pieces, and upper-case letters black pieces.
    3. Each character of board indicates if a square is empty (space), or if it contains one piece. The squares are described row-wise, starting from the top row. The example chess board corresponds to the string:
      "RHBQKBHRPPPPPPPP pppppppprhbqkbhr"
    4. color is the an rgb tuple representing the color of the margin. "Lightening" color gives you the color of the non-white squares on the board (see below).
    5. margin is the distance between the grid and the edge of the screen.

    You must satisfy the following additional requirements to get full credit:
    1. You should be able to handle any width and height. The size of the board should adjust to fit the whole board on the screen.
    2. Your margin must be exactly what we specified. That is: your board must expand to fill up the full screen leaving only the margin given as colored space.
    3. Use Unicode characters to represent chess pieces More information about unicode characters
    4. Your font size must adjust with the size of the board. Pick a reasonable ratio between your square/cell size and your font size.
    5. Your board should have a checkerboard pattern, with the top left square always being white.
    6. Your non-white squares should be a "lightened" version of your color parameter. You can lighten an rgb color by adding a constant amount to the r, g, and b values (but make sure they're capped at 255!). See the rgbString function from the graphics notes to turn this tuple into a tkinter-compatible color. You may choose any reasonable constant to lighten by.