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.
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.
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 firstname.lastname@example.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.
- 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 parabola
- Check if a point is inside, outside or on the ellipse
- Count Integral points inside a Triangle
- Find a point that lies inside exactly K given squares
- Check whether a given point lies inside a rectangle or not
- Check whether a point lies inside a sphere or not
- Check whether a given point lies on or inside the rectangle | Set 3
- Check if a point lies on or inside a rectangle | Set-2
- 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
Improved By : shrikanth13