SHARE
TWEET

Untitled

andrefmandrade Jun 27th, 2020 1,430 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // # MAPA EXEMPLO #
  2. // https://i.imgur.com/bP5jyjf.png
  3.  
  4.  
  5. //zona mais para dentro
  6. const zona1 = [
  7.   [ -49.2829952, -16.6897996 ],
  8.   [ -49.280034, -16.693417 ],
  9.   [ -49.276987, -16.697281 ],
  10.   [ -49.2728242, -16.702378 ],
  11.   [ -49.2684898, -16.6991718 ],
  12.   [ -49.2647562, -16.6964177 ],
  13.   [ -49.2620525, -16.6909506 ],
  14.   [ -49.2639837, -16.6894296 ],
  15.   [ -49.264842, -16.6869631 ],
  16.   [ -49.2646703, -16.6859765 ],
  17.   [ -49.2639837, -16.6858121 ],
  18.   [ -49.2646703, -16.68425 ],
  19.   [ -49.2657003, -16.6842911 ],
  20.   [ -49.2676744, -16.6777136 ],
  21.   [ -49.2731247, -16.6785769 ],
  22.   [ -49.2798194, -16.6753703 ],
  23.   [ -49.2806348, -16.6758636 ],
  24.   [ -49.2787466, -16.6781658 ],
  25.   [ -49.2782745, -16.6794402 ],
  26.   [ -49.2777166, -16.681249 ],
  27.   [ -49.2773733, -16.6837156 ],
  28.   [ -49.2777595, -16.6857299 ],
  29.   [ -49.2796049, -16.6871276 ],
  30.   [ -49.2823085, -16.6892652 ],
  31.   [ -49.2829952, -16.6897996 ]
  32. ]
  33.  
  34. //zona mais para fora
  35. const zona2 = [
  36.   [ -49.291813, -16.6948852 ],
  37.   [ -49.2734452, -16.708532 ],
  38.   [ -49.2566224, -16.7100118 ],
  39.   [ -49.2519017, -16.6922544 ],
  40.   [ -49.2528458, -16.6813196 ],
  41.   [ -49.2663212, -16.6724398 ],
  42.   [ -49.2802258, -16.6706309 ],
  43.   [ -49.2906972, -16.6714531 ],
  44.   [ -49.2883797, -16.6874859 ],
  45.   [ -49.2885514, -16.6951319 ],
  46.   [ -49.291813, -16.6948852 ]
  47. ];
  48.  
  49.  
  50.  
  51. //coordenada do lifebox para teste
  52. const coordenada = [-49.2721061,-16.691911];
  53. const coordenadaMinhaCasa = [-49.3048243, -16.7300259];
  54.  
  55. function IsPointInPolygon(poly_array, test_point) {
  56.   var inside = false;
  57.   var test_x = test_point[0];
  58.   var test_y = test_point[1];
  59.   for(var i=0; i<(poly_array.length-1); i++) {
  60.       var p1_x = poly_array[i][0];
  61.       var p1_y = poly_array[i][1];
  62.       var p2_x = poly_array[i+1][0];
  63.       var p2_y = poly_array[i+1][1];
  64.       if((p1_y<test_y && p2_y>=test_y) || (p2_y<test_y && p1_y>=test_y)) { // this edge is crossing the horizontal ray of testpoint
  65.           if((p1_x+(test_y-p1_y)/(p2_y-p1_y)*(p2_x-p1_x)) < test_x) { // checking special cases (holes, self-crossings, self-overlapping, horizontal edges, etc.)
  66.               inside=!inside;
  67.           }
  68.       }
  69.   }
  70.   return inside;
  71. }
  72.  
  73. //como a zona1 tambem é dentro da zona2 (verificar de dentro para fora?);
  74. console.log(IsPointInPolygon(zona1, coordenada));
  75. console.log(IsPointInPolygon(zona2, coordenada));
  76.  
  77. console.log(IsPointInPolygon(zona1, coordenadaMinhaCasa));
  78. console.log(IsPointInPolygon(zona2, coordenadaMinhaCasa));
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top