Check whether four points make a parallelogram
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. 
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.
C++
// C++ code to test whether four points make a// parallelogram or not#include <bits/stdc++.h>using namespace std;// structure to represent a pointstruct point { double x, y; point() { } point(double x, double y) : x(x), y(y) { } // defining operator < to compare two points bool operator<(const point& other) const { if (x < other.x) { return true; } else if (x == other.x) { if (y < other.y) { return true; } } return false; }};// Utility method to return mid point of two pointspoint getMidPoint(point points[], int i, int j){ return point((points[i].x + points[j].x) / 2.0, (points[i].y + points[j].y) / 2.0);}// method returns true if point of points array form// a parallelogrambool isParallelogram(point points[]){ map<point, vector<point> > midPointMap; // looping over all pairs of point to store their // mid points int P = 4; for (int i = 0; i < P; i++) { for (int j = i + 1; j < P; j++) { point temp = getMidPoint(points, i, j); // storing point pair, corresponding to // the mid point midPointMap[temp].push_back(point(i, j)); } } int two = 0, one = 0; // looping over (midpoint, (corresponding pairs)) // map to check the occurrence of each midpoint for (auto x : midPointMap) { // updating midpoint count which occurs twice if (x.second.size() == 2) two++; // updating midpoing count which occurs once else if (x.second.size() == 1) one++; // if midpoint count is more than 2, then // parallelogram is not possible else return false; } // for parallelogram, one mid point should come // twice and other mid points should come once if (two == 1 && one == 4) return true; return false;}// Driver code to test above methodsint main(){ point points[4]; points[0] = point(0, 0); points[1] = point(4, 0); points[2] = point(1, 3); points[3] = point(5, 3); if (isParallelogram(points)) cout << "Given points form a parallelogram"; else cout << "Given points does not form a " "parallelogram"; return 0;} |
Java
import java.util.*;public class Main { // structure to represent a point static class Point { double x, y; Point() { } Point(double x, double y) { this.x = x; this.y = y; } // defining equals and hashCode method to compare two points @Override public boolean equals(Object obj) { if (obj == this) return true; if (!(obj instanceof Point)) return false; Point other = (Point) obj; return Double.compare(x, other.x) == 0 && Double.compare(y, other.y) == 0; } @Override public int hashCode() { return Objects.hash(x, y); } } // Utility method to return mid point of two points static Point getMidPoint(Point[] points, int i, int j) { return new Point((points[i].x + points[j].x) / 2.0, (points[i].y + points[j].y) / 2.0); } // method returns true if point of points array form // a parallelogram static boolean isParallelogram(Point[] points) { Map<Point, List<Point>> midPointMap = new HashMap<>(); // looping over all pairs of point to store their // mid points int P = 4; for (int i = 0; i < P; i++) { for (int j = i + 1; j < P; j++) { Point temp = getMidPoint(points, i, j); // storing point pair, corresponding to // the mid point if (!midPointMap.containsKey(temp)) { midPointMap.put(temp, new ArrayList<>()); } midPointMap.get(temp).add(new Point(i, j)); } } int two = 0, one = 0; // looping over (midpoint, (corresponding pairs)) // map to check the occurrence of each midpoint for (List<Point> pointsList : midPointMap.values()) { int size = pointsList.size(); // updating midpoint count which occurs twice if (size == 2) { two++; } // updating midpoing count which occurs once else if (size == 1) { one++; } // if midpoint count is more than 2, then // parallelogram is not possible else { return false; } } // for parallelogram, one mid point should come // twice and other mid points should come once if (two == 1 && one == 4) { return true; } return false; } // Driver code to test above methods public static void main(String[] args) { Point[] points = new Point[4]; points[0] = new Point(0, 0); points[1] = new Point(4, 0); points[2] = new Point(1, 3); points[3] = new Point(5, 3); if (isParallelogram(points)) { System.out.println("Given points form a parallelogram"); } else { System.out.println("Given points do not form a parallelogram"); } }}// This code is contributed by Prince Kumar |
C#
// C# code to test whether four points make a// parallelogram or notusing System;using System.Collections.Generic;// structure to represent a pointpublic struct Point{ public double x, y; public Point(double x, double y) { this.x = x; this.y = y; } // defining operator < to compare two points public static bool operator < (Point p1, Point p2) { if (p1.x < p2.x) return true; else if (p1.x == p2.x) { if (p1.y < p2.y) return true; } return false; } // defining operator > to compare two points public static bool operator > (Point p1, Point p2) { if (p1.x > p2.x) return true; else if (p1.x == p2.x) { if (p1.y > p2.y) return true; } return false; }}public class GFG { // Utility method to return mid point of two points static Point getMidPoint(Point[] points, int i, int j) { return new Point((points[i].x + points[j].x) / 2.0, (points[i].y + points[j].y) / 2.0); } // method returns true if point of points array form // a parallelogram static bool isParallelogram(Point[] points) { Dictionary<Point, List<Point> > midPointMap = new Dictionary<Point, List<Point> >(); // looping over all pairs of point to store their // mid points int P = 4; for (int i = 0; i < P; i++) { for (int j = i + 1; j < P; j++) { Point temp = getMidPoint(points, i, j); // storing point pair, corresponding to // the mid point if (midPointMap.ContainsKey(temp)) { midPointMap[temp].Add(new Point(i, j)); } else { midPointMap[temp] = new List<Point>(); midPointMap[temp].Add(new Point(i, j)); } } } int two = 0, one = 0; // looping over (midpoint, (corresponding pairs)) // map to check the occurrence of each midpoint foreach(var x in midPointMap) { // updating midpoint count which occurs twice if (x.Value.Count == 2) two++; // updating midpoing count which occurs once else if (x.Value.Count == 1) one++; // if midpoint count is more than 2, then // parallelogram is not possible else return false; } // for parallelogram, one mid point should come // twice and other mid points should come once if (two == 1 && one == 4) return true; return false; } // Driver code to test above methods static public void Main(string[] args) { Point[] points = new Point[4]; points[0] = new Point(0, 0); points[1] = new Point(4, 0); points[2] = new Point(1, 3); points[3] = new Point(5, 3); if (isParallelogram(points)) { Console.WriteLine( "Given points form a parallelogram"); } else { Console.WriteLine( "Given points do not form a parallelogram"); } }}// This code is contributed by prasad264 |
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
This article is contributed by Aarti_Rathi and Utkarsh Trivedi. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@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.





.gif)

Please Login to comment...