繁体   English   中英

如何正确使用触发功能?

[英]How do I use trig functions correctly?

给定方向(度数和距离),我尝试使用math.sin和math.cos函数来计算用户在5x5网格上必须经过的x和y距离。

A)有人可以告诉我为什么我的代码遇到逻辑错误吗? 这一切都向我结帐。

Please input the angle in degrees of the fire quickly!90
Please input the distance of the fire ~VERY~ quickly (input a number between 1 and 5)5
The fire is 1.000000 streets to the west of the firehouse!
The fire is 5 streets to the south of the firehouse!

它返回错误的行驶方向(向南而不是向北),并指示用户向西行驶1个街区。 这太可怕了。

我的第二个问题是:我将如何做到这一点,以便乌龟沿着网格从中心点到最靠近网格的块(用户输入)沿网格绘制一条蓝线。

码:

import turtle
import math
NUMBER_OF_LICENSES=10

def goodbyeMessage():
    print("Thank you for using the program!")

def getInfo():
    fire_Direction=math.degrees(float(input("Please input the angle in degrees of the fire quickly!")))
    fire_Distance=float(input("Please input the distance of the fire ~VERY~ quickly (input a number between 1 and 5)"))
    return fire_Direction,fire_Distance


def announceDirections(horizontolDirection,verticalDirection):

    if horizontolDirection < 0:
        print("The fire is %f streets to the west of the firehouse!" %abs(int(horizontolDirection)))
    elif horizontolDirection > 0:
        print("The fire is %f streets to the east of the firehouse!" %abs(int(horizontolDirection)))
    else:
        pass

    if verticalDirection < 0:
        print("The fire is %d streets to the south of the firehouse!" %abs(int(verticalDirection)))
    elif verticalDirection > 0:
        print("The fire is %d streets to the north of the firehouse!" %abs(int(verticalDirection)))
    else:
        pass

    if verticalDirection == 0 and horizontolDirection == 0:
        print("The firehouse is on fire!")


def giveDirection(fire_Direction,fire_Distance):
    horizontolDirection = round(fire_Distance * (math.cos(fire_Direction)))
    verticalDirection = round(fire_Distance * (math.sin(fire_Direction)))
    announceDirections(horizontolDirection,verticalDirection)




    return horizontolDirection, verticalDirection



def reportFire():

    fire_Direction,fire_Distance=getInfo()
    horizontolDirection,verticalDirection = giveDirection(fire_Direction,fire_Distance)



def drawHorizontal():
    turtle.speed(0)
    turtle.penup()
    turtle.forward(-300)
    turtle.left(90)
    turtle.forward(300)
    turtle.right(90)
    turtle.pendown()
    for i in range(5):
        turtle.forward(500)
        turtle.forward(-500)
        turtle.right(90)
        turtle.forward(100)
        turtle.left(90)

    turtle.forward(500)
    turtle.left(90)
    turtle.forward(500)
    turtle.left(90)
    turtle.forward(500)
    turtle.left(180)

def drawVertical():
    for i in range(5):
            turtle.forward(100)
            turtle.right(90)
            turtle.forward(500)
            turtle.forward(-500)
            turtle.left(90)
    turtle.forward(-500) #Back to upper left corner which will be main drawing control point

def drawFireStation():
    #From main drawing control point
    turtle.penup()
    #225 instead of 250 so firestation circle is centered in middle of grid
    turtle.forward(225)
    turtle.right(90)
    turtle.forward(250)
    turtle.pendown()
    turtle.circle(25)
    turtle.penup()
    turtle.forward(-250)
    turtle.left(90)
    turtle.forward(-225)
def drawGrid():
    turtle.showturtle()
    turtle.speed(0)
    drawHorizontal()
    drawVertical()
    drawFireStation()



def main():
    drawGrid()
    for i in range(NUMBER_OF_LICENSES):
        reportFire()
    goodbyeMessage()

if __name__ == "__main__":
    main()

trig函数期望其参数为弧度

您可以使用math.radians()函数将度数转换为弧度

例如:

fire_Direction = math.radians(fire_Direction)
horizontolDirection = round(fire_Distance * (math.cos(fire_Direction)))
verticalDirection = round(fire_Distance * (math.sin(fire_Direction)))

编辑:

我注意到您在此处错误地使用了math.degrees:

fire_Direction=math.degrees(float(input("Please input the angle in degrees of the fire quickly!")))

您可能应该只将其更改为math.radians(...) (与math.degrees相反)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM