Saturday, July 7, 2012

CS4HS USask July 3-4th

Note:  I also attended the UTM CS4HS event later in the month!

I attended the University of Saskatchewan CS4HS session this past week.  It focused on Python.  I really find Python, PyDev and Eclipse to be a very nice environment for learning computer science.  I've been using Netbeans/Java SDK with the Blue Pelican textbook, but it hasn't been that great for me.  I installed the 64-bit Windows software on my Dell Inspiron this morning.  It was easy.  First, I downloaded Eclipse and installed it (classic version 4.2) - just unzips a folder.  Then I downloaded and installed Python.  I downloaded the 64-bit version 3.2.3.  Then I installed PyDev from within Eclipse.  Earlier this week, I tried to install PyDev with a download, but I couldn't get it working, so I figured it must be a plugin install within the IDE (sort of like how Netbeans does it).  I made a Google doc with my notes.

CS4HS are Google sponsored PD opportunities organised by universities for high school teachers.  One of the participants mentioned he was going to the University of Alberta CS4HS event this month.  I had signed up for the UT Mississauga CS4HS Workshop on July 23-25.  They are free, so that's great for me.

One of the things I liked was how you could use Turtle graphics to teach recursion.  We were asked to do one involving triangles.  Here's a simpler one (a box within a box within a box ...).

The code is pretty simple:

"""
 A program that uses turtle graphics to draw an 'H' fractal.
 
 The documentation for the Turtle graphics module is in
 section 23.1 'turtle -- Turtle graphics' of the Python
 Standard Library documentation at python.org.
"""

import turtle

# Global name for the turtle that will
# do all of the drawing
Leo = turtle.Turtle()

def drawLine(x1, y1, x2, y2):
    """
    Use the global turtle, 'Leo', to draw a line
    from (x1,y1) to (x2,y2)
    """
    global Leo
    Leo.penup()
    Leo.setposition(x1, y1)
    Leo.pendown()
    Leo.setposition(x2, y2)
    Leo.penup()
   
def drawBox(N, size, x, y):
    """
    Recursively draw N 'H' fractals centered at (x,y)
    Input:
      N -- an integer indicating maximum recursive depth
      size -- size (width), in pixels, to draw the 'H'
      x,y -- integers. The coordinates of the center of the 'H'
    """
    if N > 0:
        halfSize = size // 1.2
        leftX, rightX = x - halfSize, x + halfSize
        topY, bottomY = y + halfSize, y - halfSize

        drawLine(leftX,  bottomY, leftX,  topY)
        drawLine(leftX,  topY,    rightX, topY)
        drawLine(rightX, topY, rightX, bottomY)
        drawLine(rightX, bottomY, leftX,bottomY )
        #3 triangles per triangle
        # top, bottom left, bottom right
        drawBox(N-1, halfSize, x ,  y)
        #drawT(N-1, halfSize, rightX, bottomY)
       

if __name__ == '__main__':
    # Set the ninja turtle's speed to 'fast'
    Leo.speed("fast")
    # Draw the H fractal centered at the center of the screen
    drawBox(20, 300, 0, 0)
    # Leave the window up until the user clicks in it
    Leo.getscreen().exitonclick()

No comments:

Post a Comment