The Wayback Machine - https://web.archive.org/web/20241116192520/https://www.geeksforgeeks.org/number-of-ways-a-convex-polygon-of-n2-sides-can-split-into-triangles-by-connecting-vertices/
Open In App

Number of ways of Triangulation for a Polygon

Last Updated : 06 Nov, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Given a convex polygon with n sides. The task is to calculate the number of ways in which triangles can be formed by connecting vertices with non-crossing line segments.
Examples: 
 

Input: n = 3
Output: 1 
It is already a triangle so it can only be formed in 1 way.

Input: n = 4
Output: 2 
It can be cut into 2 triangles by using either pair of opposite vertices. 

Input: n = 6
Output: 14 (Below, There are all 14 polygon).
It can be cut into 2 triangles by using either pair of opposite vertices. 

4

The above problem is an application of a catalan numbers. The task is to only find the (n-2)’th Catalan Number. First few catalan numbers for n = 0, 1, 2, 3, 4, 5 are 1 1 2 5 14 42, â€Ķ (considered from 0th number)

C++
// C++ code of finding Number of ways a convex polygon of n+2 
// sides can split into triangles by connecting vertices
#include <iostream>
using namespace std;

// Function to calculate the binomial coefficient C(n, k)
int binomialCoeff(int n, int k) {
    // C(n, k) is the same as C(n, n-k)
    if (k > n - k) {
        k = n - k;
    }

    int res = 1;
    // Calculate the value of n! / (k! * (n-k)!)
    for (int i = 0; i < k; ++i) {
        res *= (n - i);
        res /= (i + 1);
    }
    return res;
}

// Function to find the nth Catalan number
int countWays(int n) {
    n = n - 2;
    // Calculate C(2n, n)
    int c = binomialCoeff(2 * n, n);
    // Return the nth Catalan number
    return c / (n + 1);
}

int main() {
    int n = 6;
    cout << countWays(n) << endl;
    return 0;
}
C
// C code of finding Number of ways a convex polygon of n+2 
// sides can split into triangles by connecting vertices
#include <stdio.h>

// Function to calculate the binomial coefficient C(n, k)
int binomialCoeff(int n, int k) {
    // C(n, k) is the same as C(n, n-k)
    if (k > n - k) {
        k = n - k;
    }

    int res = 1;
    // Calculate the value of n! / (k! * (n-k)!)
    for (int i = 0; i < k; ++i) {
        res *= (n - i);
        res /= (i + 1);
    }
    return res;
}

// Function to find the nth Catalan number
int countWays(int n) {
    n = n - 2;
  
    // Calculate C(2n, n)
    int c = binomialCoeff(2 * n, n);
  
    // Return the nth Catalan number
    return c / (n + 1);
}

int main() {
    int n = 6;
    printf("%d\n", countWays(n));
    return 0;
}
Java
// Java code of finding Number of ways a convex polygon of n+2 
// sides can split into triangles by connecting vertices
public class PolygonTriangulation {
  
    // Function to calculate the binomial coefficient C(n, k)
    static int binomialCoeff(int n, int k) {
      
        // C(n, k) is the same as C(n, n-k)
        if (k > n - k) {
            k = n - k;
        }

        int res = 1;
      
        // Calculate the value of n! / (k! * (n-k)!)
        for (int i = 0; i < k; ++i) {
            res *= (n - i);
            res /= (i + 1);
        }
        return res;
    }

    // Function to find the nth Catalan number
    static int countWays(int n) {
        n = n - 2;
      
        // Calculate C(2n, n)
        int c = binomialCoeff(2 * n, n);
      
        // Return the nth Catalan number
        return c / (n + 1);
    }

    public static void main(String[] args) {
        int n = 6;
        System.out.println(countWays(n));
    }
}
Python
# Python code of finding Number of ways a convex polygon of n+2 
# sides can split into triangles by connecting vertices

def binomialCoeff(n, k):
    # C(n, k) is the same as C(n, n-k)
    if k > n - k:
        k = n - k

    res = 1
    # Calculate the value of n! / (k! * (n-k)!)
    for i in range(k):
        res *= (n - i)
        res //= (i + 1)
    return res

# Function to find the nth Catalan number
def countWays(n):
    n = n - 2
    
    # Calculate C(2n, n)
    c = binomialCoeff(2 * n, n)
    
    # Return the nth Catalan number
    return c // (n + 1)

