2011-0004

从 Trac 迁移的文章

这是从旧校内 Wiki 迁移的文章,可能存在一些样式问题,您可以向 memset0 反馈。

原文章内容如下:

by wizard1990
如果起始状态和结束状态相同 则只需判断开始和结束的时候 矩形是否在屏幕内 输出-1或abs(x1-x2)+abs(y1-y2)
如果起始状态和结束状态不同 考虑旋转时会形成一个圆 半径r为矩形中心到矩形的端点距离 
此时 在屏幕内画个另一个(a-2r)*(b-2r)的矩形 套在屏幕内 这样 如果鼠标在这个矩形外部 则无法旋转 如果起点和终点都在这个范围内 就得进行额外的移动 即垂直移动到最近的内部矩阵的边(或端点) 旋转后再移动到目标地点 注意如果w或h小于2r时 如果需要旋转则直接判-1 即使开始和结束时都合法


#include <cstdio>

#include <cmath>

#include <algorithm>

using namespace std;

const double EPS = 1e-6;

int w, h, a[2], d2;

bool in(int x, int y, int t) {
    if (2 * x - a[t] < 0 || 2 * x + a[t] > 2 * w) return false;
    if (2 * y - a[!t] < 0 || 2 * y + a[!t] > 2 * h) return false;
    return true;
}

bool gao(int x) {
    return (4 * x * x < d2);
}

int main() {    
    while (4 == scanf("%d%d%d%d", &w, &h, &a[0], &a[1])) {
        d2 = a[0]*a[0]+a[1]*a[1];
        int x1, y1, t1, x2, y2, t2;
        scanf("%d%d%d%d%d%d", &x1, &y1, &t1, &x2, &y2, &t2);
        t1--, t2--;
        if (!in(x1, y1, t1) || !in(x2, y2, t2)) {
            puts("-1");
            continue;
        }
        double d = sqrt(d2);
        double ans = fabs(x1-x2) + fabs(y1-y2);
        if (d2 > min(w*w, h*h) && t1 != t2) {
            puts("-1");
            continue;
        }

//      printf("r=%f ans=%f\n", r, ans);
        if (t1 != t2) {
            if (gao(x1) && gao(x2)) {
                ans += d - x1 - x2 - fabs(x1-x2);
            } else if (gao(w - x1) && gao(w - x2)) {
                ans += d - 2 * w + x1 + x2 - fabs(x1-x2);
            }
            if (gao(y1) && gao(y2)) {
                ans += d - y1 - y2 - fabs(y1-y2);
            } else if (gao(h - y1) && gao(h - y2)) {
                ans += d - 2 * h + y1 + y2 - fabs(y1-y2);
            }
        }
        printf("%.3f\n", ans);
    }
}

by wizard1990

如果起始状态和结束状态相同 则只需判断开始和结束的时候 矩形是否在屏幕内 输出-1或abs(x1-x2)+abs(y1-y2)

如果起始状态和结束状态不同 考虑旋转时会形成一个圆 半径r为矩形中心到矩形的端点距离

此时 在屏幕内画个另一个(a-2r)*(b-2r)的矩形 套在屏幕内 这样 如果鼠标在这个矩形外部 则无法旋转 如果起点和终点都在这个范围内 就得进行额外的移动 即垂直移动到最近的内部矩阵的边(或端点) 旋转后再移动到目标地点 注意如果w或h小于2r时 如果需要旋转则直接判-1 即使开始和结束时都合法

#include

#include

#include

using namespace std;

const double EPS = 1e-6;

int w, h, a[2], d2;

bool in(int x, int y, int t) {

if (2 * x - a[t] < 0 || 2 * x + a[t] > 2 * w) return false;

if (2 * y - a[!t] < 0 || 2 * y + a[!t] > 2 * h) return false;

return true;

}

bool gao(int x) {

return (4 * x * x < d2);

}

int main() {

while (4 == scanf("%d%d%d%d", &w, &h, &a[0], &a[1])) {

d2 = a[0]*a[0]+a[1]*a[1];

int x1, y1, t1, x2, y2, t2;

scanf("%d%d%d%d%d%d", &x1, &y1, &t1, &x2, &y2, &t2);

t1--, t2--;

if (!in(x1, y1, t1) || !in(x2, y2, t2)) {

puts("-1");

continue;

}

double d = sqrt(d2);

double ans = fabs(x1-x2) + fabs(y1-y2);

if (d2 > min(w*w, h*h) && t1 != t2) {

puts("-1");

continue;

}

// printf("r=%f ans=%f\n", r, ans);

if (t1 != t2) {

if (gao(x1) && gao(x2)) {

ans += d - x1 - x2 - fabs(x1-x2);

} else if (gao(w - x1) && gao(w - x2)) {

ans += d - 2 * w + x1 + x2 - fabs(x1-x2);

}

if (gao(y1) && gao(y2)) {

ans += d - y1 - y2 - fabs(y1-y2);

} else if (gao(h - y1) && gao(h - y2)) {

ans += d - 2 * h + y1 + y2 - fabs(y1-y2);

}

}

printf("%.3f\n", ans);

}

}