如何确定点是否在2D凸多边形内? [英] How to determine if a point is inside a 2D convex polygon?

查看:172
本文介绍了如何确定点是否在2D凸多边形内?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个凸多边形(通常只是一个旋转的方形),我知道所有4个点。如何确定给定点(黄色/绿色)是否在内多边形?

解决方案

http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html 显示如何做这对于任何多边形。



我有这个Java的实现,但是它太大而不能完整地发布在这里。然而,你应该能够解决它:

  class Boundary {
private final Point [] points; //构成边界的点
...


/ **
*如果给定点包含在边界内,则返回true。
*参见:http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html
* @param test检查
* @return true如果点在边界内,则否则返回false
*
* /
public boolean contains(Point test){
int i;
int j;
布尔结果= false; ((points [i] .y> test.y)!
for(i = 0,j = points.length-1; i (test.x<(points [j] .x - points [i] .x)*(test.y - points [i] .y)/(points [j] .y-points [i] .y)+ points [i] .x)){
result =!result;
}
}
返回结果;


$ / code $ / pre

这里是Point类的草图 p>

  / ** 
*二维笛卡尔点。
* /
public class Point {
public final double x;
public final double y;
...
}


I have a convex polygon (typically just a rotated square), and I know all of 4 points. How do I determine if a given point (yellow/green) is inside the polygon?

EDIT: For this particular project, I don't have access to all of the libraries of the JDK, such as AWT.

解决方案

This page: http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html shows how to do this for any polygon.

I have a Java implementation of this, but it is too big to post here in its entirety. However, you should be able to work it out:

class Boundary {
    private final Point[] points; // Points making up the boundary
    ...


    /**
     * Return true if the given point is contained inside the boundary.
     * See: http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html
     * @param test The point to check
     * @return true if the point is inside the boundary, false otherwise
     *
     */
    public boolean contains(Point test) {
      int i;
      int j;
      boolean result = false;
      for (i = 0, j = points.length - 1; i < points.length; j = i++) {
        if ((points[i].y > test.y) != (points[j].y > test.y) &&
            (test.x < (points[j].x - points[i].x) * (test.y - points[i].y) / (points[j].y-points[i].y) + points[i].x)) {
          result = !result;
         }
      }
      return result;
    }
}

And here is a sketch of the Point class

/**
 * Two dimensional cartesian point.
 */
public class Point {
  public final double x;
  public final double y;
  ...
}

这篇关于如何确定点是否在2D凸多边形内?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