3 回答

TA貢獻1900條經驗 獲得超5個贊
“很有可能”,它可以做得更簡單(例如,沒有 x2 和 y2 變量,但我不確定)
如果我們的目標是使用 turtle 繪制一個簡單的線函數,我們可以更簡單:
from turtle import Turtle, Screen
from math import pi, sin
def draw_wave(frequency=1):
angle = 0
while angle < 2 * pi:
turtle.goto(angle, sin(angle * frequency))
angle += 0.05
screen = Screen()
screen.setworldcoordinates(0, -1.25, 2 * pi, 1.25)
turtle = Turtle()
draw_wave(2)
turtle.hideturtle()
screen.exitonclick()
然后根據需要修飾(斧頭等)。

TA貢獻1995條經驗 獲得超2個贊
我不確定我是否理解“折線圖”、https://en.wikipedia.org/wiki/Line_graph或https://en.wikipedia.org/wiki/Line_chart是什么意思?對于第二種情況,例如,對于 sinus 函數,可以使用以下簡化代碼來完成。
import turtle as tr
import math as m
x0, y0 = -300, 275 # The point near the upper left corner of the Turtle screen - virtual origin of coordinates
Y0 = -y0 + 100 # The reference vertical coordinate for the second function
A0 = 100 # The amplitude of sinus function
f0 = 80 # 1/frequency (reverse frequency)
def draw1():
x1 = 0
y1 = A0 - A0 * m.sin(x1/f0)
tr.goto(x0 + x1, y0 - y1)
tr.down()
tr.dot(size = 1)
for x2 in range(abs(x0)*2):
y2 = A0 - A0 * m.sin(x1/f0)
tr.goto(x0 + x2, y0 - y2)
tr.dot(size = 1)
x1, y1 = x2, y2
def draw2(f0):
x1 = 0
y1 = Y0 + A0 * m.sin(x1/f0)
tr.goto(x0 + x1, y1)
tr.down()
tr.dot(size = 1)
for x2 in range(abs(x0)*2):
y2 = Y0 + A0 * m.sin(x1/f0)
tr.goto(x0 + x2, y2)
tr.dot(size = 1)
x1, y1 = x2, y2
tr.speed('fastest')
tr.up()
tr.goto(x0, y0)
tr.hideturtle()
tr.color('red')
draw1() # The pivot point - the virtual origin of coordinates (x0 and y0)
tr.up()
tr.goto(x0,y0)
tr.color('blue')
draw2(f0/2) # The pivot point - x0 and Y0
input() # waiting for the <Enter> press in the console window
“很有可能”,它可以做得更簡單(例如,沒有x2和y2變量,但我不確定) - 我從 Tkinter 畫布上繪制這個方法。而且這種方法可能適用于第一種情況(當然,需要進行某種修改)。

TA貢獻1780條經驗 獲得超5個贊
我在想你想繪制線性方程:mx + b
import turtle as t
a = t.window_width() / 2
def graph(m_x, m_y, b):
x = [i for i in range(int(-a), int(a), m_x)]
return list(zip(x, map(lambda x_val: ((m_x / m_y) * x_val) + b, x)))
for x, y in graph(1, 10, 0):
t.goto(x, y)
t.mainloop()
添加回答
舉報