Solving Coordinate Geometry Problems in Python from scratch
By: Nikhil Adithyan
Tags: Programming, Python, Tutorial, Education, Algorithms

Solving Coordinate Geometry Problems in Python from scratch

Learn to construct your own Python functions to solve geometrical problems without importing any packages

Photo by Dawid Małecki from Pexels

Introduction

Coordinate Geometry also is known as Analytical Geometry or Cartesian Geometry, used to analyze or study geometry through the means of coordinates and vertices. This concept is held in much geometrical mathematics for example to find the area of geometrical shapes using coordinates, finding the midpoints, dividing a line segment into m:n ratio, and so on. Each of these concepts has its own formulas and methodology for solving the problems. What if we can solve these using python?

Python, being a general-purpose programming language, is highly powerful and efficient in solving mathematical tasks or problems. Even though there are several scientific packages like NumPy and SciPy, defining our own mathematical functions and parameters on top of python would be more flexible. So, what are we going to solve? Read the concept below to know more about it.

Concept

In this article, we are going to solve a problem based on the triangle and a simple calculation on finding the area of the quadrilateral.

Triangle case: There are three mid-points of the triangle P, Q, and R whose coordinates are (12, 6), (11, 4), and (8, 4) respectively are given to us. We have to find the vertices of the triangle (A, B, and C), area of the triangle, compare the original triangle and the mid-point triangle (based on size), check collinearity, and finally plot the triangle. Of course, this problem would take four pages to conclude but, it just takes minutes to calculate in python.

Quadrilateral case: Four vertices A, B, C and D whose coordinates are (-4, -2), (-3, -5), (3, -2) and (2, 3) respectively are given. We have to find the area of the quadrilateral, check collinearity, and plot the quadrilateral. This is what the whole concept is going to be.

Now, let’s program and calculate it in python.

Solving Triangle Case

To solve the problem, the given components are just the coordinates of the mid-points. So, we have to derive the coordinates of the vertices, find the area, check collinearity, compare the original triangle versus the mid-point triangle, and finally plot both the original and mid-point triangle.

Step-1 | Finding the coordinates of the Vertices

The first step is going to be finding the coordinates of the vertices A, B, and C. To calculate the coordinates of the vertices, we can define a function that takes mid-points as the parameters and pass on a formula to calculate the coordinates of A, B, and C. Let’s define the function in python!

Python Implementation:

Code Explanation: Firstly, we are defining a function ‘find_triangle_coordinates’ that takes the mid-points as parameters. Inside the function, we are slicing the given mid-points into single numerical variables and stored them as p1, p2, q1, q2, r1, r1. Next, we are creating three empty lists named A, B, and C in which we are going to store the calculated coordinates. After that, we are passing on a formula in which we used the sliced values of the mid-points and stored that in the respective numerical variables. Explaining the formula intuitively, we are adding the first and third midpoint value and subtracting the value with the second midpoint value. Followed by that, we are appending all the calculated coordinates into the relevant vertices A, B, and C and finally returned those coordinates.

Step-2 | Calculating the Area

In this step, we are going to define a function for finding the area of the triangle that takes the vertices of the triangle as parameters. We will pass on a function inside the function to find the area using the coordinates of the vertices. Follow the code to define the function in python.

Python Implementation:

Code Explanation: Just like how we did in the previous step, we are defining a function ‘find_triangle_area’ that takes the coordinates of vertices as parameters. Inside the function, we are slicing and storing the values into the respective coordinate values. After that, we are passing a formula that takes the sliced coordinate values and calculates the area. The formula is nothing but it takes the coordinate values and crosses multiples them. To understand the formula more effectively, imagine a matrix that has all the x-coordinates in the first row and all the y-coordinates in the second row. Note that, after listing all the three x and y coordinates in the matrix, we have to add another first value of the x and y coordinate at the end of the matrix. Now, take x1 and cross multiply the value with y2, like that, x2 with y3 and x3 with y1. After calculating this part, we have to subtract it with a set of other cross multiplied values starting from y1 with x2, y2 with x3, and finally y3 with x1. Sometimes while applying this formula, we may end up with a negative result but, the area cannot be negative. So using the ‘abs’ function we are converting the sign to positive if the value is negative and multiplying the output with 0.5 or 1/2 (part of the formula). After this whole process, we are returning the final output of the function.

Step-3 | Check Collinearity

What is collinearity? In coordinate geometry, the vertices are said to have collinearity when the area of the triangle results in ‘zero’ square units. We can define a function in python which helps us to check whether the given vertices have collinearity or not.

Python Implementation:

Code Explanation: As usual, we are defining a function ‘check_triangle_collinearity’ that takes the vertices of the triangle as parameters. Inside the function, we are using the ‘find_triangle_area’ function which we defined a step ago to find the area of the triangle. After that, we passing on an if-else statement that returns true when the area which we calculated inside the function is equal to ‘zero’. If the statement gets satisfied, it returns a message saying that the given vertices are collinear. If not, it returns a message of non-collinearity.

Step-4 | Compare the Triangles

In this step, we are going to define a simple and minimal function that compares the given triangles and gives us a result that how many times the original triangle is bigger than the mid-point triangle. Follow the code define the function in python.

Python Implementation:

Code Explanation: As I said before, this function ‘compare_triangles’ is a simple and minimal function that takes the original triangle area and the mid-point triangle area as parameters. Inside the function, we are storing a string that says how many times the original triangle is bigger than the mid-point triangle inside a variable ‘message’. Finally, we returning the message variable.

Step-5 | Plotting the Triangle

In this final step, we are going to define a function that makes us able to plot a triangle using the coordinates of the vertices. When it comes to plotting or graphing, without any other option we have to use the matplotlib package, or else, it takes us long to define the function. So, make sure you have matplotlib installed in your python environment. If you don’t have matplotlib installed in the python environment, then follow or copy the code and paste it into your command prompt terminal: pip install matplotlib

After installing the plotting package, we are now ready to define our plot function in python.

Python Implementation:

Code Explanation: We are defining a function ‘plot_triangle’ that takes the vertices of the triangle and color of the triangle as parameters. Inside the function, we are slicing the coordinates into single numerical variables (just like how we did in the preceding steps). After that, we are importing the plotting package which is the matplotlib package, and tuning some parameters to customize the size (20 inches in width and 10 inches in height) and style (ggplot) of the plot. Next, we are storing the graph into a variable ‘fig’ and finally returning the plot.

Step-6 | The Results

This step is the most exciting one when compared to all the others as we are going to use our defined function to solve the problem and see the results. We can use the defined function just by calling them. Without further ado, let’s implement our defined functions in python!

Python Implementation:

Output: