Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // # MAPA EXEMPLO #
- // https://i.imgur.com/bP5jyjf.png
- //zona mais para dentro
- const zona1 = [
- [ -49.2829952, -16.6897996 ],
- [ -49.280034, -16.693417 ],
- [ -49.276987, -16.697281 ],
- [ -49.2728242, -16.702378 ],
- [ -49.2684898, -16.6991718 ],
- [ -49.2647562, -16.6964177 ],
- [ -49.2620525, -16.6909506 ],
- [ -49.2639837, -16.6894296 ],
- [ -49.264842, -16.6869631 ],
- [ -49.2646703, -16.6859765 ],
- [ -49.2639837, -16.6858121 ],
- [ -49.2646703, -16.68425 ],
- [ -49.2657003, -16.6842911 ],
- [ -49.2676744, -16.6777136 ],
- [ -49.2731247, -16.6785769 ],
- [ -49.2798194, -16.6753703 ],
- [ -49.2806348, -16.6758636 ],
- [ -49.2787466, -16.6781658 ],
- [ -49.2782745, -16.6794402 ],
- [ -49.2777166, -16.681249 ],
- [ -49.2773733, -16.6837156 ],
- [ -49.2777595, -16.6857299 ],
- [ -49.2796049, -16.6871276 ],
- [ -49.2823085, -16.6892652 ],
- [ -49.2829952, -16.6897996 ]
- ]
- //zona mais para fora
- const zona2 = [
- [ -49.291813, -16.6948852 ],
- [ -49.2734452, -16.708532 ],
- [ -49.2566224, -16.7100118 ],
- [ -49.2519017, -16.6922544 ],
- [ -49.2528458, -16.6813196 ],
- [ -49.2663212, -16.6724398 ],
- [ -49.2802258, -16.6706309 ],
- [ -49.2906972, -16.6714531 ],
- [ -49.2883797, -16.6874859 ],
- [ -49.2885514, -16.6951319 ],
- [ -49.291813, -16.6948852 ]
- ];
- //coordenada do lifebox para teste
- const coordenada = [-49.2721061,-16.691911];
- const coordenadaMinhaCasa = [-49.3048243, -16.7300259];
- function IsPointInPolygon(poly_array, test_point) {
- var inside = false;
- var test_x = test_point[0];
- var test_y = test_point[1];
- for(var i=0; i<(poly_array.length-1); i++) {
- var p1_x = poly_array[i][0];
- var p1_y = poly_array[i][1];
- var p2_x = poly_array[i+1][0];
- var p2_y = poly_array[i+1][1];
- 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
- 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.)
- inside=!inside;
- }
- }
- }
- return inside;
- }
- //como a zona1 tambem é dentro da zona2 (verificar de dentro para fora?);
- console.log(IsPointInPolygon(zona1, coordenada));
- console.log(IsPointInPolygon(zona2, coordenada));
- console.log(IsPointInPolygon(zona1, coordenadaMinhaCasa));
- console.log(IsPointInPolygon(zona2, coordenadaMinhaCasa));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement