Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define ZERO 1e-8
- #define INF 1e100
- #define equal(a, b) (fabs((a)-(b))<ZERO)
- using namespace std;
- typedef struct {
- double fX, fY;
- } stPoint;
- long i, iN;
- stPoint tIntersect;
- stPoint stLine1[2], stLine2[2];
- long intersection(stPoint ptLine1[], stPoint ptLine2[], stPoint *ptIntersect) {
- double fSlope1, fSlope2;
- fSlope1 = (equal(ptLine1[0].fX, ptLine1[1].fX)) ? (INF) :
- ((ptLine1[1].fY - ptLine1[0].fY) / (ptLine1[1].fX - ptLine1[0].fX));
- fSlope1 = (equal(ptLine2[0].fX, ptLine2[1].fX)) ? (INF) :
- ((ptLine2[1].fY - ptLine2[0].fY) / (ptLine2[1].fX - ptLine2[0].fX));
- if(equal(fSlope1, fSlope2)) {
- if(equal(ptLine1[0].fY - fSlope1*ptLine1[0].fX, ptLine2[0].fY - fSlope2*ptLine2[0].fX)) cout << "LINE\n";
- else cout << "-1 -1\n";
- return 0;
- }
- ptIntersect -> fX = (fSlope1*ptLine1[0].fX - ptLine1[0].fY - fSlope2*ptLine2[0].fX + ptLine2[0].fY) / (fSlope1 - fSlope2);
- if(equal(ptLine1[0].fX, ptLine1[1].fX)) ptIntersect -> fX = ptLine1[0].fX;
- if(equal(ptLine2[0].fX, ptLine2[1].fX)) ptIntersect -> fX = ptLine2[0].fX;
- ptIntersect -> fY = fSlope1 * (ptIntersect -> fX - ptLine1[0].fX) + ptLine1[0].fY;
- if(equal(ptLine1[0].fX, ptLine1[1].fX)) ptIntersect -> fY = fSlope2 * (ptIntersect -> fX - ptLine2[0].fX) + ptLine2[0].fY;
- return 1;
- }
- int main() {
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);
- cin >> stLine1[0].fX >> stLine1[0].fY >> stLine1[1].fX >> stLine1[1].fY;
- cin >> stLine2[0].fX >> stLine2[0].fY >> stLine2[1].fX >> stLine2[1].fY;
- if(intersection(stLine1, stLine2, &tIntersect)) cout << tIntersect.fX << " " << tIntersect.fY;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement