AOJ  CGL_1_A

求点在直线上的投影坐标,考虑定比分点的性质做,注意特判边界条件

#include <cstdio>
#include <cmath>

using namespace std;

double xx1, x2, yy1, y2, xx0, yy0;
int q;
int main()
{
    scanf("%lf%lf%lf%lf", &xx1, &yy1, &x2, &y2);
    scanf("%d", &q);
    for (int i = 1; i <= q; ++i)
    {
        scanf("%lf%lf", &xx0, &yy0);
        double lam = ((x2 - xx1) * (xx0 - xx1) + (y2 - yy1) * (yy0 - yy1)) / ((x2 - xx1) * (x2 - xx1) + (y2 - yy1) * (y2 - yy1));
        if (lam != 1)
        {
            lam = lam / (1 - lam);
            // printf("%lf:::\n",lam);
            printf("%.8lf %.8lf\n", (xx1 + lam * x2) / (1 + lam), (yy1 + lam * y2) / (1 + lam));
        }else printf("%.8lf %.8lf\n",x2,y2);
    }
    return 0;
}
上一篇
下一篇