java 双坐标多边形

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15620590/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 20:17:17  来源:igfitidea点击:

Polygons with Double Coordinates

javadoublepolygon

提问by Shudy

I have some questions about Polygons with points of Double type... What I have to do, is given points, create the polygon, and then, test if 1 concrete point is inside the polygon or not.

我有一些关于带有 Double 类型点的多边形的问题......我必须做的是,给定点,创建多边形,然后测试 1 个具体点是否在多边形内。

so I kwnow that in Java there's a class, called Polygon, and is used like that: (triangle)

所以我知道在 Java 中有一个叫做 Polygon 的类,它是这样使用的:(三角形)

int valoresX[] = { 100, 150, 200 };
int valoresY[] = { 100, 200, 100 };
int n = valoresX.length;
Polygon city= new Polygon(valoresX,valoresY,n);

But my "polygons" has to be of "Double" type, not "int" (easy example)

但是我的“多边形”必须是“Double”类型,而不是“int”(简单的例子)

Double valoresX[] = { 1000.10, 150.10, 200.10 };
Double valoresY[] = { 100.10, 200.10, 100.10 };

In my project i dont really need to paint it on an applet or similar, I just need to calculate if the point is inside or not.

在我的项目中,我真的不需要把它画在小程序或类似的东西上,我只需要计算点是否在里面。

So my question is:

所以我的问题是:

Is any way to do polygons with double coordenates , that allow to calcultate if the point(double) is inside the polygon or not?

有没有办法用双坐标做多边形,允许计算点(双)是否在多边形内?

Thanks for all!!!

谢谢大家!!!

Shudy

舒迪

回答by Russell Zahniser

You can do this with Path2D.Double:

你可以用Path2D.Double做到这一点:

Path2D path = new Path2D.Double();

path.moveTo(valoresX[0], valoresY[0]);
for(int i = 1; i < valoresX.length; ++i) {
   path.lineTo(valoresX[i], valoresY[i]);
}
path.closePath();

See also this question:

另见这个问题:

Implementing Polygon2D in Java 2D

在 Java 2D 中实现 Polygon2D