26

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

Shudy
  • 7,806
  • 19
  • 63
  • 98

1 Answers1

29

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

Community
  • 1
  • 1
Russell Zahniser
  • 16,188
  • 39
  • 30
  • 1
    First of all, thanks for all, and the fast answer! I'mg gonna try it, and see if it works to my project ;) Thanks! Shudy – Shudy Mar 25 '13 at 17:23