就一道算法题,求圆(半径+中心坐标)和矩形(左下右上坐标)有没有交集,代码:
int main() { double radius, x_center, y_center, x1, y1, x2, y2; scanf("%lf %lf %lf %lf %lf %lf %lf", &radius, &x_center, &y_center, &x1, &y2, &x2, &y2); vector<double> vec1 = { abs(x_center - (x1 + x2) / 2), abs(y_center - (y1 + y2) / 2) }; vector<double> vec2 = { x2 - (x1 + x2) / 2, y2 - (y1 + y2) / 2 }; vector<double> vec3 = { max(0., vec1[0] - vec2[0]), max(0., vec1[1] - vec2[1]) }; string res = vec3[0] * vec3[0] + vec3[1] * vec3[1] <= radius * radius ? "True" : "False"; cout << res << endl; }只能过91%,改精度也没用,不知道为啥。
全部评论
(2) 回帖