问题描述
嘿,我正在使用pyGame,我想:
A)创建一个返回两点之间角度的函数。我试着用math.atan2来做这件事,我得到了非常奇怪的回报。我试了这两个(三角洲X,deltaY)和(deltaY,deltaX)。有什么建议吗?B
$ b B)给出一个长度和一个角度,使用这两个从0返回一个点。
例如,LengthDir(2,45)使用(长度,角度)将返回(2,2)。
感谢您的帮助。我在互联网上搜索了所有东西,找不到任何东西来帮助我......
返回。如果您需要学位,请将结果乘以180 /π。 def A(dx,dy):
return math.atan2(dy,dx)* 180 / math.pi
类似地,所有三角函数 math
以弧度操作。如果你输入一个学位,你需要先乘以π/ 180。
def LengthDir(length,angle):
radian_angle = angle * math.pi / 180
return(length * math.cos(radian_angle),length * math.sin(radian_angle))
Python提供了方便的函数和所以你不需要记住180 /π的常数。
def A(dx,dy):
返回math.degrees(math.atan2(dy,dx))
定义长度(长度,角度):
radian_angle = math.radians(angle)
return(length * math.cos(radian_angle),length * math.sin(radian_angle))
Hey, I'm using pyGame and I want to:
A) make a function that returns the angle between two points. I've tried doing this with math.atan2 and I'm getting really wierd returns. I tried this with both (delta X, deltaY) and (deltaY, deltaX). Any suggestions?
B) given a length and an angle, return a point using those two from 0.For example, LengthDir(2,45) using (length,angle) would return (2,2).
Thanks for the help. I've searched all over the internet and I couldn't find anything to help me...
math.atan2
returns radians. If you need degree, multiply the result by 180/π.
def A(dx, dy):
return math.atan2(dy, dx) * 180 / math.pi
Similarly, all trigonometric functions in math
operate in radians. If you input a degree, you need to multiply by π/180 first.
def LengthDir(length, angle):
radian_angle = angle * math.pi / 180
return (length * math.cos(radian_angle), length * math.sin(radian_angle))
Python provides the convenient functions math.degrees
and math.radians
so you don't need to memorize the constant 180/π.
def A(dx, dy):
return math.degrees( math.atan2(dy, dx) )
def LengthDir(length, angle):
radian_angle = math.radians(angle)
return (length * math.cos(radian_angle), length * math.sin(radian_angle))
这篇关于蟒蛇 - 2个数学题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!