Reference blog https://blog.csdn.net/yangkunpengD/article/details/51329115
It has been modified in the same way.
Judgment method: three conditions are met at the same time (1: four sides are equal, 2: the side is not 0, 3: there is a right angle)
Train of thought:
(1) Set point.
Sort the points and fix the serial number of four points
Note: when sorting, you can arrange and combine them in ascending or descending order of abscissa or ordinate. There is no problem.
Such as:
bool cmp(point a, point b)
{
if (a.x != b.x)
return a.x < b.x; //If the abscissa is not equal, all points are arranged in ascending order of abscissa
return a.y < b.y;//If the abscissa is equal, all points are arranged in ascending order of ordinate
}
perhaps
bool cmp(point a, point b)
{
if (a.y != b.y)
return a.y > b.y; //If the ordinates are not equal, all points are arranged in descending order of ordinates
return a.x < b.x;//If the ordinates are equal, all points are arranged in ascending order of abscissa
}
(2) Determine the edge.
Calculate the side length.
(3) Judge whether it is a square
Judgment method: three conditions are met at the same time (1: four sides are equal, 2: the side is not 0, 3: there is a right angle)
The code is as follows:
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
struct point
{
double x, y;
} a[4];
bool cmp(point a, point b)
{
if (a.x != b.x)
return a.x < b.x; //If the abscissa is not equal, all points are arranged in ascending order of abscissa
return a.y < b.y;//If the abscissa is equal, all points are arranged in ascending order of ordinate
}
double TwoPointDiatance(point a, point b)//Calculate the distance between two points
{
return sqrt(pow((a.x - b.x), 2) + pow((a.y - b.y), 2));
}
bool IsRightAngle(point a, point b, point c)//Judge whether it is a right angle
{
double x;
x = (a.x - b.x)* (a.x - c.x) + (a.y - b.y)*(a.y - c.y);
if (x < 0.00001)
return 1;
else
return 0;
}
int main()
{
int t, k;
double s1, s2, s3, s4;
cout << "Please enter the number of times you want to play: ";
cin >> t;
cout << "Enter the coordinates of the 4 points:" << endl;
while (t--)
{
for (int i = 0; i < 4; i++)
cin >> a[i].x >> a[i].y;
//Fix point, sort, label point
sort(a, a + 4, cmp);
//Fixed edge
s1 = TwoPointDiatance(a[0], a[2]);
s2 = TwoPointDiatance(a[0], a[1]);
s3 = TwoPointDiatance(a[3], a[1]);
s4 = TwoPointDiatance(a[2], a[3]);
//Whether the analysis is square
if (s1 == s2&&s3 == s4&&s1 == s2&&s1 != 0 && IsRightAngle(a[0], a[1], a[2]))//Three conditions are satisfied at the same time (1: four sides are equal, 2: the side is not 0, 3: there is a right angle)
cout << "Yes" << endl;
else
cout << "No" << endl;
cout << "Left " << t << " Second time." << endl;
cout << "Enter the coordinates of the 4 points:" << endl;
}
return 0;
}
/*
Several sets of square test coordinates:
0 1 1 1 1 0 0 0
0 2 3 -2 -1 -5 -4 -1
0 4 4 7 7 3 3 0
0 1 1 6 5 0 6 5
*/