# Count of parallelograms in a plane

Given some points on a plane, which are distinct and no three of them lie on the same line. We need to find number of Parallelograms with the vertices as the given points.

Examples:

Input : points[] = {(0, 0), (0, 2), (2, 2), (4, 2), (1, 4), (3, 4)} Output : 2 Two Parallelograms are possible by choosing above given point as vertices, which are shown in below diagram.

We can solve this problem by using a special property of parallelograms that diagonals of a parallelogram intersect each other in the middle. So if we get such a middle point which is middle point of more than one line segment, then we can conclude that a parallelogram exists, more accurately if a middle point occurs x times, then diagonals of possible parallelograms can be chosen in ^{x}C_{2} ways, i.e. there will be x*(x-1)/2 parallelograms corresponding to this particular middle point with a frequency x. So we iterate over all pair of points and we calculate their middle point and increase frequency of middle point by 1. At the end, we count number of parallelograms according to the frequency of each distinct middle point as explained above. As we just need frequency of middle point, division by 2 is ignored while calculating middle point for simplicity.

`// C++ program to get number of Parallelograms we ` `// can make by given points of the plane ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Returns count of Parallelograms possible ` `// from given points ` `int` `countOfParallelograms(` `int` `x[], ` `int` `y[], ` `int` `N) ` `{ ` ` ` `// Map to store frequency of mid points ` ` ` `map<pair<` `int` `, ` `int` `>, ` `int` `> cnt; ` ` ` `for` `(` `int` `i=0; i<N; i++) ` ` ` `{ ` ` ` `for` `(` `int` `j=i+1; j<N; j++) ` ` ` `{ ` ` ` `// division by 2 is ignored, to get ` ` ` `// rid of doubles ` ` ` `int` `midX = x[i] + x[j]; ` ` ` `int` `midY = y[i] + y[j]; ` ` ` ` ` `// increase the frequency of mid point ` ` ` `cnt[make_pair(midX, midY)]++; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Iterating through all mid points ` ` ` `int` `res = 0; ` ` ` `for` `(` `auto` `it = cnt.begin(); it != cnt.end(); it++) ` ` ` `{ ` ` ` `int` `freq = it->second; ` ` ` ` ` `// Increase the count of Parallelograms by ` ` ` `// applying function on frequency of mid point ` ` ` `res += freq*(freq - 1)/2 ` ` ` `} ` ` ` ` ` `return` `res; ` `} ` ` ` `// Driver code to test above methods ` `int` `main() ` `{ ` ` ` `int` `x[] = {0, 0, 2, 4, 1, 3}; ` ` ` `int` `y[] = {0, 2, 2, 2, 4, 4}; ` ` ` `int` `N = ` `sizeof` `(x) / ` `sizeof` `(` `int` `); ` ` ` ` ` `cout << countOfParallelograms(x, y, N) << endl; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

Output:

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 contribute@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.

## Recommended Posts:

- Number of parallelograms when n horizontal parallel lines intersect m vertical parallellines
- Distance between a point and a Plane in 3 D
- Mirror of a point through a 3 D plane
- Hammered distance between N points in a 2-D plane
- Program to find X, Y and Z intercepts of a plane
- Number of triangles in a plane if no more than two points are collinear
- Program to determine the octant of the axial plane
- Find mirror image of a point in 2-D plane
- Shortest distance between a Line and a Point in a 3-D plane
- Find the foot of perpendicular of a point in a 3 D plane
- Program to check whether 4 points in a 3-D plane are Coplanar
- Number of Triangles that can be formed given a set of lines in Euclidean Plane
- Program to find equation of a plane passing through 3 points
- Find foot of perpendicular from a point in 2 D plane to a Line
- Maximum number of region in which N non-parallel lines can divide a plane