# Python Code to display COS, SIN waves and a Spiral

As we have been doing in previous posts, this is getting interesting. In this post I will be sharing some Python Code snippets to demonstrate use of Python Math Functions and create some beautiful patterns.

## Creating Rainbow Colored Spiral

The first program uses the mathematical functions to create a spiral. The two mathematical functions calculate X and Y values for the next point to plot the spiral. Here I have used one point (center of drawing area) as fixed end point of line. It will stay as the start of the spiral and will not change throughout drawing process. Each line will be drawn from this fixed point.

The X and Y coordinates generated by spiral functions will be calculated in each iteration. This will be the second end point of the line. When these lines are created in the loop by resetting the color in iteration, a rainbow colored filled spiral pattern will be created.

#### Python Code

```import random
import time
import math
from graphics import *

def main():
# generate list of colors
col_arr=["violet","indigo","blue","green",
"yellow","orange","red","pink",
"brown","purple","gray","maroon",
"black"]
workArea = GraphWin('Spiral Pattern ', 300, 300) # give title and dimensions

i=0
while i<=360:
x = i* math.cos(i*math.pi/180)
y = i*math.sin(i*math.pi/180)

rcir=Line(Point(150, 150), Point(150+x, 150+y))# create line object with one end fixed at one point and other end pointed generated
rcir.setOutline(col_arr[i%13]) #change color
rcir.setWidth(i%5)#change width of outline
rcir.draw(workArea)#draw line
time.sleep(0.01)
i+=1

message = Text(Point(workArea.getWidth()/2, 250), 'Click to Exit')
message.draw(workArea)
workArea.getMouse()# get mouse to click on screen to exit
workArea.close() # close the workArea window

main()
```

## Creating Cosine Wave

This program uses the mathematical Cosine Function to create a Cosine wave. This Cos function is passed the current iteration counter used as X coordinate. It returns Y value for the next point in plotting the Cosine wave. Here I have used iteration counter as X coordinate and newly calculated Y value to get the second end point of line.

The X and Y coordinates generated this way will be updated in each iteration. The lines are created by resetting the color between the points (X value equal to counter, Y=150) and (X value equal to counter, Y= 150+Cos value generated with counter value).

#### Python Code

```import random
import time
import math
from graphics import *

def main():
# generate list of colors
col_arr=["violet","indigo","blue","green",
"yellow","orange","red","pink",
"brown","purple","gray","maroon",
"black"]
workArea = GraphWin('Cosine Wave Pattern', 720, 300) # give title and dimensions

i=0
while i<=720:
x = math.cos((i%360)*math.pi/180)
rlin=Line(Point(i, 150), Point(i, 150+100*x))
rlin.setOutline(col_arr[i%13]) #change color
rlin.setWidth(i%5)#change width of outline
rlin.draw(workArea)#draw line at generated points
time.sleep(0.01)
i+=1

message = Text(Point(workArea.getWidth()/2, 250), 'Click to Exit')
message.draw(workArea)
workArea.getMouse()# get mouse to click on screen to exit
workArea.close() # close the workArea window

main()
```

## Creating Sine Wave

This program uses the mathematical Sine Function to create a Sine wave. This Sin function is passed the current iteration counter used as X coordinate. The Sine function returns Y value for the next point in plotting the Sine wave. Here I have used iteration counter as X coordinate and newly calculated Y value to get the second end point of line.

The X and Y coordinates generated this way will be updated in each iteration. The lines are created by resetting the color between the points (X value equal to counter, Y=150) and (X value equal to counter, Y= 150+Sin value generated with counter value).

#### Python Code

```import random
import time
import math
from graphics import *

def main():
# generate list of colors
col_arr=["violet","indigo","blue","green",
"yellow","orange","red","pink",
"brown","purple","gray","maroon",
"black"]
workArea = GraphWin('Sine Wave', 720, 300) # give title and dimensions

i=0
while i<=720:
x = math.sin((i%360)*math.pi/180)
rlin=Line(Point(i, 150), Point(i, 150+100*x))
rlin.setOutline(col_arr[i%13]) #change color
rlin.setWidth(i%5)#change width of outline
rlin.draw(workArea)#draw line at genereated points
time.sleep(0.01)
i+=1

message = Text(Point(workArea.getWidth()/2, 250), 'Click to Exit')
message.draw(workArea)
workArea.getMouse()# get mouse to click on screen to exit
workArea.close() # close the workArea window

main()
```