Projection Aizu - CGL_1_A(求直线交点的模板题)
题意:
求点在线上的投影点
AC代码:
#include <bits/stdc++.h>
using namespace std;
int q;
struct point {
double x, y;
};
struct line {
double x, y;
};
double X(line A, line B) {
return A.x * B.y - A.y * B.x;}
point intersection(point u1, point u2, point v1, point v2) {
point ret = u1;double t = ((u1.x - v1.x) * (v1.y - v2.y) - (u1.y - v1.y) * (v1.x - v2.x)) / ((u1.x - u2.x) * (v1.y - v2.y) - (u1.y - u2.y) * (v1.x - v2.x));ret.x += (u2.x - u1.x) * t;ret.y += (u2.y - u1.y) * t;return ret;
}
point ptoline(point p, point l1, point l2) {
point t = p;t.x += l1.y - l2.y, t.y += l2.x - l1.x;return intersection(p, t, l1, l2);
}
int main() {
point p0, p1, p2, p3, ans;line s1, s2;cin >> p2.x >> p2.y >> p3.x >> p3.y;cin >> q;while (q--) {
cin >> p0.x >> p0.y;ans = ptoline(p0, p2, p3);cout << fixed << setprecision(12) << ans.x << " " << ans.y << endl;}return 0;
}