Determine whether a polygon is complex or simple

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.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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:
stats786Author Commented:
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?
Yes, this sounds plausible. But you can simply omit to test the neighbor edges as they will never intersect each other.
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;

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.