Assignment 6 – Polar Coordinate Guides

Dean Zeller Due: Thursday, March 13th

CS10061 10 points

Spring, 2008

Objective The student will create scalable and paramaterizable graphic procedures using polar coordinates and following a set of design specs.

Background: Trigonometry

The underlying purpose of this series of lessons is to test the viability of using graphics programming as a method of teaching middle-school mathematics. This technique uses the trigonometric functions sine and cosine to determine location of points on a circle. The diagram of the unit circle (a circle with a radius of one) indicates how sine and cosine can be used to determine a particular point on the circle.

Within the unit circle diagram, the location of point (x,y) is:

(x,y) = (cosq, sinq)

If q is 60°, then (x,y) is:

(x,y) = (cos 60°, sin 60°) = (.500, .866)

Accounting for the center and radius of the circle, the location of a point (x,y) is:


Calculating Polar Guides

The Python functions for sine and cosine require an angle measurement in radians, so values will have to be converted. The following python code diagram indicates how to create a series of guides to draw a pentagon or star. When programming, the only code that needs to be changed is the angles in bold; the remaining programming is the same.

p0 = [centerX + cos( 0/180.0*pi)*radiusX, centerY - sin( 0/180.0*pi)*radiusY]

p1 = [centerX + cos( 72/180.0*pi)*radiusX, centerX - sin( 72/180.0*pi)*radiusY]

p2 = [centerX + cos(144/180.0*pi)*radiusX, centerX - sin(144/180.0*pi)*radiusY]

p3 = [centerX + cos(216/180.0*pi)*radiusX, centerX - sin(216/180.0*pi)*radiusY]

p4 = [centerX + cos(288/180.0*pi)*radiusX, centerX - sin(288/180.0*pi)*radiusY]

Instructions

Create at least two graphic procedures implementing these polar coordinate guides, similar to the template below. Following the design specification rules from the previous assignment. You may use polar coordinate guides in your drawing in combination with vertical and horizontal guides.

Picture Ideas

  1. Objects from the requested object list in the story presentations (sun, beach ball, moon…)
  2. Regular polygons (see Activity 2 handout)
  3. Line art (see Activity 4 handout)

Turning in your assignment

·  Name your program file using the following format: Assign6Lastname.py. For example, your instructor would name his Assign6Zeller.py.

·  Test your procedure following the format of the template.

·  Email your program as an attachment to the library supervisors with a carbon copy to the instructor.
(To: , Cc: )

·  Present your objects to the class.

Grading

You will be graded on the following criteria:

Effort Quantity and complexity of the guides and pictures

Format Correctly following the design specifications (including polar coordinates)

Reusability The extent to which a programmer can create the drawing through a flexible set of parameters

Creativity Imagination and aesthetic quality of the pictures

Assign6Template.py

###########################################################################

# #

# <Enter your name here> #

# Assignment 6 -- Polar Coordinate Guides #

# Date: <date turned in> #

# #

# The following is a series of pictures drawn using the Tkinter graphics #

# library. Specific titles, author information, and descriptions are #

# provided. The pictures are parameterizable for position, size, and #

# optional colors. For this program, guides are created using polar #

# coordinates. #

# #

###########################################################################

from Tkinter import *

from math import *

###########################################################################

# pause_n_clear #

# #

# Author: Dean Zeller (02-25-08) #

# Purpose: clear the canvas and set a new background #

# Parameters: #

# c -- canvas to clear #

# color -- new background color (optional, default is white) #

###########################################################################

def pause_n_clear(c, color='white', message="Hit Enter to continue..."):

raw_input(message)

c.delete(ALL) #delete all objects on canvas

c.create_rectangle(-10,-10,1000,1000,fill=color) #create new background

###########################################################################

########################### Picture Functions ###########################

###########################################################################

###########################################################################

# draw_pentagon1 #

# #

# Author: Dean Zeller (02-25-08) #

# Purpose: Draws a pentagon #

# Parameters: #

# c -- canvas to draw pentagon #

# left, bottom -- the bottom-left corner of the pentagon #

# width, height -- the width and height of the pentagon #

# Optional parameters: #

# color -- color of the pentagon #

###########################################################################

def draw_pentagon1(c, left, bottom, width, height, color='green2'):

# calculate horizontal and vertical guides

right = left + width

