zoukankan      html  css  js  c++  java
  • [Unity算法]点是否在多边形范围内

    参考链接:

    https://www.zhihu.com/question/26551754

    http://www.cnblogs.com/leoin2012/p/6425089.html

    原理如下:

    代码实现:

     1 using UnityEngine;
     2 using System.Collections.Generic;
     3 
     4 public class MathTool {
     5 
     6     /// <summary>
     7     /// 点是否在多边形范围内
     8     /// </summary>
     9     /// <param name="p"></param>
    10     /// <param name="vertexs">多边形顶点列表</param>
    11     /// <returns></returns>
    12     public static bool IsPointInPolygon(Vector2 p, List<Vector2> vertexs)
    13     {
    14         int crossNum = 0;
    15         int vertexCount = vertexs.Count;
    16 
    17         for (int i = 0; i < vertexCount; i++)
    18         {
    19             Vector2 v1 = vertexs[i];
    20             Vector2 v2 = vertexs[(i + 1) % vertexCount];
    21 
    22             if (((v1.y <= p.y) && (v2.y > p.y))
    23                 || ((v1.y > p.y) && (v2.y <= p.y)))
    24             {
    25                 if (p.x < v1.x + (p.y - v1.y) / (v2.y - v1.y) * (v2.x - v1.x))
    26                 {
    27                     crossNum += 1;
    28                 }
    29             }
    30         }
    31 
    32         if (crossNum % 2 == 0)
    33         {
    34             return false;
    35         }
    36         else
    37         {
    38             return true;
    39         }
    40     }
    41 }
  • 相关阅读:
    tcp/心跳包
    TCP协议中的三次握手和四次挥手(图解)
    http 中get和post
    xmpp总结
    IOS中http请求使用cookie
    sdwebimage总结
    iOS断言
    Object-C自定义对象NSLog输入信息
    NSTimer你真的会用了吗
    ios中block中的探究
  • 原文地址:https://www.cnblogs.com/lyh916/p/10633132.html
Copyright © 2011-2022 走看看