VOOZH about

URL: https://www.geeksforgeeks.org/dsa/check-whether-four-points-make-parallelogram/

⇱ Check whether four points make a parallelogram - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Check whether four points make a parallelogram

Last Updated : 23 Apr, 2025

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.

👁 parallelogram.

Examples:

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. 

Output:

Given points form a parallelogram

Time Complexity: O(p2logp) , where p is number of points
Auxiliary Space: O(p2), where p is number of points

  • Another approach to check if four points form a parallelogram is to use vector operations. We can calculate the vectors formed by the pairs of points and check if they satisfy the properties of a parallelogram.
  • Here is the algorithm for this approach:
  • Take four points A, B, C, and D as input.
  • Calculate vectors AB and CD using the formula (B - A) and (D - C).
  • Calculate vectors AC and BD using the formula (C - A) and (D - B).
  • Check if AB and CD are parallel by taking their cross product. If the cross product is zero, then they are parallel.
  • Check if AC and BD are parallel by taking their cross product. If the cross product is zero, then they are parallel.
  • If AB and CD are parallel and AC and BD are parallel, then the points form a parallelogram.

Here is the C++ code implementation of this approach:


Output:

Given points form a parallelogram

Time Complexity:  O(n^2logn) where n is the number of points
Auxiliary Space:  O(n^2) since the map data structure is used to store the midpoints, and the worst-case number of midpoints that can be stored is n^2.


Comment
Article Tags:
Article Tags: