How to make a cosine wave graph in Python turtle?
Last Updated :
19 Aug, 2022
In this article, we will learn how to draw a Cosine wave and the inverse of a cosine wave using a turtle in Python.
What is Cosine?
The Cosine function, also written as cos or cos(x), reduces the hypotenuse of a right triangle to the projection onto the x-axis. cosine signal waveform with a shape identical to that of a sine wave it’s occurring exactly before one by four(1/4) cycle of the sine wave.
Cos θ = Adjacent side/Hypotenuse
The Cosine graph, and their degree
degree |
convert to radians |
Cos x |
0 |
0 |
1 |
30 |
Ï€/6 |
√3/2 |
45 |
Ï€/4 |
√1/2 |
60 |
Ï€/3 |
1/2 |
90 |
Ï€/2 |
0 |
Cos Waveform:
Example 1: Generating Cosine wave
In this example, we will import the required module and set the coordination, after that we will draw vertical and horizontal lines to draw our cosine wave.
Python3
import math
import turtle
win = turtle.Screen()
win.bgcolor( "white" )
win.setworldcoordinates( 0 , - 2 , 3600 , 2 )
t = turtle.Turtle()
t.goto( 0 , 2 )
t.goto( 0 , - 2 )
t.goto( 0 , 0 )
t.goto( 3600 , 0 )
t.penup()
t.goto( 0 , 1 )
t.pendown()
t.pencolor( "blue" )
t.pensize( 4 )
for x in range ( 3600 ):
y = math.cos(math.radians(x))
t.goto(x, y)
|
Output:
What is Inverse Cosine Wave?
Inverse cosine is also known as arccosine. It is reciprocal of the Cosine wave. Cosine inverse of the same ratio will give the measure of the angle, y= cos -1(x) <=> cos y = x. Here, the cosine function is equal to the Adjacent side divided by the hypotenuse, and Each range value between -1 to 1 is within the limited domain (0,180).
θ = Cos -1(Adjacent side/hypotenuse)
The Inverse Cosine graph, and their degree:
y |
0 |
Ï€/6 |
Ï€/3 |
Ï€/2 |
2Ï€/3 |
5Ï€/6 |
Ï€ |
x=cos-1 y |
1 |
√3/2 |
√1/2 |
0 |
-√1/2 |
-√3/2 |
-1 |
Cos inverse Waveform:
Example 2: Inverse Cosine
In this example, we will import the required module and set the coordination, after that we will draw vertical and horizontal lines to draw our Inverse cosine wave.
Python3
import math
import turtle
win = turtle.Screen()
win.bgcolor( "white" )
win.setworldcoordinates( - 1 , - 180 , 1 , 180 )
t = turtle.Turtle()
t.goto( 1 , 0 )
t.goto( - 1 , 0 )
t.penup()
t.goto( 0 , 0 )
t.pendown()
t.goto( 0 , 180 )
t.goto( 0 , - 180 )
t.penup()
t.goto( 1 , 0 )
t.pendown()
t.pencolor( "blue" )
t.pensize( 4 )
for y in range ( 0 , 180 ):
x = math.cos(math.radians(y))
t.goto(x, y)
|
Output:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...