We are given four segments as a pair of coordinates of their end points. We need to tell whether those four line segments make a rectangle or not.
Input : segments = [(4, 2), (7, 5), (2, 4), (4, 2), (2, 4), (5, 7), (5, 7), (7, 5)] Output : Yes Given these segment make a rectangle of length 3X2. Input : segment = [(7, 0), (10, 0), (7, 0), (7, 3), (7, 3), (10, 2), (10, 2), (10, 0)] Output : Not These segments do not make a rectangle. Above examples are shown in below diagram.
This problem is mainly an extension of How to check if given four points form a square
We can solve this problem by using properties of a rectangle. First, we check total unique end points of segments, if count of these points is not equal to 4 then the line segment can’t make a rectangle. Then we check distances between all pair of points, there should be at most 3 different distances, one for diagonal and two for sides and at the end we will check the relation among these three distances, for line segments to make a rectangle these distance should satisfy Pythagorean relation because sides and diagonal of rectangle makes a right angle triangle. If they satisfy mentioned conditions then we will flag polygon made by line segment as rectangle otherwise not.
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 email@example.com. 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.
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- How to check if two given line segments intersect?
- Given n line segments, find if any two segments intersect
- Check whether a given point lies inside a rectangle or not
- Check if a point lies on or inside a rectangle | Set-2
- Check if given four integers (or sides) make rectangle
- Check if any point overlaps the given Circle and Rectangle
- Check whether a given point lies on or inside the rectangle | Set 3
- Ratio of area of a rectangle with the rectangle inscribed in it
- Largest subset of rectangles such that no rectangle fit in any other rectangle
- How to check if given four points form a square
- Check if the given 2-D points form T-shape or not
- Check whether a very large number of the given form is a multiple of 3.
- Check whether nodes of Binary Tree form Arithmetic, Geometric or Harmonic Progression
- Maximum number of segments that can contain the given points
- Split the array into odd number of segments of odd lengths
Improved By : harrypotter0