1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
| #include <stdio.h> #include <string.h> #include <math.h> struct Point { double fX; double fY; }; Point beg[2], end[2]; int nN; const double fPrecision = 1e-8;
double Det(double fX1, double fY1, double fX2, double fY2) { return fX1 * fY2 - fX2 * fY1; }
double Cross(Point a, Point b, Point c) { return Det(b.fX - a.fX, b.fY - a.fY, c.fX - a.fX, c.fY - a.fY); }
int DblCmp(double fD) { if (fabs(fD) < fPrecision) { return 0; } else { return (fD > 0 ? 1 : -1); } }
double DotDet(double fX1, double fY1, double fX2, double fY2) { return fX1 * fX2 + fY1 * fY2; }
double Dot(Point a, Point b, Point c) { return DotDet(b.fX - a.fX, b.fY - a.fY, c.fX - a.fX, c.fY - a.fY); }
int BetweenCmp(Point a, Point b, Point c) { return DblCmp(Dot(a, b, c)); }
int SegCross(Point a, Point b, Point c, Point d, Point p) { double s1, s2, s3, s4; int d1, d2, d3, d4; d1 = DblCmp(s1 = Cross(a, b, c)); d2 = DblCmp(s2 = Cross(a, b, d)); d3 = DblCmp(s3 = Cross(c, d, a)); d4 = DblCmp(s4 = Cross(c, d, b));
Point e, f; e.fX = a.fX - b.fX; e.fY = a.fY - b.fY; f.fX = c.fX - d.fX; f.fY = c.fY - d.fY; if (DblCmp(Det(e.fX, e.fY, f.fX, f.fY)) == 0) { if (d1 * d2 > 0 d3 * d4 > 0) { return 0; } else { return 2; } }
p.fX = (c.fX * s2 - d.fX * s1) / (s2 - s1); p.fY = (c.fY * s2 - d.fY * s1) / (s2 - s1); return 1; }
int main() { while (scanf("%d", &nN) == 1) { printf("INTERSECTING LINES OUTPUT\n"); Point p; for (int i = 0; i < nN; ++i) { scanf("%lf%lf%lf%lf", &beg[0].fX, &beg[0].fY, &end[0].fX, &end[0].fY); scanf("%lf%lf%lf%lf", &beg[1].fX, &beg[1].fY, &end[1].fX, &end[1].fY); int nRet = SegCross(beg[0], end[0], beg[1], end[1], p); if (nRet == 0) { printf("NONE\n"); } else if (nRet == 1) { printf("POINT %.2f %.2f\n", p.fX, p.fY); } else { printf("LINE\n"); } } printf("END OF OUTPUT\n"); }
return 0; }
|