Polygon polygon=new Polygon();
polygon.addPoint(6,4);
polygon.addPoint(25,4);
polygon.addPoint(25,13);
polygon.addPoint(6,13);
我需要知道Point(25,4)
中存在的polygon
,但
System.out.println(polygon.contains(25,4));
返回false,这个方法似乎只是检查多边形内的Point。
那么,我如何检查已经在多边形中添加的点?
答案 0 :(得分:2)
我会使用Polygon.npoints,Polygon.xpoints和Polygon.ypoints来构造hasPoint(Polygon p,int x,int y)方法。
http://docs.oracle.com/javase/7/docs/api/java/awt/Polygon.html
需要对此进行测试,但我想它可能如下所示:
private boolean hasPoint(Polygon p, int x, int y) {
for (int i = 0; i < p.npoints; i++) {
if (p.xpoints[i] == x && p.ypoints[i] == y)
return true;
}
return false;
}
答案 1 :(得分:2)
假设您无法控制多边形的创建方式,您可以使用PathIterator
获取任意Polygon
的每个部分。 PathIterator
包含有关每个线段起始位置的信息:
import java.awt.*;
import java.awt.geom.*;
class PolygonUtil
{
public static boolean contains(Polygon polygon, int x, int y)
{
if (polygon.contains(x, y))
return true;
PathIterator pi = polygon.getPathIterator(null);
double[] location = new double[6];
while(! pi.isDone())
{
int type = pi.currentSegment(location);
int piX = (int)location[0];
int piY = (int)location[1];
if (x == piX && y == piY)
return true;
pi.next();
}
return false;
}
public static void main(String...args)
{
Polygon polygon=new Polygon();
polygon.addPoint(10, 10);
polygon.addPoint(30, 10);
polygon.addPoint(30, 30);
polygon.addPoint(10, 30);
// Test 4 corners
System.out.println("Corners:");
System.out.println( PolygonUtil2.contains(polygon, 10, 10) );
System.out.println( PolygonUtil2.contains(polygon, 30, 10) );
System.out.println( PolygonUtil2.contains(polygon, 30, 30) );
System.out.println( PolygonUtil2.contains(polygon, 10, 30) );
// Test inside/outside left edge
System.out.println("Left:");
System.out.println( PolygonUtil2.contains(polygon, 9, 20) );
System.out.println( PolygonUtil2.contains(polygon, 10, 20) );
System.out.println( PolygonUtil2.contains(polygon, 11, 20) );
// Test inside/outside left edge
System.out.println("Right:");
System.out.println( PolygonUtil2.contains(polygon, 29, 20) );
System.out.println( PolygonUtil2.contains(polygon, 30, 20) );
System.out.println( PolygonUtil2.contains(polygon, 31, 20) );
// Test above/below top edge
System.out.println("Top:");
System.out.println( PolygonUtil2.contains(polygon, 20, 9) );
System.out.println( PolygonUtil2.contains(polygon, 20, 10) );
System.out.println( PolygonUtil2.contains(polygon, 20, 11) );
// Test above/below bottom edge
System.out.println("Bottom:");
System.out.println( PolygonUtil2.contains(polygon, 20, 29) );
System.out.println( PolygonUtil2.contains(polygon, 20, 30) );
System.out.println( PolygonUtil2.contains(polygon, 20, 31) );
}
}
或者,如果我理解API中找到的definition of insideness
,您可以使用:
import java.awt.*;
import java.awt.geom.*;
class PolygonUtil2
{
public static boolean contains(Polygon polygon, int x, int y)
{
System.out.print(x + " : " + y + " - ");
if (polygon.contains(x, y))
return true;
if (polygon.contains(x - 1, y))
return true;
if (polygon.contains(x, y - 1))
return true;
if (polygon.contains(x - 1, y - 1))
return true;
return false;
}
public static void main(String...args)
{
Polygon polygon=new Polygon();
polygon.addPoint(10, 10);
polygon.addPoint(30, 10);
polygon.addPoint(30, 30);
polygon.addPoint(10, 30);
// Test 4 corners
System.out.println("Corners:");
System.out.println( PolygonUtil2.contains(polygon, 10, 10) );
System.out.println( PolygonUtil2.contains(polygon, 30, 10) );
System.out.println( PolygonUtil2.contains(polygon, 30, 30) );
System.out.println( PolygonUtil2.contains(polygon, 10, 30) );
// Test inside/outside left edge
System.out.println("Left:");
System.out.println( PolygonUtil2.contains(polygon, 9, 20) );
System.out.println( PolygonUtil2.contains(polygon, 10, 20) );
System.out.println( PolygonUtil2.contains(polygon, 11, 20) );
// Test inside/outside left edge
System.out.println("Right:");
System.out.println( PolygonUtil2.contains(polygon, 29, 20) );
System.out.println( PolygonUtil2.contains(polygon, 30, 20) );
System.out.println( PolygonUtil2.contains(polygon, 31, 20) );
// Test above/below top edge
System.out.println("Top:");
System.out.println( PolygonUtil2.contains(polygon, 20, 9) );
System.out.println( PolygonUtil2.contains(polygon, 20, 10) );
System.out.println( PolygonUtil2.contains(polygon, 20, 11) );
// Test above/below bottom edge
System.out.println("Bottom:");
System.out.println( PolygonUtil2.contains(polygon, 20, 29) );
System.out.println( PolygonUtil2.contains(polygon, 20, 30) );
System.out.println( PolygonUtil2.contains(polygon, 20, 31) );
}
}
这两种方法似乎适用于直线水平/垂直线。我会让你测试成角度的线条。