Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Does a module exist which already find an angle and the distance between two points?

First of all, I apologize to post this easy question. Probably there is a module to compute angle and distance between two points.

  • A = (560023.44957588764,6362057.3904932579)
  • B = (560036.44957588764,6362071.8904932579)
like image 981
Gianni Spear Avatar asked Feb 02 '26 08:02

Gianni Spear


1 Answers

Given

enter image description here

you could compute the angle, theta, and the distance between A and B with:

import math
def angle_wrt_x(A,B):
    """Return the angle between B-A and the positive x-axis.
    Values go from 0 to pi in the upper half-plane, and from 
    0 to -pi in the lower half-plane.
    """
    ax, ay = A
    bx, by = B
    return math.atan2(by-ay, bx-ax)

def dist(A,B):
    ax, ay = A
    bx, by = B
    return math.hypot(bx-ax, by-ay)

A = (560023.44957588764, 6362057.3904932579)
B = (560036.44957588764, 6362071.8904932579)
theta = angle_wrt_x(A, B)
d = dist(A, B)
print(theta)
print(d)

which yields

0.839889619638  # radians
19.4743420942

(Edit: Since you are dealing with points in a plane, its easier to use atan2 than the dot-product formula).

like image 180
unutbu Avatar answered Feb 03 '26 22:02

unutbu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!