Advertisement
intricate_potato

Cyrus-Beck Algorithm Implementation

Feb 15th, 2021
467
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.03 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define pii pair<double, double>
  6.  
  7. vector<pii> vertices,normal;
  8.  
  9. int n;
  10. double tE, tL;
  11.  
  12.  
  13. // Function to calculate dot product
  14. double dot_product(pii A, pii B)
  15. {
  16.     return (A.first*B.first + A.second*B.second);
  17. }
  18.  
  19. // Operator overloading for Calculation among points
  20.  
  21. pii operator - (const pii &A, const pii &B)
  22. {
  23.     return make_pair(A.first-B.first, A.second-B.second);
  24. }
  25.  
  26. pii operator + (const pii &A, const pii &B){
  27.   return make_pair(A.first+B.first, A.second+B.second);
  28. }
  29.  
  30. // Operator overloading for multiplying a value to a point
  31. pii operator * (const pii &A, const double t){
  32.   return make_pair(t*A.first, t*A.second);
  33. }
  34.  
  35. // Function to compute Normal
  36. void compute_normal()
  37. {
  38.     pii prev = vertices[0];
  39.     for(int i=1; i<n; i++)
  40.     {
  41.         normal[i-1].second = -1 * (prev.first-vertices[i].first);
  42.         normal[i-1].first = prev.second-vertices[i].second;
  43.         prev = vertices[i];
  44.     }
  45.     normal[n-1].second = -1* (prev.first-vertices[0].first);
  46.     normal[n-1].first = prev.second-vertices[0].second;
  47. }
  48.  
  49. // Function to check whether the line intersects or not
  50.  
  51. int check(pii A, pii B)
  52. {
  53.     pii p = B - vertices[0];
  54.     double d1 = dot_product(p, normal[0]);
  55.     pii q = A - vertices[0];
  56.     double d2 = dot_product(q, normal[0]);
  57.     if(d1<0&&d2<0)
  58.       return 1;
  59.     // If the line doesn't intersect
  60.     if(d1>=0&&d2>=0)
  61.       return 0;
  62.     return -1;
  63. }
  64.  
  65. // Function to Clip
  66. void Clip(pii A, pii B)
  67. {
  68.     tE = 0; tL = 1;
  69.     for(int i=0; i<n; ++i)
  70.     {
  71.         double d = dot_product((B-A), normal[i]);
  72.         if(d==0)
  73.             continue;
  74.         double t = dot_product(A-(vertices[i]), normal[i])/dot_product((A-B),normal[i]);
  75.         //cout<<dot_product(A-(vertices[i]), normal[i])<<" "<<dot_product((A-B), normal[i])<<endl;
  76.         //cout<<d<<"  =  "<<t<<endl<<endl;
  77.         if(d<0&&t>tE)
  78.             tE = t;
  79.         else if(d>0&&t<tL)
  80.             tL = t;
  81.     }
  82. }
  83.  
  84. int main()
  85. {
  86.     cout<<"Enter the number of vertices\n";
  87.     cin>>n;
  88.     vertices.resize(n);
  89.     normal.resize(n);
  90.     cout<<"Enter the coordinates of the vertices in order\n";
  91.     for(int i=0; i<n; ++i)
  92.     {
  93.         cin>>vertices[i].first>>vertices[i].second;
  94.     }
  95.  
  96.     sort(vertices.begin(), vertices.end());
  97.  
  98.     cout<<"Enter the co-ordinates of the line \n";
  99.     pii A, B;
  100.     cin>>A.first>>A.second>>B.first>>B.second;
  101.  
  102.     compute_normal();
  103.     int chk = check(A, B);
  104.     //cout<<chk<<endl;
  105.     if(chk==-1)
  106.     {
  107.         Clip(A, B);
  108.         //cout<<tE<<" "<<tL;
  109.         if(tE>tL)
  110.             chk = 0;
  111.         else
  112.         {
  113.             pii E = A + (B-A)*tE;
  114.             pii L = A + (B-A)*tL;
  115.             //cout<<tE<<", "<<tL;
  116.             cout<<"\n("<<E.first<<", "<<E.second<<"), ("<<L.first<<", "<<L.second<<")";
  117.         }
  118.     }
  119.     if(chk==1)
  120.     cout<<"\nNo Clipping Points Found";
  121.  
  122. //    for(int j=0; j<normal.size(); j++)
  123. //    {
  124. //        cout<<normal[j].first<<", "<<normal[j].second<<endl;
  125. //    }
  126.  
  127.     return 0;
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement