题目链接
题意:
判断两条直线的关系:相交 平行 重合
代码:
#include <stdio.h> #include <cstring> #include <cmath> #include <iostream> #include <queue> #include <map> #include <list> #include <utility> #include <set> #include <algorithm> #include <deque> #include <iomanip> #include <vector> #define mem(arr, num) memset(arr, 0, sizeof(arr)) #define _for(i, a, b) for (int i = a; i <= b; i++) #define __for(i, a, b) for (int i = a; i >= b; i--) #define IO \ ios::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); using namespace std; typedef long long ll; typedef vector<int> vi; const ll INF = 0x3f3f3f3f; const int mod = 1e9 + 7; const int N = 5000 + 5; string str[3] = {"POINT", "LINE", "NONE"}; int main() { int n; cin >> n; cout << "INTERSECTING LINES OUTPUT" << endl; while (n--) { double x1, x2, x3, x4, y1, y2, y3, y4; cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3 >> x4 >> y4; double _x1 = x2 - x1, _x2 = x4 - x3, _y1 = y2 - y1, _y2 = y4 - y3; if (_x1 == 0 || _x2 == 0) { //斜率不存在; if (_x1 == 0 && _x2 == 0) { if (x1 == x3) cout << str[1] << endl; else cout << str[2] << endl; } else if (_x2 != 0) { cout << str[0] << " " << setiosflags(ios::fixed) << setprecision(2) << x1 << " " << (_y2 / _x2) * x1 + (y3 - (_y2 / _x2)*x3) << endl; } else if (_x1 != 0){ cout << str[0] << " " << setiosflags(ios::fixed) << setprecision(2) << x3 << " " << (_y1 / _x1) * x3 + (y1 - (_y1 / _x1)*x1) << endl; } } else { double k1 = _y1/_x1, k2 = _y2/_x2; double b1 = y1 - k1*x1, b2 = y3 - k2 * x3; if(k1 == k2){ if(b1 == b2) cout << str[1] << endl; else cout << str[2] << endl; } else { cout << str[0] << " " << setiosflags(ios::fixed) << setprecision(2) << (b2-b1)/(k1-k2) << " " <<k1 * (b2-b1)/(k1-k2) + b1 <<endl; } } } cout << "END OF OUTPUT" << endl; return 0; }