結果

Visual Inverse Trig and atan2 Without Math Libraries

Inverse trig and atan2 — finding an angle from a point's coordinates

Plain English first

Regular trig starts with an angle and gives you a point.

Inverse trig goes the other direction: you have a point and you want the angle.

Imagine you know where something is on screen — you have its x and y position. Inverse trig tells you the angle you'd need to face to look directly at it.

The picture

Given a point (x, y), draw an arrow from the origin to the point. The angle θ is measured from the positive x-axis (pointing right) counterclockwise to your arrow.

              y
              |   * (x, y)
              |  /
              | / ← θ is this angle
              |/_________ x

Standard math notation

θ = arctan(y / x)    ← basic, loses quadrant info
θ = atan2(y, x)      ← correct, uses both signs

Why atan2 exists

arctan(y/x) has a problem: the same ratio can come from two opposite quadrants.

For example, (-3, -4) and (3, 4) both give y/x = -4/-3 = 4/3. But they point in completely opposite directions.

atan2(y, x) fixes this by looking at the signs of x and y separately.

Verbose Python with descriptive names

def find_angle_from_point(x_coordinate, y_coordinate):
    """
    Given a point at (x, y), find the angle in radians from
    the positive x-axis to the arrow pointing at that point.

    Uses atan2 logic to correctly handle all four quadrants.
    Returns an angle between -π and +π radians (-180° to 180°).
    """
    # Handle the special case where the point is at the origin
    if x_coordinate == 0 and y_coordinate == 0:
        return 0  # no direction defined

    # Use the basic arctangent for the magnitude of the angle
    if x_coordinate != 0:
        basic_angle = arctangent_approximation(y_coordinate / x_coordinate)
    else:
        # Pointing straight up or down
        PI = 3.141592653589793
        return PI / 2 if y_coordinate > 0 else -PI / 2

    PI = 3.141592653589793

    # Adjust the quadrant based on signs of x and y
    if x_coordinate > 0:
        # Quadrant I (x+, y+) or Quadrant IV (x+, y-)
        # basic_angle is already correct
        final_angle = basic_angle

    elif x_coordinate < 0 and y_coordinate >= 0:
        # Quadrant II: x is negative, y is positive or zero
        # The basic angle is negative here; add π to flip to correct side
        final_angle = basic_angle + PI

    else:
        # Quadrant III: x is negative, y is negative
        # Subtract π to get the correct negative angle
        final_angle = basic_angle - PI

    return final_angle


def angle_to_degrees(angle_in_radians):
    PI = 3.141592653589793
    return angle_in_radians * 180 / PI

Worked example

# Enemy is at position (3, 4). You are at origin. What angle do you face?
enemy_x = 3
enemy_y = 4

angle_radians = find_angle_from_point(enemy_x, enemy_y)
angle_degrees = angle_to_degrees(angle_radians)
# angle_degrees ≈ 53.1°

# Same for the opposite direction:
angle_radians = find_angle_from_point(-3, -4)
angle_degrees = angle_to_degrees(angle_radians)
# angle_degrees ≈ -126.9°  ← correctly in Quadrant III

Common mistakes

  • Using atan(y/x) instead of atan2(y, x) — you lose quadrant information and get wrong angles half the time.
  • Forgetting that atan2 takes (y, x) order, not (x, y). The y comes first in almost every language.
  • Not handling x == 0 before dividing — causes division by zero.

See also

評論

尚無評論。成為第一個吧!


評論經審核後顯示。