用矢量计算:
cpp
class Solution {
public:
bool checkOverlap(int radius, int xCenter, int yCenter, int x1, int y1, int x2, int y2) {
//矩形中心
float Tx=(float)(x1+x2)/2;
float Ty=(float)(y1+y2)/2;
//强行进行对称操作,只考虑第一象限
if(xCenter<Tx){
xCenter=2*Tx-xCenter;
}
if(yCenter<Ty){
yCenter=2*Ty-yCenter;
}
//圆心到对角顶点的距离
int rDTx=xCenter-x2;
int rDTy=yCenter-y2;
//四种情况
if(rDTx>0&&rDTy>0){
return rDTx*rDTx+rDTy*rDTy<=radius*radius;
}
else if(rDTx<=0&&rDTy>0){
return rDTy<=radius;
}
else if(rDTy<=0&&rDTx>0){
return rDTx<=radius;
}
else if(rDTx<=0&&rDTy<=0){
return true;
}
return false;
}
};