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

发布于 2024-10-19 21:51:55 字数 985 浏览 2 评论 0原文

我有一组 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;
}

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 and Y coords example:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅紫色的梦幻 2024-10-26 21:51:55

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

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

编辑:这是使用 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 的一部分):

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

  }

}

You may start from this: http://en.wikipedia.org/wiki/Point_in_polygon

You also might look into JTS Topology Suite.
And in particular use this function.

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

  }

}

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

  }

}
给妤﹃绝世温柔 2024-10-26 21:51:55

我一直都是这样做的:

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.

I've always done it like so:

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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文