Table of Contents
Introduction
What Is Collinearity?
Why Collinearity Matters: A Real-World Drone Delivery Scenario
Mathematical Approaches to Test Collinearity
Complete Implementation with Test Cases
Best Practices and Edge Case Handling
Conclusion
Introduction
In geometry and computational problem-solving, determining whether three points lie on a straight line—known as collinearity—is a surprisingly common and critical task. While it may sound like a textbook exercise, collinearity checks power real-world systems from autonomous vehicles to urban planning.
This article explores how to test collinearity in Python, using a compelling real-time use case from the rapidly growing drone delivery industry. You’ll learn robust, efficient, and numerically stable methods—with production-ready code and thorough testing.
What Is Collinearity?
Three points A(x1​,y1​) , B(x2​,y2​) , and C(x3​,y3​) are collinear if they all lie on the same straight line. This means the area of the triangle they form is zero, or equivalently, the slope between any two pairs of points is identical.
However, directly comparing slopes can fail due to division by zero (vertical lines) or floating-point precision errors. A better approach uses the cross product or area of triangle method.
Why Collinearity Matters: A Real-World Drone Delivery Scenario
Imagine you're an engineer at SkyDrop, a startup deploying autonomous drones to deliver medical supplies in rural areas. Your drone flies from a hospital (Point A) to a remote clinic (Point C), with a mandatory battery-swap station at Point B along the route. Before dispatching, your system must verify that A, B, and C are collinear—ensuring the drone doesn’t waste energy deviating from a straight path. If they’re not aligned, the flight plan is rejected, and logistics are re-optimized.
![PlantUML Diagram]()
This isn’t theoretical: in mountainous or GPS-denied regions, even small deviations increase risk and battery consumption. Collinearity validation is a silent guardian of efficiency and safety.
Mathematical Approaches to Test Collinearity
Method 1. Area of Triangle (Cross Product)
The area of triangle formed by three points is:
Area=21​∣x1​(y2​−y3​)+x2​(y3​−y1​)+x3​(y1​−y2​)∣
If the area is zero, the points are collinear.
This avoids division and handles vertical lines gracefully.
Method 2. Vector Cross Product
Treat vectors AB and AC. If their 2D cross product is zero, they’re parallel—and thus collinear:
Cross=(x2​−x1​)(y3​−y1​)−(y2​−y1​)(x3​−x1​)
Both methods are equivalent. We’ll use the cross product for its simplicity and numerical stability.
For floating-point coordinates, use a small epsilon tolerance instead of exact zero.
Complete Implementation with Test Cases
from typing import Tuple
import unittest
import math
def are_collinear(p1: Tuple[float, float],
p2: Tuple[float, float],
p3: Tuple[float, float],
epsilon: float = 1e-9) -> bool:
"""
Determine if three 2D points are collinear using the cross product method.
Args:
p1, p2, p3: Points as (x, y) tuples
epsilon: Tolerance for floating-point comparison
Returns:
True if points are collinear within tolerance, False otherwise
"""
x1, y1 = p1
x2, y2 = p2
x3, y3 = p3
# Compute cross product of vectors (p2 - p1) and (p3 - p1)
cross = (x2 - x1) * (y3 - y1) - (y2 - y1) * (x3 - x1)
return abs(cross) < epsilon
class TestCollinearity(unittest.TestCase):
def test_basic_collinear_points(self):
self.assertTrue(are_collinear((0, 0), (1, 1), (2, 2)))
self.assertTrue(are_collinear((1, 3), (2, 5), (3, 7))) # y = 2x + 1
def test_vertical_line(self):
self.assertTrue(are_collinear((5, 1), (5, 10), (5, -3)))
def test_horizontal_line(self):
self.assertTrue(are_collinear((1, 4), (10, 4), (-2, 4)))
def test_non_collinear_points(self):
self.assertFalse(are_collinear((0, 0), (1, 1), (1, 2)))
self.assertFalse(are_collinear((0, 0), (0, 1), (1, 0)))
def test_floating_point_tolerance(self):
# Nearly collinear due to floating-point error
p1, p2 = (0.0, 0.0), (1.0, 1.0)
p3 = (2.0, 2.0 + 1e-10) # Tiny deviation
self.assertTrue(are_collinear(p1, p2, p3))
# Clearly non-collinear
p3 = (2.0, 2.1)
self.assertFalse(are_collinear(p1, p2, p3))
def test_drone_delivery_scenario(self):
hospital = (100.0, 200.0)
battery_station = (150.0, 250.0)
clinic = (200.0, 300.0)
self.assertTrue(are_collinear(hospital, battery_station, clinic))
def simulate_drone_route_validation():
"""Simulate SkyDrop's route validation system"""
routes = [
((10, 20), (30, 40), (50, 60)), # Valid
((0, 0), (1, 1), (2, 3)), # Invalid
((100, 100), (100, 200), (100, 300)), # Valid vertical
]
print("=== Drone Route Validation ===")
for i, (A, B, C) in enumerate(routes, 1):
status = "APPROVED" if are_collinear(A, B, C) else "REJECTED"
print(f"Route {i}: {status} — Points {A}, {B}, {C}")
if __name__ == "__main__":
simulate_drone_route_validation()
print("\nRunning unit tests...")
unittest.main(argv=[''], exit=False, verbosity=2)
![3]()
Best Practices and Edge Case Handling
Use epsilon tolerance for floating-point coordinates to avoid precision errors.
Avoid slope-based methods—they break on vertical lines and amplify rounding errors.
Validate input types in production systems (e.g., ensure points are 2-tuples).
Document coordinate system assumptions (e.g., Cartesian, GPS lat/long—note: this method assumes planar geometry; for GPS, use geodesic calculations).
Conclusion
Testing collinearity is far more than a geometry exercise—it’s a vital check in autonomous systems, computer vision, robotics, and logistics. By using the cross product method, you gain a fast, division-free, and robust solution that handles all edge cases.
In the drone delivery world, this tiny function ensures life-saving supplies reach remote villages efficiently and safely. Whether you're building the next SkyDrop or solving algorithmic challenges, mastering collinearity checks makes your code smarter, safer, and production-ready.