24
votes

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.

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

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

1

1 Answers

26
votes

You can do this with 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