Advertisement
Guest User

gc

a guest
Dec 19th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.26 KB | None | 0 0
  1. #include <stack>
  2. #include <iostream>
  3. #include <vector>
  4. #include <algorithm>
  5. #include <math.h>
  6. using namespace std;
  7. struct Point
  8. {
  9. int x, y;
  10. };
  11. Point p0;
  12. int pozitie_punct(Point ac1[],Point ac2[], int n1,int n2)
  13. {
  14. if(n1!=n2)
  15. {cout<<"Punctul A se afla in exterior"; // acoperirile convexe au numar diferit de elemente, deci nu coincid
  16. return 0;}
  17.  
  18. int verif=0;
  19. for(int i=0;i<n1;i++)
  20. if(ac1[i].x!=ac2[i].x||ac1[i].y!=ac2[i].y)
  21. verif=1;
  22.  
  23. if(verif==0)
  24. return 1;
  25.  
  26. cout<<"Punctul A se afla in exterior";
  27. return 0;
  28. //acoperirile convexe coincid
  29. //punctul se afla fie in interior, fie pe una din laturi
  30.  
  31.  
  32.  
  33.  
  34. }
  35.  
  36.  
  37. Point nextToTop(stack<Point> &S)
  38. {
  39. Point p = S.top();
  40. S.pop();
  41. Point res = S.top();
  42. S.push(p);
  43. return res;
  44. }
  45.  
  46. // A utility function to swap two points
  47. int swap(Point &p1, Point &p2)
  48. {
  49. Point temp = p1;
  50. p1 = p2;
  51. p2 = temp;
  52. }
  53.  
  54. // A utility function to return square of distance
  55. // between p1 and p2
  56. int distSq(Point p1, Point p2)
  57. {
  58. return (p1.x - p2.x)*(p1.x - p2.x) +
  59. (p1.y - p2.y)*(p1.y - p2.y);
  60. }
  61.  
  62. // Fac o functie care imi gaseste orientarea tripletului (p, q, r).
  63. // si imi va return una din cele 3 valori:
  64. // 0 --> p, q si r sunt coliniare
  65. // 1 --> clockwise
  66. // 2 --> counterclockwise
  67. int orientation(Point p, Point q, Point r)
  68. {
  69. int val = (q.y - p.y) * (r.x - q.x) -
  70. (q.x - p.x) * (r.y - q.y);
  71.  
  72. if (val == 0) return 0; // colinear
  73. return (val > 0)? 1: 2; // clock or counterclock wise
  74. }
  75.  
  76. // A function used by library function qsort() to sort an array of
  77. // points with respect to the first point
  78. int compare(const void *vp1, const void *vp2)
  79. {
  80. Point *p1 = (Point *)vp1;
  81. Point *p2 = (Point *)vp2;
  82.  
  83. // Find orientation
  84. int o = orientation(p0, *p1, *p2);
  85. if (o == 0)
  86. return (distSq(p0, *p2) >= distSq(p0, *p1))? -1 : 1;
  87.  
  88. return (o == 2)? -1: 1;
  89. }
  90.  
  91. // Prints convex hull of a set of n points.
  92. void convexHull(Point points[], int n, Point ac[], int &nr)
  93. {
  94. // Find the bottommost point
  95. int ymin = points[0].y, min = 0;
  96. for (int i = 1; i < n; i++)
  97. {
  98. int y = points[i].y;
  99.  
  100. // Pick the bottom-most or chose the left
  101. // most point in case of tie
  102. if ((y < ymin) || (ymin == y &&
  103. points[i].x < points[min].x))
  104. ymin = points[i].y, min = i;
  105. }
  106.  
  107. // Place the bottom-most point at first position
  108. swap(points[0], points[min]);
  109.  
  110. // Sort n-1 points with respect to the first point.
  111. // A point p1 comes before p2 in sorted ouput if p2
  112. // has larger polar angle (in counterclockwise
  113. // direction) than p1
  114. p0 = points[0];
  115. qsort(&points[1], n-1, sizeof(Point), compare);
  116.  
  117. // If two or more points make same angle with p0,
  118. // Remove all but the one that is farthest from p0
  119. // Remember that, in above sorting, our criteria was
  120. // to keep the farthest point at the end when more than
  121. // one points have same angle.
  122. int m = 1; // Initialize size of modified array
  123. for (int i=1; i<n; i++)
  124. {
  125. // Keep removing i while angle of i and i+1 is same
  126. // with respect to p0
  127. while (i < n-1 && orientation(p0, points[i],
  128. points[i+1]) == 0)
  129. i++;
  130.  
  131.  
  132. points[m] = points[i];
  133. m++; // Update size of modified array
  134. }
  135.  
  136. // If modified array of points has less than 3 points,
  137. // convex hull is not possible
  138. if (m < 3) return;
  139.  
  140. // Create an empty stack and push first three points
  141. // to it.
  142. stack<Point> S;
  143. S.push(points[0]);
  144. S.push(points[1]);
  145. S.push(points[2]);
  146.  
  147. // Process remaining n-3 points
  148. for (int i = 3; i < m; i++)
  149. {
  150. // Keep removing top while the angle formed by
  151. // points next-to-top, top, and points[i] makes
  152. // a non-left turn
  153. while (orientation(nextToTop(S), S.top(), points[i]) != 2)
  154. S.pop();
  155. S.push(points[i]);
  156. }
  157.  
  158. // Now stack has the output points, print contents of stack
  159. while (!S.empty())
  160. {
  161. Point p = S.top();
  162. ac[nr]=p;
  163. nr++;
  164. cout << "(" << p.x << ", " << p.y <<")" << endl;
  165. S.pop();
  166. }
  167. }
  168.  
  169.  
  170.  
  171.  
  172.  
  173.  
  174.  
  175.  
  176. void sortare(int n,Point v[])
  177. {
  178. int k,i,j;
  179. do{
  180. k=0;
  181. for(i=0;i<n-1;i++)
  182. if(v[i].x>v[i+1].x||((v[i].x==v[i+1].x)&&(v[i].y>v[i+1].y)))
  183. {
  184. Point aux=v[i];
  185. v[i]=v[i+1];
  186. v[i+1]=aux;
  187. k=1;
  188. }
  189. }while(k==1);
  190. }
  191. int verif_lat(Point x1, Point x2, Point aux)
  192. {
  193. float a,b,c;
  194. a = sqrt(pow((x1.x-x2.x),2)+pow((x1.y-x2.y),2));
  195. b = sqrt(pow((x1.x-aux.x),2)+pow((x1.y-aux.y),2));
  196. c = sqrt(pow((aux.x-x2.x),2)+pow((aux.y-x2.y),2));
  197. if(b+c == a)
  198. return 1;
  199. return 0;
  200. }
  201.  
  202.  
  203.  
  204. // AVEM DE TRATAT 3 CAZURI!
  205. /*
  206. 1.Daca e in afara, acoperirea convexa se schimba
  207. 2.1 Daca e pe vreo latura
  208. 2.2 Daca e in interior
  209. */
  210. int main()
  211. {
  212. int n;
  213. cout<<"Numar varfuri poligon=";
  214. cin>>n;
  215. Point points[n],points1[n+1];
  216. for(int i=0; i<n; i++)
  217. {
  218. cout << "Varful " << i + 1 << endl;
  219. cout << "x=";
  220. cin >> points[i].x;
  221. cout << "y=";
  222. cin >> points[i].y;
  223. points1[i].x = points[i].x;
  224. points1[i].y = points[i].y;
  225. }
  226.  
  227. cout << endl;
  228. cout << "Punct A\n";
  229. Point A;
  230. cout << "x=";
  231. cin >> A.x;
  232. cout << "y=";
  233. cin >> A.y;
  234. points1[n].x = A.x;
  235. points1[n].y = A.y;
  236. Point acoperire1[n];
  237. Point acoperire2[n+1];
  238. int nr_ac1 = 0, nr_ac2 = 0;
  239. convexHull(points1, n,acoperire1, nr_ac1);
  240. convexHull(points1, n+1,acoperire2, nr_ac2);
  241.  
  242. Point aux[nr_ac2];
  243.  
  244. for(int i=0;i<nr_ac1;i++)
  245. cout<<acoperire1[i].x<<' '<<acoperire1[i].y<<endl;
  246. cout<<endl;
  247.  
  248. for(int i=0;i<nr_ac2;i++)
  249. cout<<acoperire2[i].x<<' '<<acoperire2[i].y<<endl;
  250. cout<<endl;
  251.  
  252. for(int i=0; i<nr_ac2; i++)
  253. {
  254. aux[i].x = acoperire2[i].x;
  255. aux[i].y = acoperire2[i].y;
  256. }
  257. //sortam cele 2 acoperiri convexe crescator dupa x(y in caz de egalitate)
  258. sortare(nr_ac1,acoperire1);
  259. sortare(nr_ac2,acoperire2);
  260. // cout << pozitie_punct(acoperire1,acoperire2,nr_ac1,nr_ac2) << "\n";
  261. int ok = 0; Point aux1, aux2;
  262. if(pozitie_punct(acoperire1,acoperire2,nr_ac1,nr_ac2))
  263. //verific cazul in care este pe o latura
  264. {
  265. for(int i=0; i<n-1; i++)
  266. if(verif_lat(aux[i], aux[i+1], A) == 1)
  267. {
  268. ok = 1;
  269. aux1.x = aux[i].x;
  270. aux1.y = aux[i].y;
  271. aux2.x = aux[i+1].x;
  272. aux2.y = aux[i+1].y;
  273. }
  274. if(verif_lat(aux[0],aux[n-1],A)==1)
  275. {
  276. ok = 1;
  277. aux1.x = aux[0].x;
  278. aux1.y = aux[0].y;
  279. aux2.x = aux[n-1].x;
  280. aux2.y = aux[n-1].y;
  281. }
  282.  
  283. if(ok == 1)
  284. cout<<"Varful se afla pe latura: "<<"x: "<<aux1.x<<" y: "<<aux1.y<<"\n"<<"x: "<<aux2.x<<" y: "<<aux2.y<<"\n";
  285. else
  286. cout<<"Varful este in interior";
  287. }
  288.  
  289. return 0;
  290. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement