Number of possible Triangles in a Cartesian coordinate system
Last Updated :
30 Aug, 2022
Given n points in a Cartesian coordinate system. Count the number of triangles formed.
Examples:
Input : point[] = {(0, 0), (1, 1), (2, 0), (2, 2)
Output : 3
Three triangles can be formed from above points.
A simple solution is to check if the determinant of the three points selected is non-zero or not. The following determinant gives the area of a Triangle (Also known as Cramer's rule).
Area of the triangle with corners at (x1, y1), (x2, y2) and (x3, y3) is given by:
{\displaystyle Area = \pm \frac{1}{2}\begin{bmatrix} x1 & y1 & 1\\ x2 & y2 & 1\\ x3 & y3 & 1 \end{bmatrix}}
We can solve this by taking all possible combination of 3 points and finding the determinant.
C++
// C++ program to count number of triangles that can
// be formed with given points in 2D
#include <bits/stdc++.h>
using namespace std;
// A point in 2D
struct Point
{
int x, y;
};
// Returns determinant value of three points in 2D
int det(int x1, int y1, int x2, int y2, int x3, int y3)
{
return x1*(y2 - y3) - y1*(x2 - x3) + 1*(x2*y3 - y2*x3);
}
// Returns count of possible triangles with given array
// of points in 2D.
int countPoints(Point arr[], int n)
{
int result = 0; // Initialize result
// Consider all triplets of points given in inputs
// Increment the result when determinant of a triplet
// is not 0.
for (int i=0; i<n; i++)
for (int j=i+1; j<n; j++)
for (int k=j+1; k<n; k++)
if (det(arr[i].x, arr[i].y, arr[j].x,
arr[j].y, arr[k].x, arr[k].y))
result++;
return result;
}
// Driver code
int main()
{
Point arr[] = {{0, 0}, {1, 1}, {2, 0}, {2, 2}};
int n = sizeof(arr)/sizeof(arr[0]);
cout << countPoints(arr, n);
return 0;
}
Java
// Java program to count number
// of triangles that can be formed
// with given points in 2D
class GFG{
// Returns determinant value
// of three points in 2D
static int det(int x1, int y1, int x2, int y2, int x3, int y3)
{
return (x1 * (y2 - y3) - y1 *
(x2 - x3) + 1 * (x2 *
y3 - y2 * x3));
}
// Returns count of possible
// triangles with given array
// of points in 2D.
static int countPoints(int [][]Point, int n)
{
int result = 0; // Initialize result
// Consider all triplets of
// points given in inputs
// Increment the result when
// determinant of a triplet is not 0.
for(int i = 0; i < n; i++)
for(int j = i + 1; j < n; j++)
for(int k = j + 1; k < n; k++)
if(det(Point[i][0], Point[i][1],
Point[j][0], Point[j][1],
Point[k][0], Point[k][1])>=0)
result = result + 1;
return result;
}
// Driver code
public static void main(String[] args)
{
int Point[][] = {{0, 0},{1, 1},{2, 0},{2, 2}};
int n = Point.length;
System.out.println(countPoints(Point, n));
}
}
// This code is contributed by
// mits
Python3
# Python program to count number
# of triangles that can be formed
# with given points in 2D
# Returns determinant value
# of three points in 2D
def det(x1, y1, x2, y2, x3, y3):
return (x1 * (y2 - y3) - y1 *
(x2 - x3) + 1 * (x2 *
y3 - y2 * x3))
# Returns count of possible
# triangles with given array
# of points in 2D.
def countPoints(Point, n):
result = 0 # Initialize result
# Consider all triplets of
# points given in inputs
# Increment the result when
# determinant of a triplet is not 0.
for i in range(n):
for j in range(i + 1, n):
for k in range(j + 1, n):
if(det(Point[i][0], Point[i][1],
Point[j][0], Point[j][1],
Point[k][0], Point[k][1])):
result = result + 1
return result
# Driver code
Point = [[0, 0], [1, 1],
[2, 0], [2, 2]]
n = len(Point)
print(countPoints(Point, n))
# This code is contributed by
# Sanjit_Prasad
C#
// C# program to count number
// of triangles that can be formed
// with given points in 2D
using System;
class GFG{
// Returns determinant value
// of three points in 2D
static int det(int x1, int y1, int x2, int y2, int x3, int y3)
{
return (x1 * (y2 - y3) - y1 *
(x2 - x3) + 1 * (x2 *
y3 - y2 * x3));
}
// Returns count of possible
// triangles with given array
// of points in 2D.
static int countPoints(int[,] Point, int n)
{
int result = 0; // Initialize result
// Consider all triplets of
// points given in inputs
// Increment the result when
// determinant of a triplet is not 0.
for(int i = 0; i < n; i++)
for(int j = i + 1; j < n; j++)
for(int k = j + 1; k < n; k++)
if(det(Point[i,0], Point[i,1], Point[j,0], Point[j,1],Point[k,0], Point[k,1])>=0)
result = result + 1;
return result;
}
// Driver code
public static void Main()
{
int[,] Point = new int[,] { { 0, 0 }, { 1, 1 }, { 2, 0 }, { 2, 2 } };
int n = Point.Length/Point.Rank;
Console.WriteLine(countPoints(Point, n));
}
}
// This code is contributed by mits
PHP
<?php
// PHP program to count number
// of triangles that can be formed
// with given points in 2D
// Returns determinant value
// of three points in 2D
function det($x1, $y1, $x2,
$y2, $x3, $y3)
{
return ($x1 * ($y2 - $y3) - $y1 *
($x2 - $x3) + 1 * ($x2 *
$y3 - $y2 * $x3));
}
// Returns count of possible
// triangles with given array
// of points in 2D.
function countPoints($Point, $n)
{
$result = 0; // Initialize result
// Consider all triplets of
// points given in inputs
// Increment the result when
// determinant of a triplet is not 0.
for($i = 0; $i < $n; $i++)
for($j = $i + 1; $j < $n; $j++)
for($k = $j + 1; $k < $n; $k++)
if(det($Point[$i][0], $Point[$i][1],
$Point[$j][0], $Point[$j][1],
$Point[$k][0], $Point[$k][1]))
$result = $result + 1;
return $result;
}
// Driver code
$Point = array(array(0, 0),
array(1, 1),
array(2, 0),
array(2, 2));
$n = count($Point);
echo countPoints($Point, $n);
// This code is contributed by
// mits
?>
JavaScript
<script>
// JavaScript program to count number
// of triangles that can be formed
// with given points in 2D
// Returns determinant value
// of three points in 2D
function det(x1, y1, x2, y2, x3, y3)
{
return (x1 * (y2 - y3) -
y1 * (x2 - x3) +
1 * (x2 * y3 - y2 * x3));
}
// Returns count of possible
// triangles with given array
// of points in 2D.
function countPoints(Point, n)
{
// Initialize result
let result = 0;
// Consider all triplets of
// points given in inputs
// Increment the result when
// determinant of a triplet is not 0.
for(let i = 0; i < n; i++)
for(let j = i + 1; j < n; j++)
for(let k = j + 1; k < n; k++)
if (det(Point[i][0], Point[i][1],
Point[j][0], Point[j][1],
Point[k][0], Point[k][1]) >= 0)
result = result + 1;
return result;
}
// Driver Code
let Point = [ [ 0, 0 ], [ 1, 1 ],
[ 2, 0 ], [ 2, 2 ] ];
let n = Point.length;
document.write(countPoints(Point, n));
// This code is contributed by souravghosh0416
</script>
Output:
3
Time Complexity: O(n^{3}) .
Auxiliary Space: O(1) because it is using constant space
Optimization :
We can optimize the above solution to work in O(n2) using the fact that three points cannot form a triangle if they are collinear. We can use hashing to store slopes of all pairs and find all triangles in O(n2) time.
Similar Reads
Count number of triangles possible for the given sides range Given four integers A, B, C, and D, the task is to find the number of distinct sets (X, Y, and Z) where X, Y and Z denotes the length of sides forming a valid triangle. A ? X ? B, B ? Y ? C, and C ? Z ? D.Examples: Input: A = 2, B = 3, C = 4, D = 5 Output: 7 Explanation: Possible Length of Side of T
10 min read
Find the coordinates of a triangle whose Area = (S / 2) Given an integer S, the task is to find the coordinates of a triangle whose area is (S / 2). Examples: Input: S = 4 Output: (0, 0) (1000000000, 1) (999999996, 1) Input: S = 15 Output: (0, 0) (1000000000, 1) (999999985, 1) Approach: It is known than the area of the triangle whose coordinates are (X1,
5 min read
Count number of right triangles possible with a given perimeter Given a perimeter P, the task is to find the number of right triangles possible with perimeter equal to p.Examples: Input: P = 12 Output: number of right triangles = 1 The only right angle possible is with sides hypotenuse = 5, perpendicular = 4 and base = 3. Input: p = 840 Output: number of right t
6 min read
Number of triangles that can be formed with given N points Given X and Y coordinates of N points on a Cartesian plane. The task is to find the number of possible triangles with the non-zero area that can be formed by joining each point to every other point. Examples: Input: P[] = {{0, 0}, {2, 0}, {1, 1}, {2, 2}} Output: 3 Possible triangles can be [(0, 0},
9 min read
Count number of triangles possible with length of sides not exceeding N Given an integer N, the task is to find the total number of right angled triangles that can be formed such that the length of any side of the triangle is at most N. A right-angled triangle satisfies the following condition: X2 + Y2 = Z2 where Z represents the length of the hypotenuse, and X and Y re
10 min read