VOOZH about

URL: https://www.geeksforgeeks.org/dsa/tangents-two-convex-polygons/

⇱ Tangents between two Convex Polygons - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Tangents between two Convex Polygons

Last Updated : 26 Apr, 2025

Given two convex polygons, we aim to identify the lower and upper tangents connecting them. 

As shown in the figure below, TRL and TLR represent the upper and lower tangents, respectively. 

👁 Tangents-between-two-Convex-Polygons-01

Examples:

Input: First Polygon : [[2, 2], [3, 3], [5, 2], [4, 0], [3, 1]]
Second Polygon : [[-1, 0], [0, 1], [1, 0], [0, -2]].
Output: Upper Tangent - line joining (0,1) and (3,3)
Lower Tangent - line joining (0,-2) and (4,0)
Explanation: The image clearly shows the structure and the tangents connecting the two polygons

👁 poly

Approach: 

To find the upper tangent, we begin by selecting two points: the rightmost point of polygon a and the leftmost point of polygon b. The line joining these two points is labeled as Line 1. Since this line passes through polygon b (i.e., it is not completely above it), we move to the next point in an anti-clockwise direction on b, forming Line2. This line is now above polygon b, which is good. However, it crosses polygon a, so we move to the next point on a in a clockwise direction, creating Line 3. Line 3 still crosses polygon a, prompting another move to Line 4. Line 4, however, crosses polygon b, so we proceed to Line 5. Finally, Line 5 does not cross either polygon, making it the correct upper tangent for the given polygons.

👁 Tangents-between-two-Convex-Polygons-02

For finding the lower tangent we need to move inversely through the polygons i.e. if the line is crossing the polygon b we move to clockwise next and to anti-clockwise next if the line is crossing the polygon a.

Algorithm for upper tangent:

L line joining the rightmost point of a and leftmost point of b. while (L crosses any of the polygons) { while(L crosses b) L L' : the point on b moves up. while(L crosses a) L L' : the point on a moves up. }

Algorithm for lower tangent:

L line joining the rightmost point of a and leftmost point of b. while (L crosses any of the polygons) { while (L crosses b) L L' : the point on b moves down. while (L crosses a) L L' : the point on a moves down. }

Note that the above code only computes the upper tangent. A similar approach can be used to find the lower tangent as well.


Output
upper tangent (0,1) (3,3)

Time Complexity: O(n1 log (n1) + n2 log(n2)) 
Auxiliary Space: O(1)


Comment
Article Tags:
Article Tags: