找到一个圆的X和Y轴线截距点 - Python

时间:2014-03-30 18:03:15

标签: python math geometry line zelle-graphics

嘿试图学习如何编码,我不能想出这个练习。 具体获取精确的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

3 个答案:

答案 0 :(得分:0)

对于y坐标,您可以使用类似的公式:

y =±sqrt(r ^ 2 - x ^ 2)

并且标记根部的所有事情都是一样的。

答案 1 :(得分: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

答案 2 :(得分: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.您的代码没有考虑到这一点 - 如果发生断言,上面只会抛出一个断言错误。但您可以将其升级为可以捕获和修复的错误。