Guest User

Untitled

a guest
May 21st, 2019
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.08 KB | None | 0 0
  1. struct point {
  2. long double x,y;
  3.  
  4. };
  5.  
  6. struct Vector {
  7. long double x,y;
  8. Vector() = default;
  9. Vector (int x,int y): x(x),y(y) {}
  10. Vector operator * (int b) { return Vector(x*b,y*b); }
  11. Vector operator + (Vector v2) {return Vector(x+v2.x,y+v2.y); }
  12. };
  13.  
  14. struct line {
  15. long double a,b,c;
  16. line (point p1, point p2) {
  17. a = p1.y - p2.y;
  18. b = p2.x - p1.x;
  19. c = -a*p1.x - p1.y*b;
  20. }
  21. };
  22.  
  23. struct segment {
  24. point a,b;
  25. segment (point q, point w) {
  26. a=q;
  27. b=w;
  28. }
  29. };
  30.  
  31. void printPoint (point p) {
  32. cout<<p.x<<" "<<p.y<<"\n";
  33. }
  34.  
  35. void printVector (Vector p) {
  36. cout<<p.x<<" "<<p.y<<"\n";
  37. }
  38.  
  39. long double vectorLength(Vector a) {
  40. return sqrt(a.x*a.x+a.y*a.y);
  41. }
  42.  
  43. long double pointDist (point a, point b) {
  44. return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
  45. }
  46.  
  47. long double dotproduct (Vector a, Vector b) {
  48. return a.x*b.x + a.y*b.y;
  49. }
  50. long double crossproduct (Vector a, Vector b) {
  51. return a.x*b.y - a.y*b.x;
  52. }
  53. bool cmp (point a, point b) {
  54. return a.x < b.x || a.x == b.x && a.y < b.y;
  55. }
  56.  
  57. bool cw (point a, point b, point c) {
  58. return a.x * (b.y - c.y) + b.x * (c.y - a.y) + c.x * (a.y - b.y) < 0;
  59. }
  60.  
  61. bool ccw (point a, point b, point c) {
  62. return a.x * (b.y - c.y) + b.x * (c.y - a.y) + c.x * (a.y - b.y) > 0;
  63. }
  64.  
  65. void convex_hull (vector<point> & a) { //PLEASE LOOK HERE
  66. if (a.size() == 1) return;
  67. sort (a.begin(), a.end(), &cmp);
  68. point p1 = a[0];
  69. point p2 = a.back();
  70. vector <point> up, down;
  71. up.push_back (p1);
  72. down.push_back (p1);
  73. for (int i=1;i<a.size();i++) {
  74. if (i == a.size()-1 || cw (p1, a[i], p2)) {
  75. while (up.size() >= 2 && !cw (up[up.size()-2], up[up.size()-1], a[i]))
  76. up.pop_back();
  77. up.push_back (a[i]);
  78. }
  79. if (i == a.size()-1 || ccw (p1, a[i], p2)) {
  80. while (down.size() >= 2 && !ccw (down[down.size()-2], down[down.size()-1], a[i]))
  81. down.pop_back();
  82. down.push_back (a[i]);
  83. }
  84. }
  85. a.clear();
  86. for (int i=0;i<up.size();++i)
  87. a.push_back (up[i]);
  88. for (int i=down.size()-2;i>0;i--)
  89. a.push_back (down[i]);
  90. }
Advertisement
Add Comment
Please, Sign In to add comment