嘿,我想学怎么编码,我搞不懂这个练习。 特别是得到精确的y轴截距点。 给出的公式可以得到x轴的点,但我不知道如何得到y轴的点。在
练习:
输入:圆的半径和直线的y轴截距。在
输出:在给定的y轴截距下,用水平线画出的圆。标出两个交点。 打印交点的x值*公式:x=±√r^2-y^2
Code::
from graphics import *
from math import *
def main():
# enter radius and the y intercept of the line
radius = eval(input("Put in radius:: "))
yinter = eval(input("Put in y intersec:: "))
#Draw window + circle + line
win = GraphWin()
win.setCoords(-10.0, -10.0, 10.0, 10.0)
circle = Circle(Point(0.0,0.0), radius)
mcircle = Circle(Point(0.0,0.0), 0.5)
circle.draw(win)
mcircle.draw(win)
line = Line(Point(-10, 0), Point(10, yinter))
line.draw(win)
#Calculate x axis points of intersept
xroot1 = sqrt(radius * radius - yinter * yinter)
xroot2 = -abs(xroot1)
print("Xroot 1 : ", xroot1)
print("Xroot 2 : ", xroot2)
x = 0
yroot1 = sqrt(radius * radius - x * x)
yroot2 = -abs(yroot1)
print("Yroot 1 : ", yroot1)
print("Yroot 2 : ", yroot2)
#mark two points of intersept in red
sc1 = Circle(Point(xroot1, yroot1), 0.3)
sc1.setFill('red')
sc2 = Circle(Point(xroot2, yroot2), 0.3)
sc2.setFill('red')
sc1.draw(win)
sc2.draw(win)
main()
Answer - With Radius of 8 and Y intersect point of 2
Yroot1 = 7.75
Yroot2 = -7.75
Xroot1 = 8.0
Xroot2 = -8.0
我刚刚想出了一个子程序来寻找交点,同时解决另一个Zelle图形相关的SO问题。也许有一些方法可以简化数学,但我要做的是:
输出
注意
您可以得到不产生正割的输入值,例如半径为2,截距为8。您的代码没有考虑到这一点,如果发生上述错误,则会抛出一个断言错误。但是你可以把它升级为一个可以捕捉并修复的错误。在
y坐标可以用于类似的公式:
y=±sqrt(r^2-x^2)
他们做的一切都和标记根一样。在
你应该这样写代码:
同时设置}
Xroot1
和Xroot2 = 0
或{相关问题 更多 >
编程相关推荐