Intersecting Lines POJ

题目链接

  • 题解:主要是了解了直线交点的求法是根据相似三角形然后用向量起点加上某个比例而成的。
  • 代码:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <vector>

using namespace std;
typedef  long double ld;
const ld eps = 1e-8;
const int N = 50009;
struct Point {
    ld x, y;
    Point(ld X = 0, ld Y = 0) { x = X, y = Y; }
    Point operator-(Point a) { return Point(x - a.x, y - a.y); }
    Point operator+(Point a) { return Point(x + a.x, y + a.y); }
    ld operator*(Point a) { return x * a.y - y * a.x; }
    Point operator*(ld a) {return Point(x * a,y * a);}
    ld dis() {
        return sqrt(x * x + y * y);
    }
    void out() {
        printf("%.2Lf %.2Lf
", x,y);
    }
} p[N];
int dcmp(ld a, ld b) {
    if (fabs(a-b)< eps)return 0;
    else if (a > b)return 1;
    else return -1;
}
typedef Point Vector;
struct Line {
    Point p1, p2;
    Vector v;
    //Line(Point P1= {}, Point P2 = {}, Vector V = {}) {p1 = P1, p2=P2, v = V;}
    void out() {
        printf("%.2Lf %.2Lf -> %.2Lf %.2Lf
", p1.x,p1.y, p2.x, p2.y);
    }
}L[N];
void out(ld x) {
    printf("%.2Lf
", x);
}
Point GetCross(Line a, Line b) {
    Vector v = b.p1 - a.p1;
    return b.p1 + (b.v * ((v * a.v) / (a.v * b.v)));
}
int cnt[N];
int n, m;
void solve() {
    n = 2;
    int p_cnt = 0;
    for (int i = 1; i <= n; i++) {
        ld x1, x2;
        scanf("%Lf%Lf", &x1, &x2);p_cnt++;
        p[p_cnt].x = x1, p[p_cnt].y = x2;
        p[p_cnt] = p[p_cnt];
        scanf("%Lf%Lf", &x1, &x2);
        p_cnt++;
        p[p_cnt].x = x1, p[p_cnt].y = x2;
        p[p_cnt] = p[p_cnt];
        L[i].p1 = p[p_cnt - 1];
        L[i].p2 = p[p_cnt];
        L[i].v = p[p_cnt] - p[p_cnt - 1];
    }
    //out(L[1].v * L[2].v);
    if (dcmp(L[1].v * L[2].v, 0) == 0) {
        Vector v1 = L[2].p1 - L[1].p1;
        if (dcmp(L[1].v * v1, 0) == 0)
        puts("LINE");
        else puts("NONE");
    }  else {
        Point ans = GetCross(L[1], L[2]);
        printf("POINT %.2Lf %.2Lf
", ans.x, ans.y);
    }
}
signed main() {
    puts("INTERSECTING LINES OUTPUT");
    int t = 1;scanf("%d", &t);
    while (t--) {
        solve();
    }
    puts("END OF OUTPUT");
    return 0;
}
原文地址:https://www.cnblogs.com/Xiao-yan/p/14641136.html