Determine whether a polygon is complex or simple

Posted on 2006-03-29
Last Modified: 2008-01-09
I have two arrays, storing the x and y values of the polygon.

I want to find out whether the polygon is simple or complex.

The polygon can be concave and is 2d.

Can someone possibly provide suggestions.

Many Thanks.
Question by:stats786
    LVL 11

    Expert Comment

    A simple polygon has a non-intersecting boundary, while a complex polygon has an intersection somewhere. So a natural approach would be to check whether any two sides of the polygon intersect each other.

    The intersection of 2 lines can be tested with an algorithm like this:

    Author Comment

    I've tried doing an edge-edge test, but it fails for some reason. Could it be that an edge sharing the same node brings back true when doing the edge on edge test?
    LVL 11

    Expert Comment

    Yes, this sounds plausible. But you can simply omit to test the neighbor edges as they will never intersect each other.
    LVL 11

    Accepted Solution

    This implementation should do it, but it assumes that the start and end point is not defined twice (as you did in your other question). It's also the usual way to define polygons in many APIs like Java and Windows GDI. I.e. the square would be defined as:
         static double[] polyX = { 1.0, 3.0, 3.0, 1.0 };
         static double[] polyY = { 1.0, 1.0, 3.0, 3.0 };

         public static boolean checkComplex( final double[] x, final double[] y )
              int i = 0, j;
              for( j = i + 2; j < x.length - 1; j++ )
                  if( intersect( x, y, i, j ) )
                      return true;
              for( i = 1; i < x.length; i++ )
                  for( j = i + 2; j < x.length; j++ )
                      if( intersect( x, y, i, j ) )
                          return true;
              return false;

        public static boolean intersect( final double[] x, final double[] y, int i1, int i2 )
            System.out.println( "i: " + i1 + ", j: " + i2 );
            int s1 = ( i1 > 0 ) ? i1 -1 : x.length - 1;
            int s2 = ( i2 > 0 ) ? i2 -1 : x.length - 1;
            return ccw( x[ s1 ], y[ s1 ], x[ i1 ], y[ i1 ], x[ s2 ], y[ s2 ] )
                != ccw( x[ s1 ], y[ s1 ], x[ i1 ], y[ i1 ], x[ i2 ], y[ i2 ] )
                && ccw( x[ s2 ], y[ s2 ], x[ i2 ], y[ i2 ], x[ s1 ], y[ s1 ] )
                != ccw( x[ s2 ], y[ s2 ], x[ i2 ], y[ i2 ], x[ i1 ], y[ i1 ] );

        public static boolean ccw( double p1x, double p1y, double p2x, double p2y, double p3x, double p3y )    
            double dx1 = p2x - p1x;
            double dy1 = p2y - p1y;
            double dx2 = p3x - p2x;
            double dy2 = p3y - p2y;
            return dy1 * dx2 < dy2 * dx1;

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
    Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
    Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
    This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now