centerX = (left + right)/2.0

top = bottom - height

centerY = (top + bottom)/2.0

radiusX = width/2.0

radiusY = height/2.0

# calculate points of the pentagon using sine and cosine

p0 = [centerX + cos( 0/180.0*pi)*radiusX, centerY - sin( 0/180.0*pi)*radiusY]

p1 = [centerX + cos( 72/180.0*pi)*radiusX, centerY - sin( 72/180.0*pi)*radiusY]

p2 = [centerX + cos(144/180.0*pi)*radiusX, centerY - sin(144/180.0*pi)*radiusY]

p3 = [centerX + cos(216/180.0*pi)*radiusX, centerY - sin(216/180.0*pi)*radiusY]

p4 = [centerX + cos(288/180.0*pi)*radiusX, centerY - sin(288/180.0*pi)*radiusY]

# draw pentagon and encompasing circle

c.create_oval(left,top,right,bottom)

c.create_polygon(p0, p1, p2, p3, p4, fill=color)


###########################################################################

# draw_pentagon2 #

# #

# Author: Dean Zeller (02-25-08) #

# Purpose: Draws a pentagon, similar to draw_pentagon1, except with the #

# first angle of 18 to correct the tilting. #

# Parameters: #

# c -- canvas to draw pentagon #

# left, bottom -- the bottom-left corner of the pentagon #

# width, height -- the width and height of the pentagon #

# Optional parameters: #

# color -- color of the pentagon #

###########################################################################

def draw_pentagon2(c, left, bottom, width, height, color='green2'):

# calculate horizontal and vertical guides

right = left + width

centerX = (left + right)/2.0

top = bottom - height

centerY = (top + bottom)/2.0

radiusX = width/2.0

radiusY = height/2.0

# calculate points of the pentagon using sine and cosine

p0 = [centerX + cos( 18/180.0*pi)*radiusX, centerY - sin( 18/180.0*pi)*radiusY]

p1 = [centerX + cos( 90/180.0*pi)*radiusX, centerY - sin( 90/180.0*pi)*radiusY]

p2 = [centerX + cos(162/180.0*pi)*radiusX, centerY - sin(162/180.0*pi)*radiusY]

p3 = [centerX + cos(234/180.0*pi)*radiusX, centerY - sin(234/180.0*pi)*radiusY]

p4 = [centerX + cos(306/180.0*pi)*radiusX, centerY - sin(306/180.0*pi)*radiusY]

# draw pentagon and encompasing circle

c.create_oval(left,top,right,bottom)

c.create_polygon(p0, p1, p2, p3, p4, fill=color)

###########################################################################

# draw_star #

# #

# Author: Dean Zeller (02-25-08) #

# Purpose: Draws a star #

# Parameters: #

# c -- canvas to draw pentagon #

# left, bottom -- the bottom-left corner of the star #

# width, height -- the width and height of the star #

# Optional parameters: #

# color -- color of the star #

###########################################################################

def draw_star(c, left, bottom, width, height, color='green2'):

# calculate horizontal and vertical guides

right = left + width

centerX = (left + right)/2.0

top = bottom - height

centerY = (top + bottom)/2.0

radiusX = width/2.0

radiusY = height/2.0

# calculate points of the pentagon using sine and cosine

p0 = [centerX + cos( 18/180.0*pi)*radiusX, centerY - sin( 18/180.0*pi)*radiusY]

p1 = [centerX + cos( 90/180.0*pi)*radiusX, centerY - sin( 90/180.0*pi)*radiusY]

p2 = [centerX + cos(162/180.0*pi)*radiusX, centerY - sin(162/180.0*pi)*radiusY]

p3 = [centerX + cos(234/180.0*pi)*radiusX, centerY - sin(234/180.0*pi)*radiusY]

p4 = [centerX + cos(306/180.0*pi)*radiusX, centerY - sin(306/180.0*pi)*radiusY]

# draw pentagon and encompasing circle

c.create_oval(left,top,right,bottom)

c.create_polygon(p0, p2, p4, p1, p3, fill=color)


###########################################################################

# draw_clock #

# #

# Author: Dean Zeller (02-25-08) #

# Purpose: Draws a clock with Roman numerals #

# Parameters: #

# c -- canvas to draw pentagon #

# left, bottom -- the bottom-left corner of the star #

# width, height -- the width and height of the star #

# Optional parameters: #

# facecolor -- color of the clock face #

# numbercolor -- color of the clock numerals #

# Bugs: At this point, the size of the text cannot be changed. #

###########################################################################

def draw_clock(c, left, bottom, width, height, facecolor='white', numbercolor='black'):

# calculate horizontal and vertical guides

right = left + width

centerX = (left + right)/2.0

top = bottom - height

centerY = (top + bottom)/2.0

radiusX = width/2.0 * .85

radiusY = height/2.0 * .85

# calculate points of the pentagon using sine and cosine

p1 = [centerX + cos( 60/180.0*pi)*radiusX, centerY - sin( 60/180.0*pi)*radiusY]

p2 = [centerX + cos( 30/180.0*pi)*radiusX, centerY - sin( 30/180.0*pi)*radiusY]

p3 = [centerX + cos( 0/180.0*pi)*radiusX, centerY - sin( 0/180.0*pi)*radiusY]

p4 = [centerX + cos( -30/180.0*pi)*radiusX, centerY - sin( -30/180.0*pi)*radiusY]

p5 = [centerX + cos( -60/180.0*pi)*radiusX, centerY - sin( -60/180.0*pi)*radiusY]

p6 = [centerX + cos( -90/180.0*pi)*radiusX, centerY - sin( -90/180.0*pi)*radiusY]

p7 = [centerX + cos(-120/180.0*pi)*radiusX, centerY - sin(-120/180.0*pi)*radiusY]

p8 = [centerX + cos(-150/180.0*pi)*radiusX, centerY - sin(-150/180.0*pi)*radiusY]

p9 = [centerX + cos(-180/180.0*pi)*radiusX, centerY - sin(-180/180.0*pi)*radiusY]

p10 = [centerX + cos(-210/180.0*pi)*radiusX, centerY - sin(-210/180.0*pi)*radiusY]

p11 = [centerX + cos(-240/180.0*pi)*radiusX, centerY - sin(-240/180.0*pi)*radiusY]

p12 = [centerX + cos(-270/180.0*pi)*radiusX, centerY - sin(-270/180.0*pi)*radiusY]

# draw face and numbers

c.create_oval(left,top,right,bottom, fill=facecolor)

c.create_text( p1, text="I", anchor=CENTER, fill=numbercolor)

c.create_text( p2, text="II", anchor=CENTER, fill=numbercolor)

c.create_text( p3, text="III", anchor=CENTER, fill=numbercolor)

c.create_text( p4, text="IV", anchor=CENTER, fill=numbercolor)

c.create_text( p5, text="V", anchor=CENTER, fill=numbercolor)

c.create_text( p6, text="VI", anchor=CENTER, fill=numbercolor)

c.create_text( p7, text="VII", anchor=CENTER, fill=numbercolor)

c.create_text( p8, text="VIII",anchor=CENTER, fill=numbercolor)

c.create_text( p9, text="IX", anchor=CENTER, fill=numbercolor)

c.create_text(p10, text="X", anchor=CENTER, fill=numbercolor)

c.create_text(p11, text="XI", anchor=CENTER, fill=numbercolor)

c.create_text(p12, text="XII", anchor=CENTER, fill=numbercolor)


###########################################################################

# draw_ping_pong #

# #

# Author: Dean Zeller (02-25-08) #

# Purpose: Draws a ping pong paddle with a ball #

# Parameters: #

# c -- canvas to draw pentagon #

# left, bottom -- the bottom-left corner of the star #

# width, height -- the width and height of the star #

# Optional parameters: #

# facecolor -- color of the paddle face #

# handlecolor -- color of the paddle handle #

# ballcolor -- color of the ping pong ball #

###########################################################################

def draw_ping_pong(c,left,bottom,width,height,

facecolor='red', handlecolor='tan',

ballcolor='white'):

# set up vertical and horizontal guides

gridX = width / 7.0

x0 = left

x1 = left + gridX

x2 = left + 2*gridX

x3 = left + 3*gridX

x4 = left + 4*gridX

x5 = left + 5*gridX

x6 = left + 6*gridX

gridY = height / 5.0

y0 = bottom

y1 = bottom - gridY

y2 = bottom - 2*gridY

y3 = bottom - 3*gridY

y4 = bottom - 4*gridY

y5 = bottom - 5*gridY

# set up polar guides for ball rays

radiusX = 1.2 * gridX

radiusY = 1.2 * gridY

angle = 360/7.0

p0 = [x6 + cos(0*angle/180.0*pi)*radiusX, y4 + sin(0*angle/180.0*pi)*radiusY]

p1 = [x6 + cos(1*angle/180.0*pi)*radiusX, y4 + sin(1*angle/180.0*pi)*radiusY]

p2 = [x6 + cos(2*angle/180.0*pi)*radiusX, y4 + sin(2*angle/180.0*pi)*radiusY]

p3 = [x6 + cos(3*angle/180.0*pi)*radiusX, y4 + sin(3*angle/180.0*pi)*radiusY]

p4 = [x6 + cos(4*angle/180.0*pi)*radiusX, y4 + sin(4*angle/180.0*pi)*radiusY]

p5 = [x6 + cos(5*angle/180.0*pi)*radiusX, y4 + sin(5*angle/180.0*pi)*radiusY]

p6 = [x6 + cos(6*angle/180.0*pi)*radiusX, y4 + sin(6*angle/180.0*pi)*radiusY]

# draw paddle

c.create_rectangle(x3,y3, x4,y0, fill=handlecolor)

c.create_oval(x2,y5, x5,y2, fill=facecolor)

# draw ball rays

c.create_line(x6, y4, p0)

c.create_line(x6, y4, p1)

c.create_line(x6, y4, p2)

c.create_line(x6, y4, p3)

c.create_line(x6, y4, p4)

c.create_line(x6, y4, p5)

c.create_line(x6, y4, p6)

# draw ping pong ball

ballwidth=.75*gridX

ballheight=.75*gridY

c.create_oval(x6-ballwidth/2, y4-ballheight/2,

x6+ballwidth/2, y4+ballheight/2, fill=ballcolor)


###########################################################################

############################# Main Program ##############################

###########################################################################

c = Canvas(width=900, height=600, bg='white')

c.pack(expand=YES, fill=BOTH)

pause_n_clear(c, message="Tilted Pentagons, by Dean Zeller")

draw_pentagon1(c, 10, 300, 250, 250, color="green")

draw_pentagon1(c, 300, 300, 250, 100, color="blue")

draw_pentagon1(c, 250, 50, 45, 30, color="orange")

draw_pentagon1(c, 100, 200, 75, 75, color="pink")

draw_pentagon1(c, 300, 150, 350, 50, color="red")

pause_n_clear(c, message="Fixed Pentagons, by Dean Zeller")

draw_pentagon2(c, 10, 300, 250, 250, color="green")

draw_pentagon2(c, 300, 300, 250, 100, color="blue")

draw_pentagon2(c, 250, 50, 45, 30, color="orange")

draw_pentagon2(c, 100, 200, 75, 75, color="pink")

draw_pentagon2(c, 300, 150, 350, 50, color="red")

pause_n_clear(c, message="Stars, by Dean Zeller")

draw_star(c, 10, 300, 250, 250, color="green")

draw_star(c, 300, 300, 250, 100, color="blue")

draw_star(c, 250, 50, 45, 30, color="orange")

draw_star(c, 100, 200, 75, 75, color="pink")

draw_star(c, 300, 150, 350, 50, color="red")

pause_n_clear(c, message="Clocks, by Dean Zeller")

draw_clock(c, 10, 300, 250, 250, facecolor="green", numbercolor="black")

draw_clock(c, 300, 300, 250, 100, facecolor="blue", numbercolor="yellow")

draw_clock(c, 250, 50, 45, 30, facecolor="orange", numbercolor="brown")

draw_clock(c, 100, 200, 75, 75, facecolor="pink", numbercolor="purple3")

draw_clock(c, 300, 150, 350, 50, facecolor="red", numbercolor="white")

pause_n_clear(c, message="Ping Pong, by Dean Zeller")

draw_ping_pong(c, 10, 300, 250, 250)

draw_ping_pong(c, 300, 300, 250, 100, facecolor='green', handlecolor='white')

draw_ping_pong(c, 250, 50, 45, 30, handlecolor='black', facecolor='grey70')

draw_ping_pong(c, 300, 150, 350, 50, ballcolor='salmon', facecolor='light blue')