在Python中寻找圆的X轴和Y轴截距点

1 投票
4 回答
4440 浏览
提问于 2025-04-18 00:34

嘿,我正在学习编程,但这个练习我搞不明白。特别是如何找到准确的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

4 个回答

0

在编程中,有时候我们需要处理一些数据,比如从一个地方获取数据,然后在另一个地方使用这些数据。这个过程就像是把水从一个水桶倒到另一个水桶里。

当我们提到“异步”时,其实是在说我们可以在等待某些事情完成的时候,去做其他的事情。就好比你在等水烧开的时候,可以先去切菜,而不是一直盯着锅看。

而“回调”就是一种方法,告诉程序在某件事情完成后要做什么。就像你告诉朋友:“等我水烧开了,我会叫你来。”这样你就可以在水烧开的过程中做其他事情,而不是一直等着。

所以,异步编程和回调的结合,让我们的程序更加高效,不用浪费时间在等待上。

def intersection(center, radius, p1, p2):
  dx, dy = p2[0] - p1[0], p2[1] - p1[1]
  a = dx**2 + dy**2
  b = 2 * (dx * (p1[0]- center[0]) + dy * (p1[1] - center[1]))
  c = (p1[0] - center[0])**2 + (p1[1] - center[1])**2 - radius**2
  K = b**2 - 4 * a * c
  return True if K>0 else False  

if __name__ == "__main__":  
  p1 = 10,50
  p2 = 100,50
  center= 50,150
  radius = 600

print(intersection(center, radius, p1, p2))
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=0x=0

0

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

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

然后其他的步骤都和之前一样,记得标记出根。

1

我刚刚想出了一个小程序,用来找交点,这个想法是在解决另一个与Zelle图形相关的问题时产生的。虽然数学上可能有更简单的方法,但我选择了比较复杂的方式:

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()

输出结果

在这里输入图片描述

注意

你可能会输入一些值,这些值不会产生交点,比如半径为2而截距为8。你的代码没有考虑到这一点——如果发生这种情况,上面的代码会直接抛出一个断言错误。不过你可以把这个改成一个可以捕捉并处理的错误。

撰写回答