Advertisement
nikunjsoni

1610

Jul 25th, 2021
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.12 KB | None | 0 0
  1. #define PI 3.14159265
  2. #define MARGIN 1e-9
  3.  
  4. class Solution {
  5. public:
  6.     double get_angle(int x_diff, int y_diff) {
  7.         return atan2(y_diff, x_diff) * 180 / PI;
  8.     }
  9.    
  10.     int visiblePoints(vector<vector<int>>& points, int angle, vector<int>& location) {
  11.         int i, j, n = points.size(), common = 0;
  12.         vector<double> vals;
  13.         for(i=0; i<n; i++) {
  14.             int x = points[i][0] - location[0];
  15.             int y = points[i][1] - location[1];
  16.             if(x == 0 && y == 0)
  17.                 common++;
  18.             else {
  19.                 double A = get_angle(x, y);
  20.                 if(A < 0) A += 360;
  21.                 vals.emplace_back(A);
  22.             }
  23.         }
  24.  
  25.         sort(vals.begin(), vals.end());
  26.         vector<double> a = vals;
  27.         a.insert(a.end(), vals.begin(), vals.end());
  28.         for(i=vals.size(); i<a.size(); i++)
  29.             a[i] += 360;
  30.         int ret = 0;
  31.         for(i=0, j=0; i<a.size(); i++) {
  32.             while(j < a.size() && (a[j]-a[i]<=angle + MARGIN))
  33.                   j++;
  34.             ret = max(ret, j - i);
  35.         }
  36.         return ret + common;
  37.     }
  38. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement