2011-03-03 56 views
14

我有一組X和Y點建立一個形狀,我需要知道一個對象是否在它內部或它是什麼計算?計算對象是否在一組座標中?

X和Y COORDS例如:

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

回答

30

你可以從這個開始:http://en.wikipedia.org/wiki/Point_in_polygon

你也可以看看JTS Topology Suite。 並特別使用this function

編輯:這裏是例如使用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)); 

    } 

} 
+0

我已經更新了這個問題與我有我的例子是通過閱讀理解數學/地理學很不理想通過實踐代碼我更容易理解;( – Prix 2011-03-03 02:07:12

+0

@Prix,我爲你添加了代碼示例。 – 2011-03-03 07:55:57

+0

感謝您抽出寶貴的時間與我所做的相比,這種方式讓人難以置信,非常感謝您的幫助。 – Prix 2011-03-03 10:10:26

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. 
+2

這隻適用於多邊形是簡單的,並不與自身相交http://en.wikipedia.org/wiki/Point_in_polygon – portforwardpodcast 2014-01-03 09:44:32

+0

當然,因此所有或幾乎所有的src代碼只處理與簡單的多邊形。 – AlexWien 2016-06-22 19:32:14