Find whether two triangles intersect or not
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Understanding Triangle Intersection
Determining whether two triangles intersect is an insightful geometrical challenge with significant applications in computer graphics, collision detection, and geometric modeling. This article delves into the technical methodology, explores various computational approaches, and provides examples to demonstrate these principles.
Mathematical Foundation
Two triangles intersect if they share any region in space or touch each other at any point. Intersection problems can be broken down into verifying if any segment of one triangle overlaps with any segment of the other triangle.
Given two triangles in a 3D space: • Triangle with vertices , , • Triangle with vertices , ,
The intersection can be checked using the Separating Axis Theorem (SAT).
Separating Axis Theorem (SAT)
SAT is a fundamental method used to determine if two convex shapes intersect. The principle asserts that two convex shapes do not intersect if and only if there exists a line (axis) on which their projections are disjoint.
Steps Involved in SAT for Triangles
- Identify Potential Separating Axes: • Normals of each triangle: For each triangle, the plane's normal vector can potentially separate the triangle pairs. • Cross product of edges: Edges of triangles contribute more potential axes from their cross-products.
- Project Triangle Vertices onto Axes: • For each potential separating axis, project the vertices of both triangles.
- Check Overlaps: • Determine if the projections overlap on each axis. If there's a single axis where projections are disjoint, the triangles do not intersect. If projections overlap on all axes, triangles intersect.
Computational Example
Consider two triangles in a 2D plane for simplicity, where: • : , , • : , ,
Using SAT:
- Calculate normals: For 2D, normals are perpendicular to the edge vectors.
- Project vertices: Compute projections on potential separating axes.
- Check Separation: No separation axis exists; thus, triangles intersect.
Special Cases
• Collinear Overlap: If both triangles lie on the same plane and overlap partially or fully, additional checks are required. • Degenerate Triangles: If a triangle collapses to a line or point, convert the problem to line-segment intersection checks.
Algorithms and Complexities
While SAT provides a robust method, it can be computationally exhaustive. Optimized algorithms, like the Möller–Trumbore intersection algorithm, offer speed improvements by directly solving plane equations for 3D triangle intersections.
Applications
• Computer Graphics: Efficient rendering and occlusion culling. • Collision Detection: In physics simulations and games, detecting intersection impacts decisions. • Geometric Modeling: Ensures non-overlapping model features.
Summary Table
| Method | Key Points | Time Complexity |
| Separating Axis Theorem (SAT) | Involves projecting onto axes defined by normals and cross products | per axis |
| Möller–Trumbore | Direct plane-intersection computation for higher performance in 3D environment |
This exploration into the intersection of triangles underscores a careful balance between geometric intuition and algorithmic efficiency, crucial for modern computational applications.
Related reading
- Find XOR of all numbers in a given range
- Finding 2 equal sum sub-sequences, with maximum sum?
- Finding a minimal subarray of n integers of sum k in linear time
- Finding a minimum bounding sphere for a frustum
- Finding a number that repeats even no of times where all the other numbers repeat odd no of times
- Finding a prime number after a given number
- Finding a minimum spanning tree on a directed graph
- Finding a New Minimum Spanning Tree After a New Edge Was Added to The Graph

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.