Skip to content Skip to sidebar Skip to footer

Python: Draw Tangent Graph Using Math & Turtle Libraries

I was stuck on this task for several days. Although, the solution should be simple. I apply math and turtle libraries for drawing 3 graphs: sine, cosine and tangent with amplitude

Solution 1:

To show that it should work, below is my minimalist implementation of plotting sine, cosine and tangent using turtle graphics:

import math
from turtle import Turtle, Screen

RESOLUTION = 0.1

def plot(x_points, y_points):
    for i, y in enumerate(y_points):
        ifabs(y) <= 2.0:
            yertle.goto(x_points[i], y)
            yertle.pendown()
        else:
            yertle.penup()

    yertle.penup()

screen = Screen()
screen.setworldcoordinates(0, -1.5, 2 * math.pi / RESOLUTION, 1.5)

yertle = Turtle()
yertle.penup()

x = range(int(2 * math.pi / RESOLUTION))

yertle.color("blue")
plot(x, (math.cos(n * RESOLUTION) for n in x))

yertle.color("red")
plot(x, (math.sin(n * RESOLUTION) for n in x))

yertle.color("dark green")
plot(x, (math.tan(n * RESOLUTION) for n in x))

screen.exitonclick()

OUTPUT

enter image description here

My guess is you're not waiting long enough for tangent to plot, i.e. it's slowly plotting lots of points off the window and will eventually reappear on-screen. My code works around that issue.

Solution 2:

try about this. Near to work for me, no time to get better:

for angle in range(360): y=0

y = math.tan(math.radians(angle))
if y<1and y>-1:
    t.goto(angle, y * 200)

Solution 3:

With asipmtotas

for angle in range(360):
    t.penup()
    y = math.tan(math.radians(angle))

    if y<1 and y>-1:
        t.pendown()
        t.goto(angle, y * 200)
    else:
        t.penup()
        #t.pendown()
        t.goto(angle, 200)

Post a Comment for "Python: Draw Tangent Graph Using Math & Turtle Libraries"