
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Find Four points such that they form a square whose sides are parallel to x and y axes in C++
Concept
With respect of given ‘n’ pair of points, our task is to determine four points so that they form a square whose sides are parallel to x and y axes or else display “No such square”. It should be noted that if more than one square is possible then select the one with the maximum area.
Input
n = 6, points = (2, 2), (5, 5), (4, 5), (5, 4), (2, 5), (5, 2)
Output
Side of the square is: 3, points of the square are 2, 2 5, 2 2, 5 5, 5
Explanation
The points 2, 2 5, 2 2, 5 5, 5 form a square of side 3
Input
n= 6, points= (2, 2), (5, 6), (4, 5), (5, 4), (8, 5), (4, 2)
Output
No such square
Method
Simple method − Select all possible pairs of points with four nested loops and then verifyif the points form a square which is parallel to principal axes. It has been seen that if yes then verify if it is the largest square so far in terms of the area and store the result, and then print the result at the end of the program.
Time Complexity − O(N^4)
Efficient method − Build a nested loop for top right and bottom left corner of the square and generate a square with those two points, then verify if the other two points which were assumed actually exist. Now to verify if a point exists or not, build a map and store the points in the map to decrease the time to verify whether the points exist.Moreover, keep in check the largest square by area so far and display it in the end.
Example
// C++ implemenataion of the above approach #include <bits/stdc++.h> using namespace std; // Determine the largest square void findLargestSquare1(long long int points1[][2], int n1){ // Used to map to store which points exist map<pair<long long int, long long int>, int> m1; // mark the available points for (int i = 0; i < n1; i++) { m1[make_pair(points1[i][0], points1[i][1])]++; } long long int side1 = -1, x1 = -1, y1 = -1; // Shows a nested loop to choose the opposite corners of square for (int i = 0; i < n1; i++) { // Used to remove the chosen point m1[make_pair(points1[i][0], points1[i][1])]--; for (int j = 0; j < n1; j++) { // Used to remove the chosen point m1[make_pair(points1[j][0], points1[j][1])]--; // Verify if the other two points exist if (i != j && (points1[i][0]-points1[j][0]) == (points1[i][1]-points1[j][1])){ if (m1[make_pair(points1[i][0], points1[j][1])] > 0 && m1[make_pair(points1[j][0], points1[i][1])] > 0) { // So if the square is largest then store it if (side1 < abs(points1[i][0] - points1[j][0]) || (side1 == abs(points1[i][0] -points1[j][0]) && ((points1[i][0] * points1[i][0]+ points1[i][1] * points1[i][1]) < (x1 * x1 + y1 * y1)))) { x1 = points1[i][0]; y1 = points1[i][1]; side1 = abs(points1[i][0] - points1[j][0]); } } } // Used to add the removed point m1[make_pair(points1[j][0], points1[j][1])]++; } // Used to add the removed point m1[make_pair(points1[i][0], points1[i][1])]++; } // Used to display the largest square if (side1 != -1) cout << "Side of the square is : " << side1 << ", \npoints of the square are " << x1 << ", " << y1<< " "<< (x1 + side1) << ", " << y1 << " " << (x1) << ", " << (y1 + side1) << " " << (x1 + side1) << ", " << (y1 + side1) << endl; else cout << "No such square" << endl; } //Driver code int main(){ int n1 = 6; // given points long long int points1[n1][2]= { { 2, 2 }, { 5, 5 }, { 4, 5 }, { 5, 4 }, { 2, 5 }, { 5, 2 }}; // Determine the largest square findLargestSquare1(points1, n1); return 0; }
Output
Side of the square is : 3, points of the square are 2, 2 5, 2 2, 5 5, 5
- Related Articles
- Find Four points such that they form a square whose sides are parallel to x and y axes in Python
- $ABCD$ is a square, $X$ and $Y$ are points on sides $AD$ and $BC$ respectively such that $AY = BX$. Prove that $BY = AX$ and $\angle BAY = \angle ABX$.
- Check if given four points form a Square
- Find a relation between $x$ and $y$ such that the point $(x, y)$ is equidistant from the points $(3, 6)$ and $(-3, 4)$.
- A parallelogram whose all sides are equal opposite angles are equal opposite sides are parallel is called a rhombus. Why? Why not square?
- Find number of pairs (x, y) in an array such that x^y > y^x in C++
- Find a distinct pair (x, y) in given range such that x divides y in C++
- Determine graphically the coordinates of the vertices of a triangle, the equations of whose sides are:$y=x, y=2x$ and $y+x=6$
- If \( a \) and \( b \) are distinct positive primes such that \( \sqrt[3]{a^{6} b^{-4}}=a^{x} b^{2 y} \), find \( x \) and \( y \).
- C Program to check if the points are parallel to X axis or Y axis
- If $D$ and $E$ are points on sides $AB$ and $AC$ respectively of a $\triangle ABC$ such that $DE \parallel BC$ and $BD = CE$. Prove that $\triangle ABC$ is isosceles.
- If the points $(-2, -1), (1, 0), (x, 3)$ and $(1, y)$ form a parallelogram, find the values of $x$ and $y$.
- If points $( a,\ 0),\ ( 0,\ b)$ and $( x,\ y)$ are collinear, prove that $\frac{x}{a}+\frac{y}{b}=1$.
- Let ABCD be a square of side 2a. Find the coordinates of the vertices of this square whenThe centre of the square is at the origin and coordinate axes are parallel to the sides AB and AD respectively.
- Find a relation between $x$ and $y$, if the points $(x, y), (1, 2)$ and $(7, 0)$ are collinear.
