Mati Sandacz
Mati Sandacz

Reputation: 29

How to Diagonal in Python?

def diagonal(t, x, y):
    """Makes a diagonal line to the given x, y offsets and return"""
    from math import atan2, sqrt, pi
    angle = atan2(y, x) * 180 / pi
    dist = sqrt(x**2 + y**2)
    lt(t, angle)
    fdbk(t, dist)
    rt(t, angle)

I do not understand what does the atan2 method do. How is it calculating the angle? And why is there another method that calculates the distance?

Upvotes: 0

Views: 384

Answers (2)

gboffi
gboffi

Reputation: 25023

In high school (liceo) I studied the inverse trigonometric functions, one of them the arc tan(x) or simply atan(x) — its meaning being, the angle t whose tangent is equal to x. The implied limits are -infinity ≤ x ≤ + infinity and -pi/2 ≤ t ≤ pi/2.

So, what is atan2(x, y)? It's a similar function that returns the angle t, -pi ≤ t ≤ pi, that is comprised between the unit vector in the horizontal direction and the vector OP, with P=(x,y).

Why this fuction is called atan2? because it has TWO arguments (atan has a single argument) and because you can compute atan2 using atan

atan2(x, y) <equiv> atan(y/x) or atan(y/x) ± pi

where you have to choose the particular solution based on the signs of x and y — this logic is no difficult but someone (possibly the implementors of first FORTRAN compilers?) decided that it was a so common question that it deserved a function on its own.

Another feature of atan2 is that the overflow that follows when x==0 is completely avoided,

Upvotes: 0

ABcDexter
ABcDexter

Reputation: 2941

Atan2 is simply Tan inverse trignometric function which gives the output in radians. So, it is multiplied by 180/pi to convert it into degrees.

dist is for calculating Euclidean distance between x and y.

And for the lt(t, angle) and rt(t, angle), i can guess that they stand for Left Turn, by t degrees and Right Turn by t degress respectively.

Upvotes: 1

Related Questions