# Thread: A Polygon class which accepts floating point coordinates

1. Member Join Date
Apr 2009
Posts
1
Rep Power
0

## 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!  Reply With Quote

2. Moderator   Join Date
Feb 2009
Location
New Zealand
Posts
4,717
Rep Power
17

## 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.
Last edited by pbrockway2; 04-01-2009 at 08:07 AM. Reason: I meant Area, not Shape  Reply With Quote

3. Moderator   Join Date
Feb 2009
Location
New Zealand
Posts
4,717
Rep Power
17

## Very roughly (to check that I wasn't talking complete nonsense...

Java 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;
if(it.currentSegment(coords) == PathIterator.SEG_CLOSE) {
break;
}
if(first) {
first = false;
} else {
System.out.print(" --> ");
}
System.out.printf("%.3f,%.3f", coords, coords);
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.
Last edited by pbrockway2; 04-01-2009 at 08:40 AM.  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•