计算一个对象是否在一组坐标内? [英] Calculate if an object is inside a set of coordinates?

查看:201
本文介绍了计算一个对象是否在一组坐标内?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组X和Y点来构建一个形状,我需要知道一个物体是否在其中,或者不计算它是什么?

I have a set of X and Y points that builds a shape and I need to know if an object is inside it or not what is the calculation to it ?

X和Y坐标示例:

522.56055 2389.885
544.96 2386.3406
554.18616 2369.2385
535.21814 2351.396
497.5552 2355.8396

我对数学并不是很好:(所以我会很感激一些支持了解它是如何完成的。

I am not really good with math :( so i would appreciate some support to understand how it is done.

到目前为止我的例子但似乎不太可靠:

Example of what I have so far but doesnt seem very reliable:

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;
}


推荐答案

您可以从这开始: http://en.wikipedia.org/wiki/Point_in_polygon

您还可以查看 JTS拓扑套件
特别是使用此功能

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

EDIT: Here is example using 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));

  }

}

以下是示例使用AWT(更简单并且是Java SE的一部分):

Here is example using AWT (which is simpler and is part of Java SE):

import java.awt.Polygon;

public class JavaTest {

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

    final Polygon polygon = new Polygon();
    polygon.addPoint(-10, -10);
    polygon.addPoint(-10, 10);
    polygon.addPoint(10, 10);
    polygon.addPoint(10, -10);

    System.out.println(polygon.contains(0, 0));

  }

}

这篇关于计算一个对象是否在一组坐标内?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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