# Triangle with no point inside

Given N points in 2-dimensional space, we need to find three points such that triangle made by choosing these points should not contain any other points inside. All given points will not lie on the same line so solution will always exist.

Examples:

In above diagram possible triangle with no point inside can be formed by choosing these triplets, [(0, 0), (2, 0), (1, 1)] [(0, 0), (1, 1), (0, 2)] [(1, 1), (2, 0), (2, 2)] [(1, 1), (0, 2), (2, 2)] So any of the above triplets can be the final answer.

The solution is based on the fact that if there exist triangle(s) with no points inside, then we can form a triangle with any random point among all points.

We can solve this problem by searching all three points one by one. The first point can be chosen randomly. After choosing the first point, we need two points such that their slope should be different and no point should lie inside the triangle of these three points. We can do this by choosing the second point as nearest point to the first and third point as second nearest point with the different slope. For doing this, we first iterate over all points and choose the point which is nearest to the first one and designate that as second point of the required triangle. Then we iterate one more time to find point which has different slope and has the smallest distance, which will be the third point of our triangle.

## C++

`// C/C++ program to find triangle with no point inside ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// method to get square of distance between ` `// (x1, y1) and (x2, y2) ` `int` `getDistance(` `int` `x1, ` `int` `y1, ` `int` `x2, ` `int` `y2) ` `{ ` ` ` `return` `(x2 - x1)*(x2 - x1) + ` ` ` `(y2 - y1)*(y2 - y1); ` `} ` ` ` `// Method prints points which make triangle with no ` `// point inside ` `void` `triangleWithNoPointInside(` `int` `points[][2], ` `int` `N) ` `{ ` ` ` `// any point can be chosen as first point of triangle ` ` ` `int` `first = 0; ` ` ` `int` `second, third; ` ` ` `int` `minD = INT_MAX; ` ` ` ` ` `// choose nearest point as second point of triangle ` ` ` `for` `(` `int` `i = 0; i < N; i++) ` ` ` `{ ` ` ` `if` `(i == first) ` ` ` `continue` `; ` ` ` ` ` `// Get distance from first point and choose ` ` ` `// nearest one ` ` ` `int` `d = getDistance(points[i][0], points[i][1], ` ` ` `points[first][0], points[first][1]); ` ` ` `if` `(minD > d) ` ` ` `{ ` ` ` `minD = d; ` ` ` `second = i; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Pick third point by finding the second closest ` ` ` `// point with different slope. ` ` ` `minD = INT_MAX; ` ` ` `for` `(` `int` `i = 0; i < N; i++) ` ` ` `{ ` ` ` `// if already chosen point then skip them ` ` ` `if` `(i == first || i == second) ` ` ` `continue` `; ` ` ` ` ` `// get distance from first point ` ` ` `int` `d = getDistance(points[i][0], points[i][1], ` ` ` `points[first][0], points[first][1]); ` ` ` ` ` `/* the slope of the third point with the first ` ` ` `point should not be equal to the slope of ` ` ` `second point with first point (otherwise ` ` ` `they'll be collinear) and among all such ` ` ` `points, we choose point with the smallest ` ` ` `distance */` ` ` `// here cross multiplication is compared instead ` ` ` `// of division comparison ` ` ` `if` `((points[i][0] - points[first][0]) * ` ` ` `(points[second][1] - points[first][1]) != ` ` ` `(points[second][0] - points[first][0]) * ` ` ` `(points[i][1] - points[first][1]) && ` ` ` `minD > d) ` ` ` `{ ` ` ` `minD = d; ` ` ` `third = i; ` ` ` `} ` ` ` `} ` ` ` ` ` `cout << points[first][0] << ` `", "` ` ` `<< points[first][1] << endl; ` ` ` `cout << points[second][0] << ` `", "` ` ` `<< points[second][1] << endl; ` ` ` `cout << points[third][0] << ` `", "` ` ` `<< points[third][1] << endl; ` `} ` ` ` `// Driver code to test above methods ` `int` `main() ` `{ ` ` ` `int` `points[][2] = {{0, 0}, {0, 2}, {2, 0}, ` ` ` `{2, 2}, {1, 1}}; ` ` ` `int` `N = ` `sizeof` `(points) / ` `sizeof` `(points[0]); ` ` ` `triangleWithNoPointInside(points, N); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find triangle ` `// with no point inside ` `import` `java.io.*; ` ` ` `class` `GFG ` `{ ` ` ` `// method to get square of distance between ` ` ` `// (x1, y1) and (x2, y2) ` ` ` `static` `int` `getDistance(` `int` `x1, ` `int` `y1, ` `int` `x2, ` `int` `y2) ` ` ` `{ ` ` ` `return` `(x2 - x1)*(x2 - x1) + ` ` ` `(y2 - y1)*(y2 - y1); ` ` ` `} ` ` ` ` ` `// Method prints points which make triangle with no ` ` ` `// point inside ` ` ` `static` `void` `triangleWithNoPointInside(` `int` `points[][], ` `int` `N) ` ` ` `{ ` ` ` `// any point can be chosen as first point of triangle ` ` ` `int` `first = ` `0` `; ` ` ` `int` `second =` `0` `; ` ` ` `int` `third =` `0` `; ` ` ` `int` `minD = Integer.MAX_VALUE; ` ` ` ` ` `// choose nearest point as second point of triangle ` ` ` `for` `(` `int` `i = ` `0` `; i < N; i++) ` ` ` `{ ` ` ` `if` `(i == first) ` ` ` `continue` `; ` ` ` ` ` `// Get distance from first point and choose ` ` ` `// nearest one ` ` ` `int` `d = getDistance(points[i][` `0` `], points[i][` `1` `], ` ` ` `points[first][` `0` `], points[first][` `1` `]); ` ` ` `if` `(minD > d) ` ` ` `{ ` ` ` `minD = d; ` ` ` `second = i; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Pick third point by finding the second closest ` ` ` `// point with different slope. ` ` ` `minD = Integer.MAX_VALUE; ` ` ` `for` `(` `int` `i = ` `0` `; i < N; i++) ` ` ` `{ ` ` ` `// if already chosen point then skip them ` ` ` `if` `(i == first || i == second) ` ` ` `continue` `; ` ` ` ` ` `// get distance from first point ` ` ` `int` `d = getDistance(points[i][` `0` `], points[i][` `1` `], ` ` ` `points[first][` `0` `], points[first][` `1` `]); ` ` ` ` ` `/* the slope of the third point with the first ` ` ` `point should not be equal to the slope of ` ` ` `second point with first point (otherwise ` ` ` `they'll be collinear) and among all such ` ` ` `points, we choose point with the smallest ` ` ` `distance */` ` ` `// here cross multiplication is compared instead ` ` ` `// of division comparison ` ` ` `if` `((points[i][` `0` `] - points[first][` `0` `]) * ` ` ` `(points[second][` `1` `] - points[first][` `1` `]) != ` ` ` `(points[second][` `0` `] - points[first][` `0` `]) * ` ` ` `(points[i][` `1` `] - points[first][` `1` `]) && ` ` ` `minD > d) ` ` ` `{ ` ` ` `minD = d; ` ` ` `third = i; ` ` ` `} ` ` ` `} ` ` ` ` ` `System.out.println(points[first][` `0` `] + ` `", "` ` ` `+ points[first][` `1` `]); ` ` ` `System.out.println(points[second][` `0` `]+ ` `", "` ` ` `+ points[second][` `1` `]) ; ` ` ` `System.out.println(points[third][` `0` `] +` `", "` ` ` `+ points[third][` `1` `]); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main (String[] args) ` ` ` `{ ` ` ` `int` `points[][] = {{` `0` `, ` `0` `}, {` `0` `, ` `2` `}, {` `2` `, ` `0` `}, ` ` ` `{` `2` `, ` `2` `}, {` `1` `, ` `1` `}}; ` ` ` `int` `N = points.length; ` ` ` `triangleWithNoPointInside(points, N); ` ` ` `} ` `} ` ` ` `// This article is contributed by vt_m. ` |

*chevron_right*

*filter_none*

## C#

`using` `System; ` ` ` `// C# program to find triangle ` `// with no point inside ` ` ` `public` `class` `GFG ` `{ ` ` ` `// method to get square of distance between ` ` ` `// (x1, y1) and (x2, y2) ` ` ` `public` `static` `int` `getDistance(` `int` `x1, ` `int` `y1, ` `int` `x2, ` `int` `y2) ` ` ` `{ ` ` ` `return` `(x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1); ` ` ` `} ` ` ` ` ` `// Method prints points which make triangle with no ` ` ` `// point inside ` ` ` `public` `static` `void` `triangleWithNoPointInside(` `int` `[][] points, ` `int` `N) ` ` ` `{ ` ` ` `// any point can be chosen as first point of triangle ` ` ` `int` `first = 0; ` ` ` `int` `second = 0; ` ` ` `int` `third = 0; ` ` ` `int` `minD = ` `int` `.MaxValue; ` ` ` ` ` `// choose nearest point as second point of triangle ` ` ` `for` `(` `int` `i = 0; i < N; i++) ` ` ` `{ ` ` ` `if` `(i == first) ` ` ` `{ ` ` ` `continue` `; ` ` ` `} ` ` ` ` ` `// Get distance from first point and choose ` ` ` `// nearest one ` ` ` `int` `d = getDistance(points[i][0], points[i][1], points[first][0], points[first][1]); ` ` ` `if` `(minD > d) ` ` ` `{ ` ` ` `minD = d; ` ` ` `second = i; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Pick third point by finding the second closest ` ` ` `// point with different slope. ` ` ` `minD = ` `int` `.MaxValue; ` ` ` `for` `(` `int` `i = 0; i < N; i++) ` ` ` `{ ` ` ` `// if already chosen point then skip them ` ` ` `if` `(i == first || i == second) ` ` ` `{ ` ` ` `continue` `; ` ` ` `} ` ` ` ` ` `// get distance from first point ` ` ` `int` `d = getDistance(points[i][0], points[i][1], points[first][0], points[first][1]); ` ` ` ` ` `/* the slope of the third point with the first ` ` ` `point should not be equal to the slope of ` ` ` `second point with first point (otherwise ` ` ` `they'll be collinear) and among all such ` ` ` `points, we choose point with the smallest ` ` ` `distance */` ` ` `// here cross multiplication is compared instead ` ` ` `// of division comparison ` ` ` `if` `((points[i][0] - points[first][0]) * (points[second][1] - points[first][1]) != (points[second][0] - points[first][0]) * (points[i][1] - points[first][1]) && minD > d) ` ` ` `{ ` ` ` `minD = d; ` ` ` `third = i; ` ` ` `} ` ` ` `} ` ` ` ` ` `Console.WriteLine(points[first][0] + ` `", "` `+ points[first][1]); ` ` ` `Console.WriteLine(points[second][0] + ` `", "` `+ points[second][1]); ` ` ` `Console.WriteLine(points[third][0] + ` `", "` `+ points[third][1]); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main(` `string` `[] args) ` ` ` `{ ` ` ` `int` `[][] points = ` `new` `int` `[][] ` ` ` `{ ` ` ` `new` `int` `[] {0, 0}, ` ` ` `new` `int` `[] {0, 2}, ` ` ` `new` `int` `[] {2, 0}, ` ` ` `new` `int` `[] {2, 2}, ` ` ` `new` `int` `[] {1, 1} ` ` ` `}; ` ` ` `int` `N = points.Length; ` ` ` `triangleWithNoPointInside(points, N); ` ` ` `} ` `} ` ` ` ` ` `// This code is contributed by Shrikant13 ` |

*chevron_right*

*filter_none*

**Output:**

0, 0 1, 1 0, 2

This article is contributed by **Utkarsh Trivedi**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

## Recommended Posts:

- Check whether a given point lies inside a triangle or not
- Area of a triangle inside a parallelogram
- Check if a point is inside, outside or on the ellipse
- Check if a point is inside, outside or on the parabola
- Count Integral points inside a Triangle
- Check whether a point lies inside a sphere or not
- Find a point that lies inside exactly K given squares
- Check if a point lies on or inside a rectangle | Set-2
- Check whether a given point lies inside a rectangle or not
- Check whether a given point lies on or inside the rectangle | Set 3
- How to check if a given point lies inside or outside a polygon?
- Maximum number of 2x2 squares that can be fit inside a right isosceles triangle
- Find intersection point of lines inside a section
- Find minimum radius such that atleast k point lie inside the circle
- Biggest Reuleaux Triangle within a Square which is inscribed within a Right angle Triangle