n = 6
print(countWays(n))
JavaScript
// JavaScript code of finding Number of ways a convex polygon of n+2 
// sides can split into triangles by connecting vertices
function binomialCoeff(n, k) {

    // C(n, k) is the same as C(n, n-k)
    if (k > n - k) {
        k = n - k;
    }

    let res = 1;
    
    // Calculate the value of n! / (k! * (n-k)!)
    for (let i = 0; i < k; ++i) {
        res *= (n - i);
        res /= (i + 1);
    }
    return res;
}

// Function to find the nth Catalan number
function countWays(n) {
    n = n - 2;
    
    // Calculate C(2n, n)
    let c = binomialCoeff(2 * n, n);
    
    // Return the nth Catalan number
    return c / (n + 1);
}

let n = 6;
console.log(countWays(n));

Output
14

Time Complexity: O(n), where n is nth catalan number.
Auxiliary Space: O(1)



Previous Article
Next Article

Similar Reads

Minimum Cost Polygon Triangulation
A triangulation of a convex polygon is formed by drawing diagonals between non-adjacent vertices (corners) such that the diagonals never intersect. The problem is to find the cost of triangulation with the minimum cost. The cost of a triangulation is sum of the weights of its component triangles. Weight of each triangle is its perimeter (sum of len
15+ min read
Polygon with maximum sides that can be inscribed in an N-sided regular polygon
Given a regular polygon of N sides, the task is to find the maximum sided polygon that can be inscribed inside the given polygon by joining non-adjacent vertices. Print -1, if no such polygon exist. Examples: Input: N = 8Output: 4Explanation:At most a 4 sided polygon can be inscribed inside the given 8-sided polygon as shown below: Input: N = 3Outp
3 min read
Check if given polygon is a convex polygon or not
Given a 2D array point[][] with each row of the form {X, Y}, representing the co-ordinates of a polygon in either clockwise or counterclockwise sequence, the task is to check if the polygon is a convex polygon or not. If found to be true, then print "Yes" . Otherwise, print "No". In a convex polygon, all interior angles are less than or equal to 18
9 min read
Number of sides of Polygon when maximum possible number of diagonals is given
Given a number K which is the maximum possible number of diagonals in any polygon, the task is to find the number of sides of that polygon. Examples: Input: 2Output: 4Explanation: A quadrilateral has 2 diagonals at max. Input: 6Output: -1Explanation: No polygon has at most 6 diagonals. Approach: This problem can be solved based on the following mat
6 min read
Number of triangles formed by joining vertices of n-sided polygon with two common sides and no common sides
Given N-sided polygon we need to find the total number of triangles formed by joining the vertices of the given polygon with exactly two sides being common and no side being common.Examples: Input : N = 6 Output : 6 2 The image below is of a triangle forming inside a Hexagon by joining vertices as shown above. The triangle formed has two sides (AB
5 min read
Number of occurrences of a given angle formed using 3 vertices of a n-sided regular polygon
Given an n-sided regular polygon and an angle ?, the task is to find number of occurrences of angle ( Ai, Aj, Ak ) = ? ( i < j < k) in a regular n-gon (regular polygon with n vertices) with vertices marked as A1, A2, ..., An.Examples: Input: n = 4, ang = 90 Output: 4 Input: n = 6, ang = 50 Output: 0 Approach: First we check whether such an an
6 min read
Number of cycles formed by joining vertices of n sided polygon at the center
Given an N-sided regular polygon, we have connected all the vertices at the center of the polygon, thus dividing the polygon into N equal parts. Our task is to the Count of the total number of cycles in the polygon.Note: A Cycle is a closed-loop starting and ending on same point.Examples: Input: N = 3 Output: 7 Explanation: When a 3 sided polygon i
5 min read
Find number of diagonals in n sided convex polygon
Given n > 3, find number of diagonals in n sided convex polygon.According to Wikipedia, In geometry, a diagonal is a line segment joining two vertices of a polygon or polyhedron, when those vertices are not on the same edge. Informally, any sloping line is called diagonal. Examples :  Input : 5Output : 5Explanation: Five possible diagonals are :
3 min read
Minimum area of a Polygon with three points given
Given three points of a regular polygon(n > 3), find the minimum area of a regular polygon (all sides same) possible with the points given.Examples: Input : 0.00 0.00 1.00 1.00 0.00 1.00 Output : 1.00 By taking point (1.00, 0.00) square is formed of side 1.0 so area = 1.00 . One thing to note in question before we proceed is that the number of s
14 min read
Program to find the Interior and Exterior Angle of a Regular Polygon
Given the number of sides n of a regular polygon. The task is to find out the Interior angle and Exterior angle of the polygon.Examples: Input : n = 6 Output : Interior angle: 120 Exterior angle: 60 Input : n = 10 Output: Interior angle: 144 Exterior angle: 36 Interior angle: The angle between two adjacent sides inside the polygon is known as the I
4 min read
Check if it is possible to create a polygon with a given angle
Given an angle [Tex]a [/Tex]where, [Tex]1\le a< 180 [/Tex]. The task is to check whether it is possible to make a regular polygon with all of its interior angle equal to [Tex]a [/Tex]. If possible then print "YES", otherwise print "NO" (without quotes). Examples: Input: angle = 90 Output: YES Polygons with sides 4 is possible with angle 90 degre
4 min read
Probability that the pieces of a broken stick form a n sided polygon
We have a stick of length L. The stick got broken at (n-1) randomly chosen points (lengths of parts can be non-integer or floating point numbers also) so we get n parts. We need to find the probability that these n pieces can form a n sided polygon. Examples: Input : L = 5 n = 3 Output : 0.25 We need to cut rope of length 5 into three parts. First
4 min read
Program to find the Perimeter of a Regular Polygon
Given the number of sides 'n' and the length of side 's' of a regular polygon, the task is to find out the Perimeter of this polygon. Examples: Input: n = 7, s = 10 Output: Perimeter : 70 Since the sides are 7, Hence the given polygon is Heptagon. Therefore. Perimeter = 7*10 = 70 Input: n = 5, s = 2.5 Output: Perimeter : 12.5 Since the sides are 5,
5 min read
Program to find Area of Triangle inscribed in N-sided Regular Polygon
Given the triangle inscribed in an N-sided regular polygon with given side length, formed using any 3 vertices of the polygon, the task is to find the area of this triangle. Examples: Input: N = 6, side = 10 Output: 129.904 Input: N = 8, side = 5 Output: 45.2665 Approach: Consider the 1st example: Given is a 6 sided regular polygon ABCDEF with a tr
6 min read
Side of a regular n-sided polygon circumscribed in a circle
Given two integers r and n where n is the number of sides of a regular polygon and r is the radius of the circle this polygon is circumscribed in. The task is to find the length of the side of polygon. Examples: Input: n = 5, r = 11 Output: 12.9256Input: n = 3, r = 5 Output: 8.6576 Approach: Consider the image above and let angle AOB be theta then
4 min read
Apothem of a n-sided regular polygon
Given here the side length a of a regular n-sided polygon, the task is to find the length of its Apothem. Apothem is the line drawn from the center of the polygon that is perpendicular to one of its sides. Examples: Input a = 9, n = 6 Output: 7.79424 Input: a = 8, n = 7 Output: 8.30609 Approach: In the figure, we see the polygon can be divided into
4 min read
Area of largest Circle inscribe in N-sided Regular polygon
Given a regular polygon of N sides with side length a. The task is to find the area of the Circle which inscribed in the polygon. Note : This problem is mixed version of This and This Examples: Input: N = 6, a = 4 Output: 37.6801 Explanation: In this, the polygon have 6 faces and as we see in fig.1 we clearly see that the angle x is 30 degree so th
5 min read
Check if it is possible to create a polygon with given n sides
Given an array arr[] that contains the lengths of n sides that may or may not form a polygon. The task is to determine whether it is possible to form a polygon with all the given sides. Print Yes if possible else print No.Examples: Input: arr[] = {2, 3, 4} Output: YesInput: arr[] = {3, 4, 9, 2} Output: No Approach: In order to create a polygon with
5 min read
Determine the position of the third person on regular N sided polygon
Given 'N' which represent the regular N sided polygon. Two children are standing on the vertex 'A' and 'B' of this Regular N sided polygon. The task is to determine the number of that vertex another person should stand on so that the sum of the minimum jumps required to reach A and minimum jumps required to reach B is minimized. Note: The vertices
6 min read
Ratio of area of two nested polygons formed by connecting midpoints of sides of a regular N-sided polygon
Given an N-sided polygon, the task is to find the ratio of the area of the Nth to (N + 1)th N-sided regular nested polygons generated by joining the midpoints of the sides of the original polygon. Examples : Input: N = 3Output: 4.000000Explanation: Ratio of the length of the sides formed by joining the mid-points of the triangle with the length of
4 min read
Angle between 3 given vertices in a n-sided regular polygon
Given a n-sided regular polygon and three vertices a1, a2 and a3, the task is to find the angle suspended at vertex a1 by vertex a2 and vertex a3. Examples: Input: n = 6, a1 = 1, a2 = 2, a3 = 4 Output: 90 Input: n = 5, a1 = 1, a2 = 2, a3 = 5 Output: 36 Approach: The angle subtended by an edge on the center of n sided regular polygon is 360/n.The an
6 min read
Central angle of a N sided Regular Polygon
Given an integer N representing N sided regular polygon, the task is to find the angle made by the sides on the centre of the polygon that is the central angle. The central angle is the angle formed by the two vertices forming an edge and the centre. Examples: Input: N = 6 Output: 60 Explanation: The polygon is a hexagon with an angle 60 degree. In
3 min read
Sum of internal angles of a Polygon
Given an integer N, the task is to find the sum of interior angles of an N-sided polygon. A plane figure having a minimum of three sides and angles is called a polygon.Examples: Input: N = 3 Output: 180 3-sided polygon is a triangle and the sum of the interior angles of a triangle is 180.Input: N = 6 Output: 720 Approach: The sum of internal angles
3 min read
Find the centroid of a non-self-intersecting closed Polygon
Given N vertices of the polygon, the task is to find the centroid of the polygonExamples: Input: ar = {{0, 0}, {0, 8}, {8, 8}, {8, 0}} Output: {Cx, Cy} = {4, 4} Input: ar = {{1, 2}, {3, -4}, {6, -7}} Output: {Cx, Cy} = {3.33, -3} Approach: The centroid of a non-self-intersecting closed polygon defined by n vertices (x0, y0), (x1, y1), ..., (xn-1, y
5 min read
Minimum side of square embedded in Regular polygon with N sides
Given an even number N which represents the number of sides of a regular polygon with N vertices, the task is to find the square of the minimum size such that given Polygon can completely embed in the square. A Polygon is a convex figure and has equal sides and equal angles. All sides have length 1. Embedding: Place Polygon in the square in such wa
8 min read
Find the angle of Rotational Symmetry of an N-sided regular polygon
Given an integer N which is the number of sides of a regular polygon. The task is to find the smallest angle of rotation such that the generated regular polygons have a similar position and dimensions, i.e. the new rotated polygon is in symmetry with the initial one. A shape is said to have a rotation symmetry if there exists a rotation in the rang
4 min read
Check if an N-sided Polygon is possible from N given angles
Given an array arr[] of N elements, where each element represents an angle(in degrees) of a polygon, the task is to check whether it is possible to make an N-sided polygon with all the given angles or not. If it is possible then print Yes else print No. Examples: Input: N = 3, arr[] = {60, 60, 60}Output: YesExplanation: There exists a triangle(i.e.
4 min read
Nth angle of a Polygon whose initial angle and per angle increment is given
Given four integers N, A, K, n where N represents the number of sides the polygon, A represents the initial angle of the polygon, K represents the per angle increase, the task is to find the nth angle of the polygon having N sides. If it is not possible then print -1. Examples: Input: N = 3, A = 30, K = 30, n = 3Output: 90Explanation:The 3rd angle
6 min read
Find the vertex diagonally opposite to the vertex M from an N-sided polygon
Given two integers N and M, the task is to find the vertex diagonally opposite to the Mth vertex of an N-sided polygon. Examples: Input: N = 6, M = 2 Output: 5 Explanation: It can be observed from the image above that the vertex opposite to vertex 5 is 2. Input: N = 8, M = 5 Output: 1 Explanation: It can be observed from the image above that the ve
3 min read
Sum of sides of largest and smallest child polygons possible from a given polygon
Given an integer A representing the external angle (in degrees) of a regular convex polygon, the task is to find the sum of the sides of the largest and smallest secondary polygons formed such that each edge of the secondary polygon is a chord of the primary polygon. If it is not possible to form such polygons, then print "-1". Examples: Input: A =
4 min read
three90RightbarBannerImg