Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 6 Current »

The purpose of this project was to get acquainted with the tools for writing python code and do some programming. Using the "turtle" graphics library, I began to familiarize myself with python's syntax and commands. Ultimately I wrote a program that employs functions, a powerful tool that lets me to turn an arbitrary set of commands into a single command. 

This is the program I used: Python IDLE for windows. Gonna use the library macs in the future. 

After the octagon I got a little more ambitious by programming a cube shape. Here's a picture:

The first command is always to import the turtle library. All of the forward, turning, up and down commands refer to functions defined in that library. Once turtle is imported, drawing shapes is easy. Its just a matter of moving the pen forward by x pixels and turning by y degrees. You can also lift the pen up with the up() command and put it back down with down(). Pretty straight forward. 

Here I drew a cube you can see inside of. cooool

Here we see some more interesting commands, namely the def command for defining a function. A function is a set of instructions that I've told python to recognize by a single command. For example, shapeC(), the first function in the picture above, does everything in the indented black lines of code below it.  After defining the function I only have to type shapeC() and python will execute all of those indented instructions. Functions can even be used as commands contained in another function, as I did by embedding shapeA() in shapeC(). 

Another aspect of python functions is the ability to add a parameter. sideLength in the function shapeD is its parameter. When I use the function shapeD, as I do in defining shapeE(), I specify an integer value for sideLength. Then when python refers to the lines of code defining shapeD, it will insert that integer value into every place I've used the sideLength parameter, namely the parts where it draws the sides of the star. Ultimately I can draw stars by simply typeing shapeD(x), with x being the number of pixels I want the side length to be. 

The function shapeE() shows how functions can be nested into other functions to simplify otherwise complex programming tasks. Drawing those 7 stars would take many lines of code, but with functions I can pare it down to defining a star once in shapeD and then iterating it seven times in shapeE. 

thats a picture of my extension. I decided to attack the suggested problem that was making program to draw an n-sided polygon. Then I decided to complicate matters by making it draw a star instead. I didn't anticipate how complicated drawing stars would be, but ultimately I got a working program with 3 for loops, some ugly math and an if-else thingy. 

From my shapes program, I knew that drawing a 5 sided star was simple. I could generalize the code to draw a star of (what I thought would be) any number of sides by making a for loop. The for loop allowed me to make python repeat lines of code a certain number of times, in this case drawing lines and turning to make an n-pointed star. If I want a 5 point star the code inside the loop repeats 5 times, for a 7 pointed star it repeats 7 times. 

I soon realized that the for loop would not work for even numbers of points. Instead of drawing the star of david I wanted, python would draw two triangles right on top of each other. I had to rotate the second triangle somehow. I looked up an if-then statement to execute some code if the sides requested is odd and different code if the sides requested is even. The remainder operation was useful for testing whether the sides requested was odd or even. Then I wrote another couple of for loops for drawing the 1st and 2nd overlapping polygons that make up even-pointed stars. Off-setting the second polygon proved the trickiest part of the whole process. I'm embarrassed to admit that it took over an hour of math and especially debugging to make it work. All for a uninteresting star. But it wasn't all bad, I re familiarized myself with the law of cosines and let wolfram alpha do the heavy lifting. I'm a math minor, ok. 

In case you're curious, here's the formula for how much the point of a n-sided star protrudes from the polygon it rests on:

which, typed into python, looks something like this: ((2.0 - sqrt(2.0 - (2.0 * cos(math.radians(180 - (720.0 / n)))))) / ((2.0 * cos(math.radians(180 - (720.0 / n)))) + 2.0))

I learned a few things on this project. Most importantly, that the internet is my friend for all questions about programming. Second, to make math work normally you must append .0 to every number. Third, that even the simplest projects can proliferate into hugely complex undertakings. I only hope no one points out a dead-simple solution to drawing the even sided star. 

  • No labels