1

コーディングの方法を学ぼうとしているのですが、この演習を理解できません。具体的には、正確な y 軸のインターセプト ポイントを取得します。指定された式は x 軸のポイントを取得するために機能しますが、y 軸のポイントを取得する方法がわかりません。

エクササイズ :

入力 : 円の半径と線の y 切片。

出力: 指定された y 切片でウィンドウを横切る水平線で描かれた円。交差点の 2 点をマークします。交点の 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

3 に答える 3

0

y 座標については、同様の式を使用できます。

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

根をマークすることですべて同じことを行います。

于 2014-03-30T18:10:26.627 に答える