Java 计算对象是否在一组坐标内?

Java 计算对象是否在一组坐标内?,java,coordinates,jts,Java,Coordinates,Jts,我有一组X点和Y点来构建一个形状,我需要知道一个物体是否在里面,对它的计算是什么 X和Y坐标示例: 522.56055 2389.885 544.96 2386.3406 554.18616 2369.2385 535.21814 2351.396 497.5552 2355.8396 我的数学不是很好,所以我希望得到一些支持来理解它是如何完成的 到目前为止,我所拥有但似乎不太可靠的示例: private boolean isInsideShape(Zone verifyZone, Posit

我有一组X点和Y点来构建一个形状,我需要知道一个物体是否在里面,对它的计算是什么

X和Y坐标示例:

522.56055 2389.885
544.96 2386.3406
554.18616 2369.2385
535.21814 2351.396
497.5552 2355.8396
我的数学不是很好,所以我希望得到一些支持来理解它是如何完成的

到目前为止,我所拥有但似乎不太可靠的示例:

private boolean isInsideShape(Zone verifyZone, Position object)
{
    int corners = verifyZone.getCorners();
    float[] xCoords = verifyZone.getxCoordinates();
    float[] yCoords = verifyZone.getyCoordinates();

    float x = object.getX();
    float y = object.getY();
    float z = object.getZ();

    int i, j = corners - 1;
    boolean inside = false;

    for(i = 0; i < corners; i++)
    {
        if(yCoords[i] < y && yCoords[j] >= y || yCoords[j] < y && yCoords[i] >= y)
            if(xCoords[i] + (y - yCoords[i]) / (yCoords[j] - yCoords[i]) * (xCoords[j] - xCoords[i]) < x)
                inside = !inside;
        j = i;
    }

    return inside;
}
私有布尔值isInsideShape(区域验证区域、位置对象)
{
int corners=verifyZone.getCorners();
float[]xCoords=verifyZone.getxCoordinates();
float[]yCoords=verifyZone.getyCoordinates();
float x=object.getX();
float y=object.getY();
float z=object.getZ();
int i,j=角点-1;
布尔内部=假;
对于(i=0;i=y | | yCoords[j]=y)
如果(xCoords[i]+(y-yCoords[i])/(yCoords[j]-yCoords[i])*(xCoords[j]-xCoords[i])
您可以从以下内容开始:

你也可以调查一下。 特别是使用

编辑:以下是使用JTS的示例:

import java.util.ArrayList;

import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.LinearRing;
import com.vividsolutions.jts.geom.Point;
import com.vividsolutions.jts.geom.Polygon;
import com.vividsolutions.jts.geom.impl.CoordinateArraySequence;

public class GeoTest {

  public static void main(final String[] args) {

    final GeometryFactory gf = new GeometryFactory();

    final ArrayList<Coordinate> points = new ArrayList<Coordinate>();
    points.add(new Coordinate(-10, -10));
    points.add(new Coordinate(-10, 10));
    points.add(new Coordinate(10, 10));
    points.add(new Coordinate(10, -10));
    points.add(new Coordinate(-10, -10));
    final Polygon polygon = gf.createPolygon(new LinearRing(new CoordinateArraySequence(points
        .toArray(new Coordinate[points.size()])), gf), null);

    final Coordinate coord = new Coordinate(0, 0);
    final Point point = gf.createPoint(coord);

    System.out.println(point.within(polygon));

  }

}

我一直都是这样做的:

Pick a point you know to be outside the shape.
Make a line between that point and the point you're trying to find whether it's inside the shape or not.
Count the number of sides of the shape the line crosses. 

If the count is odd, the point is inside the shape.
If the count is even, the point is outside the shape.

我用一个例子更新了这个问题,说明我对数学/地理的理解完全不好,通过阅读,我更容易理解实用代码;(感谢您花时间与我所做的相比,这样做非常简单,非常感谢您的帮助。
这样做
,从您展示的示例中,只是为了澄清,hehehei在java SE和android中都使用了这种方法。在SE中工作得非常好,但android中的运行时错误找不到类'com.livesolutions.jts.geom.GeometryFactory',引用自方法ceylon.linux.geofencepolygon.MainActivity.runrunI不认为android支持awt类这只在多边形简单且不与自身相交的情况下有效,因此所有或几乎所有src代码都只处理简单多边形。没有那么简单,请考虑纬度和经度为负值的情况de和特殊情况下,箱盖赤道经度为0。请尝试使用现有库。
Pick a point you know to be outside the shape.
Make a line between that point and the point you're trying to find whether it's inside the shape or not.
Count the number of sides of the shape the line crosses. 

If the count is odd, the point is inside the shape.
If the count is even, the point is outside the shape.