Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.71 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <stack>
  4.  
  5. using namespace std;
  6.  
  7. struct Point {
  8. float x, y;
  9. };
  10.  
  11. Point p0;
  12.  
  13. int orientation_test(Point p, Point q, Point r)
  14. {
  15. float det;
  16. det = (q.x * r.y) + (p.x * q.y) + (p.y * r.x) - (q.x * p.y) - (r.x * q.y) - (r.y * p.x);
  17. if (det == 0)
  18. return 0;
  19. if (det > 0) return 1;
  20. return 2;
  21. }
  22.  
  23. float distance_from_origin(Point p1)
  24. {
  25. return (p0.x - p1.x) * (p0.x - p1.x) + (p0.y - p1.y) * (p0.y - p1.y);
  26. }
  27.  
  28. int compare(const void *a,const void *b)
  29. {
  30. Point* p1 = (Point*)a;
  31. Point* p2 = (Point*)b;
  32. int sort_by_or;
  33. sort_by_or = orientation_test(p0, *p1, *p2);
  34. if (sort_by_or == 0)
  35. {
  36. if (distance_from_origin(*p2) >= distance_from_origin(*p1))
  37. return -1;
  38. else return 1;
  39. }
  40. if (sort_by_or == 2)
  41. return -1;
  42. else return 1;
  43. }
  44.  
  45. Point find_middle(stack<Point> & conv_hull )
  46. {
  47. Point p = conv_hull.top();
  48. conv_hull.pop();
  49. Point q = conv_hull.top();
  50. conv_hull.push(p);
  51. return q;
  52. }
  53.  
  54. void set_first(Point &p1, Point &p2)
  55. {
  56. Point aux;
  57. aux = p1;
  58. p1 = p2;
  59. p2 = aux;
  60. }
  61.  
  62. int main()
  63. {
  64. int n,i;
  65. Point input[100];
  66. cout << "Introduceti numarul de puncte : ";
  67. cin >> n;
  68. for (i = 0;i < n;i++)
  69. {
  70. float coord1, coord2;
  71. cout << "Introduceti coordonatele carteziene (x,y) pentru punctul " << i + 1 << ": ";
  72. cin >> coord1 >> coord2;
  73. input[i].x = coord1;
  74. input[i].y = coord2;
  75. }
  76. float y_min = input[0].y;
  77. int poz_min = 0;
  78. for (i = 1;i < n;i++)
  79. {
  80. if (y_min > input[i].y)
  81. {
  82. y_min = input[i].y;
  83. poz_min = i;
  84. }
  85. else if (y_min == input[i].y)
  86. {
  87. if (input[poz_min].x > input[i].x)
  88. {
  89. y_min = input[i].y;
  90. poz_min = i;
  91. }
  92. }
  93. }
  94. set_first(input[0], input[poz_min]);
  95. p0 = input[0];
  96. qsort(&input[1], n - 1, sizeof(Point), compare);
  97. int n_el_dup=1;
  98. Point input2[100];
  99. Point caz_col[100];
  100. for (i = 0;i < n;i++)
  101. {
  102. caz_col[i] = input[i];
  103. }
  104. int cnt_j = 0;
  105. i = 1;
  106. while(i < n)
  107. {
  108. while (i < n - 1 && orientation_test(p0, input[i], input[i + 1]) == 0)
  109. {
  110. i++;
  111. }
  112. input[n_el_dup] = input[i];
  113. n_el_dup++;
  114. i++;
  115. }
  116.  
  117. if (n_el_dup < 3)
  118. {
  119. cout<<"Nu este posibila acoperirea\n";
  120. return 0;
  121. }
  122. stack<Point> conv_hull;
  123. conv_hull.push(input[0]);
  124. conv_hull.push(input[1]);
  125.  
  126. for (i = 2;i < n_el_dup;i++)
  127. {
  128. conv_hull.push(input[i]);
  129. while (orientation_test(find_middle(conv_hull), conv_hull.top(), input[i]) != 2)
  130. {
  131. Point p = conv_hull.top();
  132. conv_hull.pop();
  133. input2[cnt_j] = p;
  134. cnt_j++;
  135. }
  136. conv_hull.push(input[i]);
  137. }
  138.  
  139. for(i=0;i<conv_hull.size();i++)
  140. {
  141. while (!conv_hull.empty())
  142. {
  143. Point p = conv_hull.top();
  144. cout << "(" << p.x << ", " << p.y << ")" << endl;
  145. conv_hull.pop();
  146. }
  147. }
  148.  
  149. return 0;
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement