如何使用带有海龟图形的Python制作线性图形计算器?

时间:2019-02-14 18:52:58

标签: python turtle-graphics

为什么我尝试绘制图形时此代码不起作用? y拦截似乎不起作用。

from turtle import *

m = float(input("What is the slope? "))

b = float(input("What is the y-intercept? "))

x= window_width()

y= window_height()

y= int(m*x + b)

pd()

goto(x , y)

pd()

goto(-x,-y)

pu()

goto(x/2,0)

pd()

goto(-x/2,0)


pu()

goto(0,2*y)

pd()

goto(0,-2*y)

update()

当我用y截距测试值时,它们会绕过原点,这意味着它不起作用。我正在尝试进行y拦截。

1 个答案:

答案 0 :(得分:0)

  

为什么我尝试绘制图形时此代码不起作用?

我看到两个问题:1)您似乎在以错误的顺序进行操作; 2)您错误地假设,如果y为f(x),则f(-x)为-y,这是不正确的:

from turtle import *

m = float(input("What is the slope? "))

b = float(input("What is the y-intercept? "))

x, y = window_width(), window_height()

# Draw Axes
penup()
goto(x / 2, 0)
pendown()
goto(-x / 2, 0)
penup()
goto(0, y / 2)
pendown()
goto(0, -y / 2)

# Plot function

y = int(m * x + b)

penup()
goto(x, y)

x = -x
y = int(m * x + b)

pendown()
goto(x, y)

done()

用法

> python3 test.py
What is the slope? 0.5
What is the y-intercept? 100
>

输出

enter image description here