?
Solved

Determine whether a polygon is complex or simple

Posted on 2006-03-29
4
Medium Priority
?
1,807 Views
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.
0
Comment
Question by:stats786
  • 3
4 Comments
 
LVL 11

Expert Comment

by:x4u
ID: 16327543
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:
http://www.macs.hw.ac.uk/~alison/ds98/node114.html
0
 

Author Comment

by:stats786
ID: 16328514
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?
0
 
LVL 11

Expert Comment

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

Accepted Solution

by:
x4u earned 750 total points
ID: 16328957
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;
    }
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month14 days, 23 hours left to enroll

839 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