Back

Explore Courses Blog Tutorials Interview Questions
+1 vote
2 views
in Python by (47.6k points)

In a programming language (Python, C#, etc) I need to determine how to calculate the angle between a line and the horizontal axis?

I think an image describes best what I want:

no words can describe this

Given (P1x,P1y) and (P2x,P2y) what is the best way to calculate this angle? The origin is in the top-left and only the positive quadrant is used.

1 Answer

0 votes
by (106k points)
edited by

To calculate the angle between a line and the horizontal axis firstly you need to find the difference between the start point and the endpoint. Below is the formula that you can use:-

deltaY = P2_y - P1_y

deltaX = P2_x - P1_x

After finding the endpoints calculate the angle which runs from the positive X-axis at P1 to the positive Y-axis at P1.

angleInDegrees = arctan(deltaY / deltaX) * 180 / PI

To know more about this you can have a look at the following video:-

Browse Categories

...