Geometry
Triangle Intersection
Computational Geometry
Mathematics
Algorithms

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.

Practice algorithms

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 AA with vertices VA1V_{A1}, VA2V_{A2}, VA3V_{A3} • Triangle BB with vertices VB1V_{B1}, VB2V_{B2}, VB3V_{B3}

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

  1. 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.
  2. Project Triangle Vertices onto Axes: • For each potential separating axis, project the vertices of both triangles.
  3. 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: • T1T_1: VA1=(0,0)V_{A1} = (0,0), VA2=(5,0)V_{A2} = (5,0), VA3=(0,5)V_{A3} = (0,5)T2T_2: VB1=(1,1)V_{B1} = (1,1), VB2=(6,1)V_{B2} = (6,1), VB3=(1,6)V_{B3} = (1,6)

Using SAT:

  1. Calculate normals: For 2D, normals are perpendicular to the edge vectors.
  2. Project vertices: Compute projections on potential separating axes.
  3. 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

MethodKey PointsTime Complexity
Separating Axis Theorem (SAT)Involves projecting onto axes defined by normals and cross productsO(1)O(1) per axis
Möller–TrumboreDirect plane-intersection computation for higher performance in 3D environmentO(1)O(1)

This exploration into the intersection of triangles underscores a careful balance between geometric intuition and algorithmic efficiency, crucial for modern computational applications.


Related reading
Course
Intermediate
27 lessons
15 hours
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 course
Track 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.

Practice algorithms

All Rights Reserved.