使用lua判断点在多边形内部

 
 1  -- 获取多边形外围矩形
 2  function GetPolyOutRect(p, poly)
 3      local l_max_x  =  poly[ 1 ].X;
 4      local l_max_y  =  poly[ 1 ].Y;
 5      local l_min_x  =  l_max_x;
 6      local l_min_y  =  l_min_y;
 7       for  i  =   2 , #poly  do
 8           if  l_max_x  <  poly[i].x then
 9              l_max_x  =  poly[i].x;
10          end
11           if  l_max_y  <  poly[i].y then
12              l_max_y  =  poly[i].y;
13          end
14           if  l_min_x  >  poly[i].y then
15              l_min_x  =  poly[i].y;
16          end
17           if  l_min_y  >  poly[i].y then
18              l_min_y  =  poly[i].y;
19          end
20      end
21       return  {l_min_x,l_max_x,l_min_y,l_max_y};
22  end
23 
24  -- 点是否在多边形外围矩形中
25  function IsPtInPolyRect(p, poly)
26      local rect  =  GetPolyOutRect(p, poly);
27       if  p.x  <  rect[ 1 ] or p.x  >  rect[ 2 ] or p.y  <  rect[ 3 ] or p.y  >  rect[ 4 ] then
28           return   false ;
29       else
30           return   true ;
31      end
32  end
33 
34  -- 判断点在多边形内true or  false ,点正好在多边形边上时返回值不定
35  function IsPtInPoly(p, poly)
36       if   false   ==  IsPtInPolyRect(p, poly) then
37           return   false ;
38      end
39      local l_odd  =   false ;
40      local j  =  #poly;
41       for  i  =   1 , #poly  do
42           if  ((poly[i].y  <  p.y and poly[j].y  >=  p.y) or poly[j].y  <  p.y and poly[i].y  >=  p.y)) and (poly[i].x  <=  p.x and poly[j].x  <=  p.x) then
43               if  poly[i].x  +  (p.y  -  poly[i].y) * (poly[j].x  -  poly[i].x)  /  (poly[j].y  -  poly[i].y)  <  p.x then
44                  l_odd  =  not l_odd;
45              end
46          end
47      end
48       return  l_odd;
49  end
50      
51  local p  =  {x  =   2 , y  =   2.5 };
52  local poly  =  {{x  =   1 , y  =   2 },{x  =   2 , y  =   3 },{x  =   3 , y  =   2 }}
53  local result  =  IsPtInPoly(p, poly);
54  print(result);
55  -- 由于无法从代码中直接复制,纯手敲代码,若有疏漏,请留言。
56 --代码参考了网上众多前辈的教程,不一一列出,感谢前辈
57 
58 
59 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

nwao7890

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值