Results 1 to 3 of 3
- 04-01-2009, 07:40 AM #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!
- 04-01-2009, 07:57 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
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
- 04-01-2009, 08:37 AM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
Very roughly (to check that I wasn't talking complete nonsense...
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.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[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(); } } }Last edited by pbrockway2; 04-01-2009 at 08:40 AM.
Similar Threads
-
java floating point comparison
By sardare in forum Advanced JavaReplies: 6Last Post: 03-03-2009, 04:11 PM -
number of floating point
By mohammad8065 in forum Advanced JavaReplies: 5Last Post: 12-28-2008, 09:41 AM -
Floating point values in SWT Spinner
By Java Tip in forum SWTReplies: 0Last Post: 07-07-2008, 04:50 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks