Given four points in a 2-dimensional space we need to find out whether they make a parallelogram or not.
A parallelogram has four sides. Two opposite sides are parallel and are of same lengths.
Points = [(0, 0), (4, 0), (1, 3), (5, 3)] Above points make a parallelogram. Points = [(0, 0), (2, 0), (4, 0), (2, 2)] Above points does not make a parallelogram as first three points itself are linear.
Problems for checking square and rectangle can be read from Square checking and Rectangle checking but in this problem, we need to check for the parallelogram. The main properties of the parallelogram are that opposite sides of parallelogram are parallel and of equal length and diagonals of parallelogram bisect each other. We use the second property to solve this problem. As there are four points, we can get total 6 midpoints by considering each pair. Now for four points to make a parallelogram, 2 of the midpoints should be equal and rest of them should be different.
In below code, we have created a map, which stores pairs corresponding to each midpoint. After calculating all midpoints, we have iterated over the map and check the occurrence of each midpoint, If exactly one midpoint occurred twice and other have occurred once, then given four points make a parallelogram otherwise not.
Given points form a parallelogram
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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- How to check if given four points form a square
- Find Four points such that they form a square whose sides are parallel to x and y axes
- Check if given four integers (or sides) make rectangle
- Program to check whether 4 points in a 3-D plane are Coplanar
- Check whether it is possible to join two points given on circle such that distance between them is k
- Check whether two points (x1, y1) and (x2, y2) lie on same side of a given line or not
- Check whether triangle is valid or not if three points are given
- Check whether a straight line can be formed using N co-ordinate points
- Find the Missing Point of Parallelogram
- Find all possible coordinates of parallelogram
- Program for Circumference of a Parallelogram
- Find area of parallelogram if vectors of two adjacent sides are given
- Area of a triangle inside a parallelogram
- Perimeter and Area of Varignon's Parallelogram
- Length of a Diagonal of a Parallelogram using the length of Sides and the other Diagonal
- Length of diagonal of a parallelogram using adjacent sides and angle between them
- Program to calculate area of a parallelogram
- Number of Integral Points between Two Points
- Count of obtuse angles in a circle with 'k' equidistant points between 2 given points
- Minimum number of points to be removed to get remaining points on one side of axis