Python:使用数学和amp;绘制切线图乌龟库 [英] Python: draw tangent graph using math & turtle libraries

查看:66
本文介绍了Python:使用数学和amp;绘制切线图乌龟库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这项任务上停留了几天.虽然,解决方案应该很简单.我将数学和乌龟库用于绘制3个图:正弦,余弦和正切,幅值为200.问题是我无法构建应绘制的正切图.这是我应该做的:

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 200. The problem is I cant build the tangent graph as It should be drawn. This is what I should do:

这就是我得到的:

如您所见,我的乌龟升起了,不再回来了.请不要建议我使用numpy.这是我的任务.谢谢你的进步!

As you see, my turtle goes up and doesn't come back anymore. Pls, don't suggest me to use numpy. It's out of my task. Thank you for advance!

import math
import turtle

ws = turtle.Screen()
ws.bgcolor("white")
t = turtle.Turtle()
for i in [(0,250), (0,0), (0,-250), (0,0), (400,0), (0,0)]:
    t.goto(i, None)
    t.write(i, font=("Arial", 12))

t.color("red")

for angle in range(360):
    y = math.sin(math.radians(angle))        
    t.goto(angle, y * 200)

t.penup()
t.setpos(0,200)
t.goto(0,200)
t.pendown()
t.color("blue")

for angle in range(360):
    y = math.cos(math.radians(angle))       
    t.goto(angle, y * 200)

t.penup()
t.setpos(0,0)
t.goto(0,0)
t.pendown()
t.color("green")

for angle in range(360):
    y = math.tan(math.radians(angle))
    t.goto(angle, y * 200)

ws.exitonclick()

推荐答案

为证明它应该工作,下面是我使用乌龟图形绘制正弦,余弦和切线的极简实现:

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):
        if abs(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()

输出

我的猜测是您没有等待足够长的时间来切线绘制,即它正在缓慢地在窗口上绘制许多点,最终会重新出现在屏幕上.我的代码可解决该问题.

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.

这篇关于Python:使用数学和amp;绘制切线图乌龟库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