Results 1 to 3 of 3
  1. #1
    soorena is offline Member
    Join Date
    Apr 2009
    Posts
    1
    Rep Power
    0

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

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    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 09:07 AM. Reason: I meant Area, not Shape

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    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[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.
    Last edited by pbrockway2; 04-01-2009 at 09:40 AM.

Similar Threads

  1. java floating point comparison
    By sardare in forum Advanced Java
    Replies: 6
    Last Post: 03-03-2009, 05:11 PM
  2. number of floating point
    By mohammad8065 in forum Advanced Java
    Replies: 5
    Last Post: 12-28-2008, 10:41 AM
  3. Floating point values in SWT Spinner
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-07-2008, 05:50 PM

Posting Permissions

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