繁体   English   中英

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

[英]How do I make a linear graphing calculator in Python with 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)您似乎在以错误的顺序进行操作; 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
>

OUTPUT

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM