判断四个点能否组成正方形&&一个点绕另一个的点旋转
判断四个点组成正方形。
三个条件:
1.四个边相等且长度不为0
(先把点排序,再计算)
2.有一个直角(根据3个点组成的两个向量点乘为0)
struct point
{
double x, y;
} a[4];
bool cmp(point a, point b)
{
if (a.x != b.x)
return a.x < b.x; //如果,横坐标不相等,所有点按横坐标升序排列
return a.y < b.y;//如果横坐标相等,所有点按纵坐标升序排列
}
double TwoPointDistance(point a, point b)//计算两点之间的距离
{
return sqrt(pow((a.x - b.x), 2) + pow((a.y - b.y), 2));
}
bool IsRightAngle(point a, point b, point c)//判断是否为直角
{
double x;
x = (a.x - b.x)* (a.x - c.x) + (a.y - b.y)*(a.y - c.y);
if (x == 0)
return 1;
else
return 0;
}
int Is_Square()
{
sort(a,a+4,cmp);
double s1, s2, s3, s4;
s1 = TwoPointDistance(a[0], a[2]);
s2 = TwoPointDistance(a[0], a[1]);
s3 = TwoPointDistance(a[3], a[1]);
s4 = TwoPointDistance(a[2], a[3]);
if(s1 == s2 && s2 == s3 && s3 == s4 && s1 != 0 && IsRightAngle(a[0], a[1], a[2]))
return 1;
else
return 0;
}
一个点a(x,y)绕另一个点o(x,y)旋转a度。
(逆时针旋转为正,即a;而顺时针旋转为负,即a=-a)
则旋转后:
x=(a.x-o.x)*cos(a) - (a.y-o.y)*sin(a)+o.x;
y=(a.x-o.x)*sin(a) + (a.y-o.y)*cos(a)+o.y;
b.x = ( a.x - o.x)*cos(angle) - (a.y - o.y)*sin(angle) + o.x;
b.y = (a.x - o.x)*sin(angle) + (a.y - o.y)*cos(angle) + o.y;