英文:
math.tan in python outputting strange values
问题
Here's the translated Python code with your comments and values included:
import turtle
from tkinter import *
import math
root = Tk()
screen = turtle.Screen()
t = turtle.Turtle()
t.penup()
t.speed('fastest')
w = turtle.Turtle()
w.penup()
w.speed('fastest')
w.goto(100, 0)
w.left(90)
w.pendown()
w.forward(300)
w.back(600)
w.penup()
w.hideturtle()
def x_value(data):
global angle
angle = float(data)
print(f"角度: {angle}")
t.setheading(angle)
x = Scale(root, from_=90, to=-90, command=x_value)
x.pack()
def intercept_finder():
x_difference = 100 - int(t.xcor())
print(x_difference)
b = 100
a = 50
tan_section = math.tan(math.radians(a))
print(f"Tan_Section: {tan_section}")
y_coordinate = b * tan_section
print(f"Y坐标: {y_coordinate}")
get_x = Button(root, text='输入', bg='red', width=5, command=intercept_finder)
get_x.pack()
screen.mainloop()
I've included the values b = 100
and a = 50
in the intercept_finder
function to calculate the y-coordinate using the formula b * tan(a)
with the angle in radians. This should help you test the program with the specified values.
英文:
import turtle
from tkinter import *
import math
root = Tk()
screen = turtle.Screen()
t = turtle.Turtle()
t.penup()
t.speed('fastest')
w = turtle.Turtle()
w.penup()
w.speed('fastest')
w.goto(100, 0)
w.left(90)
w.pendown()
w.forward(300)
w.back(600)
w.penup()
w.hideturtle()
def x_value(data):
global angle
angle = float(data)
print(f"Angle: {angle}")
t.setheading(angle)
x = Scale(root, from_=90, to=-90, command=x_value)
x.pack()
def intercept_finder():
x_difference = 100-int(t.xcor())
print(x_difference)
#t.forward(100*math.tan(angle))
tan_section = math.tan((angle))
print(f"Tan_Section: {tan_section}")
print(f"Tan: {100*tan_section}")
print(f"X_Cord = {t.xcor()}")
print(f"Y_Cord = {t.ycor()}")
get_x = Button(root, text='Enter', bg='red', width=5, command=intercept_finder)
get_x.pack()
screen.mainloop()
When angle is 50, the expected math.tan is supposed to be 119.1753593. math.tan gives me -0.27190061199763077.
I'm trying to make it so turtle finds the y cordinates it will be 100 steps more than its origina x value, so its starting cords (0,0) and the ending cord (100, ??). To find the y coordinates i need to use the formula b x tan(a), where b is the 100 steps and a is the angle.
I'm using:
b = 100
a = 50
Use these values for testing if the program outputted the correct output.
答案1
得分: 2
从文档中,重点是:
math.tan(x)
返回x
的弧度的正切值。
与此同时,setheading
使用度数,所以我会假设您的 angle
也是以度数表示;在传递给 math.tan
之前,您需要将度数转换为弧度:
math.tan(math.radians(angle))
如果您处于标准角度模式(请参阅setheading
链接),则不应需要进行其他转换。
英文:
From the docs, emphasis mine:
> math.tan(x)
> Return the tangent of x
radians.
Meanwhile, setheading
uses degrees so I'll assume your angle
is degrees too; you'll need to convert your degrees to radians before passing to math.tan
:
math.tan(math.radians(angle))
If you're in standard angle mode (see the setheading
link), you shouldn't need to do any other conversion.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论