zoukankan      html  css  js  c++  java
  • PNPOLY

    https://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html

    The C Code

    Here is the code, for reference. Excluding lines with only braces, there are only 7 lines of code.

    int pnpoly(int nvert, float *vertx, float *verty, float testx, float testy)
    {
      int i, j, c = 0;
      for (i = 0, j = nvert-1; i < nvert; j = i++) {
        if ( ((verty[i]>testy) != (verty[j]>testy)) &&
    	 (testx < (vertx[j]-vertx[i]) * (testy-verty[i]) / (verty[j]-verty[i]) + vertx[i]) )
           c = !c;
      }
      return c;
    }
    
    ArgumentMeaning
    nvert Number of vertices in the polygon. Whether to repeat the first vertex at the end is discussed below.
    vertx, verty Arrays containing the x- and y-coordinates of the polygon's vertices.
    testx, testy X- and y-coordinate of the test point.

    The Method

    I run a semi-infinite ray horizontally (increasing x, fixed y) out from the test point, and count how many edges it crosses. At each crossing, the ray switches between inside and outside. This is called the Jordan curve theorem.

    The case of the ray going thru a vertex is handled correctly via a careful selection of inequalities. Don't mess with this code unless you're familiar with the idea ofSimulation of Simplicity. This pretends to shift the ray infinitesimally down so that it either clearly intersects, or clearly doesn't touch. Since this is merely a conceptual, infinitesimal, shift, it never creates an intersection that didn't exist before, and never destroys an intersection that clearly existed before.

    The ray is tested against each edge thus:

    1. Is the point in the half-plane to the left of the extended edge? and
    2. Is the point's Y coordinate within the edge's Y-range?

    Handling endpoints here is tricky.

  • 相关阅读:
    码农提高工作效率-黄博文
    myeclipse与tomcat,运行jsp程序
    Ultraedit和写字板修改Tomcat 6.0的server.xml不生效
    MySQL5.5.33对应的JDBC驱动包怎样使用?
    Java是用JDBC连接MySQL数据库
    myeclipse trial expired暂时解决办法
    Json数据使用及学习方法
    在C#中使用json字符串
    vs2012换肤功能,vs2012主题及自定义主题
    给Notepad++换主题
  • 原文地址:https://www.cnblogs.com/qq378829867/p/5920600.html
Copyright © 2011-2022 走看看