
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check If Four Points Form a Square Using C++
A square is a four-sided polygon with all sides equal and all angles equal to 90 degrees. In computational geometry, determining whether four given points form a square is a common problem. In this article, we will discuss multiple approaches to check whether four given points in a 2D plane form a square using C++.
When can a four-sided polygon be called a Square?
If we are given four points, we have to determine whether it is a square or not. To determine if four points (A, B, C, D) form a square, the given conditions below must be satisfied:
- All four sides must be equal.
- The two diagonals must be equal.
- The angle between two consecutive sides must be 90 degrees.
Example 1
-
Input:
A(0, 0), B(0, 2), C(2, 2), D(2, 0) - Output: Yes
Explanation:
The length of each side of the square is: AB = BC = CD = DA = 2
Diagonals: AC = BD = sqrt(8)
Hence, all conditions of a square are satisfied. So, the given points form a square.
Example 2
-
Input:
A(1, 3), B(2, 7), C(3, 8), D(9, 5) - Output: No
Explanation:
The length of each side of the square is not equal. So, the given points do not form a square.Different Approaches to Check if Given Four Points Form a Square.
Using Distance Formula Approach
This is the simplest and most direct approach. In this approach, we calculate the distances between all four points, compare the length of two diagonals, and check if the given conditions for a square are satisfied.
Steps for Implementation
- Calculate all six distances (i.e., four sides and two diagonals) using the four points given.
- Check if the four sides are equal.
- Check if the two diagonals are equal.
- Check if the diagonals are greater than the sides.
- If all the above conditions become true, then the given points form a square.
Implementation Code
#include <iostream> #include <vector> #include <algorithm> using namespace std; struct Point { int x, y; }; int distanceSquare(Point p1, Point p2) { return (p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y); } bool isSquare(vector<Point> points) { if (points.size() != 4) { return false; } vector<int> distances; for (int i = 0; i < 4; ++i) { for (int j = i + 1; j < 4; ++j) { distances.push_back(distanceSquare(points[i], points[j])); } } sort(distances.begin(), distances.end()); return (distances[0] == distances[1] && distances[1] == distances[2] && distances[2] == distances[3] && distances[4] == distances[5] && distances[4] == 2 * distances[0]); } int main() { vector<Point> points = {{0, 0}, {0, 2}, {2, 2}, {2, 0}}; if (isSquare(points)) { cout << "Yes, the given points form a square." << endl; } else { cout << "No, the given points do not form a square." << endl; } return 0; }
Output
Yes, the given points form a square.
Time Complexity: O(1)
Space Complexity: O(1)
Using Vector Cross Product Approach
This approach makes sure that all angles are 90 degrees by checking the dot product between two consecutive sides.
Steps for Implementation
- Compute vectors for consecutive edges.
- Compute the cross product to check perpendicularity.
- Ensure all sides are equal.
Implementation Code
#include<iostream> using namespace std; struct Point { int x, y; }; int dotProduct(Point a, Point b) { return (a.x * b.x + a.y * b.y); } bool isSquare(Point p1, Point p2, Point p3, Point p4) { Point v1 = {p2.x - p1.x, p2.y - p1.y}; Point v2 = {p3.x - p2.x, p3.y - p2.y}; Point v3 = {p4.x - p3.x, p4.y - p3.y}; Point v4 = {p1.x - p4.x, p1.y - p4.y}; return dotProduct(v1, v2) == 0 && dotProduct(v2, v3) == 0 && dotProduct(v3, v4) == 0; } int main() { Point p1 = {0, 0}, p2 = {0, 2}, p3 = {2, 2}, p4 = {2, 0}; if (isSquare(p1, p2, p3, p4)) cout << "Yes, the given points form a square."; else cout << "No, the given points do not form a square."; return 0; }
Output
Yes, the given points form a square.
Time Complexity: O(1)
Space Complexity: O(1)