# A Polygon class which accepts floating point coordinates

• 04-01-2009, 08:40 AM
soorena
A Polygon class which accepts floating point coordinates
Hi everyone,

I'm writing a program to calculate the intersection of two polygons and then save the result in 3rd polygon. The points in given polygons are all in integer coordinates, however, the intersections might be floating points.

Currently I'm using Java standard Polygon class(java.awt.Polygon) which accepts integer points. To save the result in the third polygon, I need a Polygon class which accepts floating points. Can anybody let me know what should I do? I searched the net but I couldn't find such a Polygon class. Should I write that class myself?

Thanks!
• 04-01-2009, 08:57 AM
pbrockway2
I haven't done this, but...

It seems to me that you could create two Area instances from your Polygon instances. Then perform constructive geometry (in your case intersect) the areas. Then obtain the PathIterator of the resulting Area.

For each element of the path iterator you can obtain the double coordinates of the current segment.
• 04-01-2009, 09:37 AM
pbrockway2
Very roughly (to check that I wasn't talking complete nonsense...

Code:

```import java.awt.Polygon; import java.awt.geom.AffineTransform; import java.awt.geom.Area; import java.awt.geom.PathIterator; public class IntersectEg {     public static void main(String[] args) {             // a 3x3 square with one corner at the origin         Polygon firstP = new Polygon(new int[]{0, 3, 3, 0}, new int[]{0, 0, 3, 3}, 4);             // a parallelogram intersecting the upper right corner         Polygon secondP = new Polygon(new int[]{2, 5, 7, 4}, new int[]{2, 2, 3, 3}, 4);                         Area intersection = new Area(firstP);         intersection.intersect(new Area(secondP));         PathIterator it = intersection.getPathIterator(new AffineTransform());         boolean first = true;         while(true) {             double[] coords = new double[6];             if(it.currentSegment(coords) == PathIterator.SEG_CLOSE) {                 break;             }             if(first) {                 first = false;             } else {                 System.out.print(" --> ");             }             System.out.printf("%.3f,%.3f", coords[0], coords[1]);             it.next();         }     } }```
Bottom line: if it were me implementing a floating point polygon and I was interested in intersections I would think of basing it on Area rather than Polygon.