Difference between revisions of "CSC111 Final Exam 2018"

From dftwiki3
Jump to: navigation, search
Line 56: Line 56:
 
=Problem 2: An Orange Tree =
 
=Problem 2: An Orange Tree =
 
<br />
 
<br />
Write a well documented and organized Python program that will generate a tree ''as close to the tree shown below'' as possible. You should not reproduce the text in the image;  '''just the tree, its colors, and its fruit.'''
+
[[Image:fractalTreeWithOranges2018.png|center|500px]]
 
<br />
 
<br />
<center>[[Image:FractalTreeWithLeaves.png| 500px]]</center>
+
* Modify the fractal tree program shown below so that it will generate an exact replica of the tree shown above.  You should not reproduce the text in the image;  '''just the tree, its colors, and its fruit.'''
<br />
+
* Call your program '''final2.py''' and the image '''final2.jpg''' or '''final2.png'''.  Submit both to the same URL indicated in the Final Examp PB 2 section on Moodle.
* Call your program '''final2.py''' and the image '''final2.jpg''' or '''final2.png'''.  Submit both to the same URL indicated in Problem #1.
+
* Note: To change the color of a '''line''' with the graphics library, you should use the '''setOutline()''' method rather than the '''setFill()''' method.  To change the '''width''' of a line, you can use the '''setWidth()''' method.
* Note: To change the color of a '''line''' with the graphics library, you should use the '''setOutline()''' method rather than the '''setFill()''' method.
+
* I used only 3 colors to generate the tree above: "green", "blue", and "brown".
* I used only 3 colors to generate the tree above: "orange", "blue", and "brown".
 
 
<br />
 
<br />
 +
::<source lang="python">
 +
# fractalTree.py
 +
# D. Thiebaut
 +
#
 +
# Draws a fractal tree on the graphic window.
 +
#
 +
from graphics import *
 +
import math
 +
import time
 +
import random
 +
 +
# dimensions of the window
 +
MAXWIDTH = 800
 +
MAXHEIGHT = 700
 +
 +
# recursive tree-drawing function
 +
#
 +
def draw_tree(win  ,      # the canvas
 +
              order,        # the level of recursion.  Starts positive.
 +
              theta,        # angle of new branch leaving this trunk
 +
              sz,          # size of this branch
 +
              x, y,        # coordinates of base of this branch
 +
              heading,      # angle of direction of this branch
 +
              color        # color
 +
              ):   
 +
 +
  trunk_ratio = 0.29      # How big is the trunk relative to whole tree?
 +
  trunk = sz * trunk_ratio # length of trunk
 +
 +
  # compute x, y of end of the current branch
 +
  delta_x = trunk * math.cos(heading)
 +
  delta_y = trunk * math.sin(heading)
 +
  x2, y2 = x + delta_x, y + delta_y
 +
 +
  # draw current branch
 +
  branch = Line( Point( x, y), Point( x2, y2 ) )
 +
  branch.setWidth( 2 )
 +
  branch.setOutline( color )
 +
  branch.draw( win )
 +
 +
  # if this branch has sub-branches, then recurse
 +
  if order > 0:   
 +
     
 +
      # make the recursive calls to draw the two subtrees
 +
      newsz = sz*(1 - trunk_ratio)
 +
      draw_tree(win,
 +
                order-1, theta, newsz, x2, y2, heading-theta,
 +
                color )
 +
 +
      draw_tree(win,
 +
                order-1, theta, newsz, x2, y2, heading+theta,
 +
                color )
 +
     
 +
def drawName( win, name ):
 +
    ''' draws the programmer's name in a box, at the
 +
    bottom of the window'''
 +
 +
    r = Rectangle( Point( 300, win.getHeight()-35 ),
 +
                  Point( win.getWidth()-300, win.getHeight()-5 ) )
 +
    r.setFill( "yellow" )
 +
    r.setOutline( "black" )
 +
    r.draw( win )
 +
 +
    l = Text( Point( win.getWidth()//2, win.getHeight()-20 ),
 +
              name )
 +
    l.setFill( "black" )
 +
    l.draw( win )
 +
 +
# draw 1 tree in the middle of the screen, shooting straight up.
 +
def main():
 +
    win = GraphWin("CSC111 Final Exam", MAXWIDTH, MAXHEIGHT )
 +
 +
    # draw the fractal tree
 +
    theta = 0.55      # use 0.02 for tall skinny trees, 0.7 for fat trees
 +
    order = 9
 +
    draw_tree(win,
 +
              order,
 +
              theta,
 +
              MAXWIDTH*0.9, MAXWIDTH//2,
 +
              MAXHEIGHT-50,
 +
              -math.pi/2,
 +
              "magenta" )
 +
 +
    # draw programmer's name in a box
 +
    drawName( win, "Mickey Mouse" )
 +
       
 +
    # wait for user to click window
 +
    win.getMouse()
 +
    win.close()
 +
 +
main()
 +
 +
</source>
 
<br />
 
<br />
 
<br />
 
<br />
 
</onlydft>
 
</onlydft>

Revision as of 16:38, 26 April 2018

D. Thiebaut (talk) 11:39, 26 April 2018 (EDT)



...