
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Find Four points such that they form a square whose sides are parallel to x and y axes in Python
Suppose we have n pair of points; we have to find four points so that they can generate a square whose sides are parallel to x and y axes otherwise return "not possible" If we can find more than one square then select the one with whose area is maximum.
So, if the input is like n = 6, points = [(2, 2), (5, 5), (4, 5), (5, 4), (2, 5), (5, 2)], then the output will be 3, points are (2, 2) (5, 2) (2, 5) (5, 5)
To solve this, we will follow these steps −
my_map := a new map
for i in range 0 to n, do
my_map[(points[i,0], points[i,1])] = my_map.[(points[i,0], points[i,1]], 0) + 1
side := -1
x := -1
y := -1
for i in range 0 to n, do
my_map[points[i, 0], points[i, 1]] := my_map[points[i, 0], points[i, 1]] - 1
for j in range 0 to n, do
my_map[points[j, 0], points[j, 1]] := my_map[points[j, 0], points[j, 1]] - 1
if (i is not same as j and (points[i,0]-points[j,0]) is same as (points[i,1]- points[j,1])), then
if my_map[(points[i,0], points[j, 1])] > 0 and my_map[(points[j,0], points[i,1])] > 0, then
if (side < |points[i,0] - points[j,0]| or (side is same as |points[i,0] - points[j,0]| and ((points[i,0] * points[i,0] + points[i,1] * points[i,1]) < (x * x + y * y)))) −
x := points[i, 0]
y := points[i, 1]
side := |points[i,0] - points[j,0]|
my_map[points[j, 0], points[j, 1]] := my_map[points[j, 0], points[j, 1]] + 1
my_map[points[i, 0], points[i, 1]] := my_map[points[i, 0], points[i, 1]] + 1
if side is not same as -1, then
display side
display points (x,y), (x+side, y), (x,y + side), (x+side, y+side)
otherwise,
display "No such square"
Example
Let us see the following implementation to get better understanding −
def get_square_points(points,n): my_map = dict() for i in range(n): my_map[(points[i][0], points[i][1])] = my_map.get((points[i][0], points[i][1]), 0) + 1 side = -1 x = -1 y = -1 for i in range(n): my_map[(points[i][0], points[i][1])]-=1 for j in range(n): my_map[(points[j][0], points[j][1])]-=1 if (i != j and (points[i][0]-points[j][0]) == (points[i][1]-points[j][1])): if (my_map[(points[i][0], points[j][1])] > 0 and my_map[(points[j][0], points[i][1])] > 0): if (side < abs(points[i][0] - points[j][0]) or (side == abs(points[i][0] - points[j][0]) and ((points[i][0] * points[i][0] + points[i][1] * points[i][1]) < (x * x + y * y)))): x = points[i][0] y = points[i][1] side = abs(points[i][0] - points[j][0]) my_map[(points[j][0], points[j][1])] += 1 my_map[(points[i][0], points[i][1])] += 1 if (side != -1): print("Side:", side) print("Points:", (x,y), (x+side, y), (x,y + side), (x+side, y+side)) else: print("No such square") n = 6 points=[(2, 2), (5, 5), (4, 5), (5, 4), (2, 5), (5, 2)] get_square_points(points, n)
Input
6, [(2, 2), (5, 5), (4, 5), (5, 4), (2, 5), (5, 2)]
Output
Side: 3 Points: (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 C++
- $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)$.
- Program to find number of pairs between x, whose multiplication is x and they are coprime in Python
- 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.
