Advertisement
flash_7

Whirlwind Algo

Mar 2nd, 2017
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.07 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define Size 355
  5. const double pi = 2 * acos ( 0.0 );
  6. const double eps = 1e-8;
  7.  
  8. struct Point {
  9.     double x, y, r;
  10. };
  11.  
  12. vector<Point> List;
  13. double DP[Size];
  14. int vis[Size];
  15. vector<int> Graph[Size];
  16.  
  17. /// Compute A dot B:
  18. double dot(Point A, Point B){
  19.     return (A.x * B.x + A.y * B.y);
  20. }
  21.  
  22. ///Compute AB dot AC
  23. double dotProduct(Point A, Point B, Point C) {
  24.     Point AB = {B.x - A.x, B.y - A.y}; /// AB vector
  25.     Point AC = {C.x - A.x, C.y - A.y}; /// AC vector
  26.     return dot(AB, AC);
  27. }
  28.  
  29. double dist(Point A, Point B) {
  30.     return sqrt((A.x - B.x) * (A.x - B.x) + (A.y - B.y) * (A.y - B.y));
  31. }
  32.  
  33. /// Angel between vector (A-B) and (A-C)
  34. double getAngel(Point A, Point B, Point C){
  35.     double dotVal = dotProduct(A, B, C);
  36.     double a = dotVal/(dist(A,B) * dist(A,C));
  37.     if(a < -1.0) a = -1.0;
  38.     if(a > 1.0) a = 1.0;
  39.     return acos(a);
  40. }
  41.  
  42. bool DFS(int cur, Point P){
  43.     vis[cur] = 1;
  44.     for(int i = 0;i<Graph[cur].size();i++){
  45.         int v = Graph[cur][i];
  46.         double a = DP[cur] + getAngel(P, List[v], List[cur]);
  47.         if(vis[v] == -1){
  48.             DP[v] = a;
  49.             bool ret = DFS(v, P);
  50.             if(ret == true) return true;
  51.         }else{
  52.             double dif = a - DP[v];
  53.             if(dif+eps > 2.0*pi || dif < -2.0*pi+eps) return true;
  54.         }
  55.     }
  56.     return false;
  57. }
  58.  
  59. /// Add edge between adjacent vertices.
  60. void addEdge(int u, int v){
  61.     Graph[u].push_back(v);
  62.     Graph[v].push_back(u);
  63. }
  64.  
  65. bool solve(Point P){
  66.     for(int i = 0;i<List.size();i++){
  67.         addEdge(i,(i+1)%List.size());
  68.     }
  69.     CLR(vis,-1);
  70.     for(int i = 0;i<List.size();i++){
  71.         if(vis[i] == -1){
  72.             DP[i] = 0.0;
  73.             if(DFS(i, P)) return true;
  74.         }
  75.     }
  76.     return false;
  77. }
  78.  
  79. int main(){
  80.     int N;
  81.     Point A;
  82.     scanf("%d",&N);
  83.     for(int i = 0;i<N;i++){
  84.         scanf("%lf %lf",&A.x, &A.y);
  85.         List.push_back(A);
  86.     }
  87.     scanf("%lf %lf",&A.x, &A.y);
  88.     if(solve(A)) printf("Inside Polygon\n");
  89.     else printf("Outside Polygon\n");
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement