Advertisement
Alexvans

Untitled

Jun 27th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.60 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define ZERO 1e-8
  3. #define INF 1e100
  4. #define equal(a, b) (fabs((a)-(b))<ZERO)
  5. using namespace std;
  6.  
  7. typedef struct {
  8.     double fX, fY;
  9. } stPoint;
  10.  
  11. long i, iN;
  12. stPoint tIntersect;
  13. stPoint stLine1[2], stLine2[2];
  14.  
  15. long intersection(stPoint ptLine1[], stPoint ptLine2[], stPoint *ptIntersect) {
  16.     double fSlope1, fSlope2;
  17.  
  18.     fSlope1 = (equal(ptLine1[0].fX, ptLine1[1].fX)) ? (INF) :
  19.         ((ptLine1[1].fY - ptLine1[0].fY) / (ptLine1[1].fX - ptLine1[0].fX));
  20.     fSlope1 = (equal(ptLine2[0].fX, ptLine2[1].fX)) ? (INF) :
  21.         ((ptLine2[1].fY - ptLine2[0].fY) / (ptLine2[1].fX - ptLine2[0].fX));
  22.     if(equal(fSlope1, fSlope2)) {
  23.         if(equal(ptLine1[0].fY - fSlope1*ptLine1[0].fX, ptLine2[0].fY - fSlope2*ptLine2[0].fX)) cout << "LINE\n";
  24.         else cout << "-1 -1\n";
  25.         return 0;
  26.     }
  27.     ptIntersect -> fX = (fSlope1*ptLine1[0].fX - ptLine1[0].fY - fSlope2*ptLine2[0].fX + ptLine2[0].fY) / (fSlope1 - fSlope2);
  28.     if(equal(ptLine1[0].fX, ptLine1[1].fX)) ptIntersect -> fX = ptLine1[0].fX;
  29.     if(equal(ptLine2[0].fX, ptLine2[1].fX)) ptIntersect -> fX = ptLine2[0].fX;
  30.     ptIntersect -> fY = fSlope1 * (ptIntersect -> fX - ptLine1[0].fX) + ptLine1[0].fY;
  31.     if(equal(ptLine1[0].fX, ptLine1[1].fX)) ptIntersect -> fY = fSlope2 * (ptIntersect -> fX - ptLine2[0].fX) + ptLine2[0].fY;
  32.     return 1;
  33. }
  34.  
  35. int main() {
  36.     ios_base::sync_with_stdio(0);
  37.     cin.tie(0);
  38.     cout.tie(0);
  39.  
  40.     cin >> stLine1[0].fX >> stLine1[0].fY >> stLine1[1].fX >> stLine1[1].fY;
  41.     cin >> stLine2[0].fX >> stLine2[0].fY >> stLine2[1].fX >> stLine2[1].fY;
  42.  
  43.     if(intersection(stLine1, stLine2, &tIntersect)) cout << tIntersect.fX << " " << tIntersect.fY;
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement