Check given four-point in an array form a square or not in Python
In this tutorial, we will learn how to check the given four-point in an array form a square or not in Python. This four-point are given in the form of an array and we have to check either these points will form a square or not in Python. A simple solution that comes to our mind that utilizes the concept of coordinate geometry and finds the distance between each point and checks if these all distance are equal then these Point will form a square, these processes will not work here because of the coordinate of points given in the list are not in order. So, how do we solve this problem? The idea is to use the set function.
The steps we follow to solve this problem is the following:-
- Take the array from the user which contains the coordinates of four-point. So, the length of the array will be 8 and here we are solving this problem considering only for positive coordinates.
- If these points form a square then the length of the set of the array will be 2 and we will also check for all elements of the set that the number of occurrences has 4 in the given array.
- If all conditions are satisfied by the given array then these elements will form a square.
Python program to check the given four-point in an array form a square or not.
Let us consider an array given by the user is A1.
A1=[20,10,10,20,20,20,10,10]
Python program:-
A1=[20,10,10,20,20,20,10,10] b=list(set(A1)) if len(b)==2: if A1.count(b[0])==4 and A1.count(b[1])==4: print('These points will form a square.')
Output:-
These points will form a square.
Leave a Reply