python - 2 math questions -


hey, i'm using pygame , want to:

a) make function returns angle between 2 points. i've tried doing math.atan2 , i'm getting wierd returns. tried both (delta x, deltay) , (deltay, deltax). suggestions?

b) given length , angle, return point using 2 0. example, lengthdir(2,45) using (length,angle) return (2,2).

thanks help. i've searched on internet , couldn't find me...

math.atan2 returns radians. if need degree, multiply result 180/π.

def a(dx, dy):   return math.atan2(dy, dx) * 180 / math.pi 

similarly, trigonometric functions in math operate in radians. if input degree, need multiply π/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 convenient functions math.degrees , math.radians don't need memorize 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)) 

Comments

Popular posts from this blog

javascript - Enclosure Memory Copies -

php - Replacing tags in braces, even nested tags, with regex -