Advertisement
Guest User

Untitled

a guest
Jan 6th, 2016
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.71 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <cmath>
  5. #include <climits>
  6. #include <vector>
  7. #include <algorithm>
  8. using namespace std;
  9. struct point
  10. {
  11. double x, y;
  12. point(double p = 0, double q = 0)
  13. {
  14. x = p;
  15. y = q;
  16. }
  17. };
  18. struct line
  19. {
  20. point a, b;
  21. line(point x, point y)
  22. {
  23. a = x;
  24. b = y;
  25. nn = 0;
  26. }
  27. int nn;
  28. };
  29. int segNum;
  30. vector <line> Seg;
  31.  
  32. void Input()
  33. {
  34. Seg.clear();
  35. double ax, ay, bx, by;
  36. cin >> segNum;
  37. for (int i = 0; i < segNum; i++)
  38. {
  39. cin >> ax >> ay >> bx >> by;
  40. point start(ax, ay), end(bx, by);
  41. Seg.push_back(line(start, end));
  42. }
  43. }
  44.  
  45. double Direction(point Pi, point Pj, point Pk)
  46. {
  47. return (Pj.x - Pi.x) * (Pk.y - Pi.y) - (Pk.x - Pi.x) * (Pj.y - Pi.y);
  48. }
  49.  
  50. bool isIntersect(line p, line q)
  51. {
  52. double d1, d2, d3, d4;
  53. d1 = Direction(p.a, p.b, q.a);
  54. d2 = Direction(p.a, p.b, q.b);
  55. d3 = Direction(q.a, q.b, p.a);
  56. d4 = Direction(q.a, q.b, p.b);
  57. if (d1 * d2 < 0 && d3 * d4 < 0)
  58. {
  59. return true;
  60. }
  61.  
  62. else return false;
  63. }
  64.  
  65. void Compute()
  66. {
  67. int newSegNum = 0;
  68. for (int i = 0; i < segNum; i++)
  69. {
  70. for (int k = i + 1; k < segNum; k++)
  71. if (isIntersect(Seg[i], Seg[k]))
  72. Seg[i].nn += 2;
  73. newSegNum += (Seg[i].nn + 1);
  74. }
  75. cout << newSegNum << endl;
  76. }
  77.  
  78. int main()
  79. {
  80. int testCase;
  81. while (cin >> testCase)
  82. {
  83. getchar();
  84. getchar();
  85. for (int i = 0; i < testCase; i++)
  86. {
  87. if (i > 0) cout << endl;
  88. Input();
  89. Compute();
  90. }
  91. }
  92. return 0;
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement