2014-01-29 68 views
-1

給定一個方向的度數和距離,我試圖使用函數math.sin和math.cos來計算用戶將不得不在5x5網格上行進的x和y距離。如何正確使用trig函數?

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塊。這是可怕的。

我的第二個問題是這樣的: 我該如何做到這一點,所以烏龜會沿着網格從中心點向最接近網格座標的用戶輸入繪製藍線。

CODE:

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

它拼寫爲「水平」。 –

回答

2

的三角函數希望他們的論據是在radians

可以使用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的倒數)

+0

感謝您的寶貴意見,我的朋友, 我忘了提及,用戶必須輸入度數。 編輯: NM。你絕對正確。我得到它的工作。 – user3187485

+0

@ user3187485'math.radians(angle)'以角度度量並將其轉換爲弧度。這樣你可以在'math'模塊提供的trig函數中使用它們。 –