2014-03-30 56 views
0

嘿,想要学习如何编写代码,而且我不能把这个练习画出来。 具体获取精确的y轴截取点。 给出的公式给出的X轴点,但我不知道如何获得Y轴点。寻找一个圆的X轴和Y轴截取点 - Python

练习:

输入:圆和y的半径 - 的线的截距。

输出:用具有给定y截距的窗口上的水平线绘制的圆。标记交点的两点。 打印交叉点的x值*公式:X =±√r^ 2 - Y 1 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 

回答

0

对于y坐标可以使用一个类似的公式:

Y =±SQRT (r^2 - x^2)

和做标记根源一切。

+0

谢谢,更新了代码,但标记仍然有些偏离 – StephenJ2027

+0

这很奇怪。在哪个方向关闭?这是否发生在所有点上? – Mathias711

+0

啊,我明白了。我不熟悉绘制这些点,但通常你应该给X和Y坐标。 xroot1和xroot2的相应Y坐标是当然的0,而不是yroot1或yroot2。其他yroot点也是如此。图片中应该有4个点(或更少,如果圆圈接触轴而不是十字)。 – Mathias711

0

你应该写这样的代码:

x = sqrt(r ** 2 - y ** 2) 

line = Line(Point(-10, 0), Point(10, yinter)) 
line.draw(win) 

Line(Point(-10,0) is wrong, it should read: 
Line(Point(-10,yinter). 

还设置Xroot1Xroot2 = 0-x=0, x=0

-1

尝试用替换表达

xroot1 = sqrt(radius * radius - yinter * yinter) 

xroot1 = sqrt(radius ** 2 - yinter ** 2) 
+0

这似乎没有区别,表达式是相同的。你为什么认为这将有助于OP的问题? – cdlane

0

我刚想出一个子程序来找到相交点,同时解决另一个Zelle-图形相关的SO问题。有很多方法可以简化的数学,但我周围发生的很长的路要走:

from graphics import * 

def intersection(center, radius, p1, p2): 

    """ find the two points where a secant intersects a circle """ 

    dx, dy = p2.x - p1.x, p2.y - p1.y 

    a = dx**2 + dy**2 
    b = 2 * (dx * (p1.x - center.x) + dy * (p1.y - center.y)) 
    c = (p1.x - center.x)**2 + (p1.y - center.y)**2 - radius**2 

    discriminant = b**2 - 4 * a * c 
    assert (discriminant > 0), 'Not a secant!' 

    t1 = (-b + discriminant**0.5)/(2 * a) 
    t2 = (-b - discriminant**0.5)/(2 * a) 

    return Point(dx * t1 + p1.x, dy * t1 + p1.y), Point(dx * t2 + p1.x, dy * t2 + p1.y) 

def main(win): 
    center = Point(0.0, 0.0) 

    # Enter radius 

    radius = float(input("Put in radius: ")) 

    # Draw circle and center dot 

    Circle(center, radius).draw(win) 
    Circle(center, 0.3).draw(win) 

    # Enter the y intercept of the line 
    yinter = float(input("Put in y intercept: ")) 

    # Draw line 

    p1, p2 = Point(-10.0, 0.0), Point(10.0, yinter) 
    Line(p1, p2).draw(win) 

    # Mark two points of intercept in red 

    for i, root in enumerate(intersection(center, radius, p1, p2), start=1): 
     print("Root {}:".format(i), root) 
     dot = Circle(root, 0.3) 
     dot.setFill('red') 
     dot.draw(win) 

win = GraphWin() 
win.setCoords(-10.0, -10.0, 10.0, 10.0) 

main(win) 

win.getMouse() 

输出

enter image description here

注意

你可以得到值输入端,不要产生割礼,例如半径为2,截距为8.你的代码没有考虑到这一点 - 如果它发生,上面的代码只会抛出断言错误。但是,您可以将其升级为可以捕获并修复的错误。