Hey Finxters! Today, you’ll learn an important function that you may need to write in the future: how to write a Python program to calculate the angle between 2 points in a clockwise motion.
We will be using principles in the Python language to write a working program to write the angles and calculate the angle or radians in a given plane! We are going to take our time and explain everything, so have no worries as we walk through this together!
Problem: Calculating the Angle Between Two Points
In this particular problem we want to find the clockwise angle between the vector from the origin to point A, and the vector from the origin to point B. We will have three points and two vectors so that our angle is well defined. This can be used in both algebraic and geometric definitions. For our example, we will be using the geometric definition.
What We want to Accomplish: Writing a Python program that will calculate the angle in a clockwise motion.
Our program needs to be able to calculate the angles between two points from a given origin of
(0,0), point A
(0,1), and point B
(1, -1). These 3 points will give an angle of 45* from a total of 360* starting from the center of an
I want to show you 2 different ways of doing this. One way I will have a program written out showing you step by step. In the second way, I will be showing you a more Pythonic way of writing the same program—giving you an edge when writing a program for a code interview.
Method 1: Writing It Out — The Hard Way
In this example, we want to start by importing the
math module, then create a class defining the
y. We create the following variables,
v2 with default parameters.
import math class Vector: def __init__(self, x, y): self.x = x self.y = y v1 = Vector(0, 1) v2 = Vector(0, -1)
afterwards from math we use the method
atan2, Return a
tan(y / x), in radians. The result is between
pi. The vector in the plane from the origin to point
(x, y) makes this angle with the positive
X axis. The point of
atan2() is that the signs of both inputs are known to it, so it can compute the correct quadrant for the angle.
atan2(1, 1) are both
atan2(-1, -1) is
-3*pi/4. We save these new vectors to new variables:
v1_theta = math.atan2(v1.y, v1.x) v2_theta = math.atan2(v2.y, v2.x) r = (v2_theta - v1_theta) * (180.0 / math.pi) if r < 0: r % 360 print r
By taking these two points and subtracting the first from the second, multiplying it by the product of 180 divided by pi and saving it into a variable (‘
r’). We modulo the
-r by 360. r is smaller than 0 because it is going to be a negative number. Afterwards, we print
r out. This is just one way to write out this problem. It is a little messy and if we are not careful, we can easily get the incorrect answer output.
Method 2: Writing It Out — The Pythonic Way with NumPy
In this next example, I will be using NumPy to get the angle between the two point and have it returned to me. When writing in Python, it is always best to follow PEP8 and to write you program as simply as possible to take up less memory in your code and therefore less runtime when testing your program.
The first thing we will do is
import numpy as np, then define the angle using point 1 (
p1) and point 2 (
p2) as arguments. We will again use
arctan2 multiplied by
p1 to find angle 1 and
arctan2 multiplied by
p2 to find the second angle. We will return the degrees using the
np.red2deg function by first subtracting the first angle from the second, then we multiply 2 and
np.pi then we modulo the product of the two answers. When we input our vector we receive our answer correctly when we print.
import numpy as np def angle_between(p1, p2): ang1 = np.arctan2(*p1[::-1]) ang2 = np.arctan2(*p2[::-1]) return np.rad2deg((ang1 - ang2) % (2 * np.pi)) A = (1, 0) B = (1, -1) print(angle_between(A, B)) # 45. print(angle_between(B, A)) # 315.
As you can see printing the
angle_between(B,A) gives us two totality different answers! The reason for this is because the first point is moving clockwise to the second point giving us the smaller number. By going counter clockwise, we get a much larger number than the actual angle we are looking for!
As you see, writing this program in Python was easy because Python has built in math and NumPy modules to make the code beautiful and clean. It may seem arbitrary but this program can be used in several applications from architecture to taking virtual tours in hotels, apartments or even restaurants.
As shown in this photo, you can find a single point in a space with these three points. It could also be used in creating a virtual room, or in crime fighting when trying to use forensic science to catch a bad guy! Python is amazing programming language that never ceases to amaze me. I hope that after reading this article you will be inspired to write you own Python programs using what you have learned here to add to your portfolios.