gpt4 book ai didi

java - 如何查看多边形中已经添加的点?

转载 作者:行者123 更新时间:2023-12-01 18:10:00 25 4
gpt4 key购买 nike

Polygon polygon=new Polygon();
polygon.addPoint(6,4);
polygon.addPoint(25,4);
polygon.addPoint(25,13);
polygon.addPoint(6,13);

我需要知道点(25,4)存在于多边形中,但是

System.out.println(polygon.contains(25,4));

返回 false,这个方法看起来只是检查多边形内部的点。

那么,如何检查多边形中已添加的点?

最佳答案

假设您无法控制多边形的创建方式,您可以使用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 中找到的 contains 方法的内部定义,您可能可以使用:

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

这两种方法似乎都适用于水平/垂直直线。我会让你测试有角度的线。

关于java - 如何查看多边形中已经添加的点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33749816/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com