Solved

Determine whether a polygon is complex or simple

Posted on 2006-03-29
Medium Priority
1,807 Views
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
Question by:stats786
• 3

LVL 11

Expert Comment

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

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

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

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

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…