// the y value of a point on the line that intersects with the
// verticle line through x. Return true if an intersection is found,
// false otherwise.
-static bool intersects( const Point3D& p0, const Point3D& p1, double x,
- Point3D *result ) {
+static bool intersects( Point3D p0, Point3D p1, double x, Point3D *result ) {
+ // sort the end points
+ if ( p0.x() > p1.x() ) {
+ Point3D tmp = p0;
+ p0 = p1;
+ p1 = tmp;
+ }
+
+ if ( (x < p0.x()) || (x > p1.x()) ) {
+ // out of range of line segment, bail right away
+ return false;
+ }
+
// equation of a line through (x0,y0) and (x1,y1):
//
// y = y1 + (x - x1) * (y0 - y1) / (x0 - x1)
p2_index = trinodes.find( p2 );
if ( intersects(p1, p2, m.x(), &result) ) {
- // cout << "intersection = " << result << endl;
+ cout << "intersection = " << result << endl;
if ( ( result.y() < p3.y() ) &&
( result.y() > m.y() ) &&
( base_leg != FGTriSeg(p1_index, p2_index) ) ) {
p1_index = trinodes.find( p1 );
p2_index = trinodes.find( p2 );
if ( intersects(p1, p2, m.x(), &result) ) {
- // cout << "intersection = " << result << endl;
+ cout << "intersection = " << result << endl;
if ( ( result.y() < p3.y() ) &&
( result.y() > m.y() ) &&
( base_leg != FGTriSeg(p1_index, p2_index) ) ) {
sum += c[(i+1)%size].x() * c[i].y() - c[i].x() * c[(i+1)%size].y();
}
- return sum / 2.0;
+ // area can be negative or positive depending on the polygon
+ // winding order
+ return fabs(sum / 2.0);
}
FGPolygon polygon_union( const FGPolygon& subject, const FGPolygon& clip ) {
return polygon_clip( POLY_UNION, subject, clip );
}
-
-
-
const FGPolyList& gpc_polys )
{
int debug_counter = 0;
- FGPolygon poly;
int index;
in_nodes.clear();
for ( int i = 0; i < FG_MAX_AREA_TYPES; ++i ) {
polylist[i].clear();
- // cout << "area type = " << i << endl;
+ cout << "area type = " << i << endl;
debug_counter = 0;
current = gpc_polys.polys[i].begin();
last = gpc_polys.polys[i].end();
for ( ; current != last; ++current ) {
gpc_poly = *current;
+
cout << "processing a polygon, contours = "
<< gpc_poly.contours() << endl;
exit(-1);
}
- poly.erase();
-
int j;
-
for ( j = 0; j < gpc_poly.contours(); ++j ) {
cout << " processing contour = " << j << ", nodes = "
<< gpc_poly.contour_size( j ) << ", hole = "
index = in_nodes.unique_add( p );
// junkp = in_nodes.get_node( index );
// fprintf(junkfp, "%.4f %.4f\n", junkp.x(), junkp.y());
- poly.add_node(j, p);
// cout << " - " << index << endl;
}
// fprintf(junkfp, "%.4f %.4f\n",
// gpc_poly->contour[j].vertex[0].x,
// gpc_poly->contour[j].vertex[0].y);
// fclose(junkfp);
-
- poly.set_hole_flag( j, gpc_poly.get_hole_flag( j ) );
}
for ( j = 0; j < gpc_poly.contours(); ++j ) {
- poly.calc_point_inside( j, in_nodes );
+ gpc_poly.calc_point_inside( j, in_nodes );
}
+ polylist[i].push_back( gpc_poly );
+
#if 0
// temporary ... write out hole/polygon info for debugging
- for ( j = 0; j < (int)poly.contours(); ++j ) {
+ for ( j = 0; j < (int)gpc_poly.contours(); ++j ) {
char pname[256];
sprintf(pname, "poly%02d-%02d-%02d", i, debug_counter, j);
+ cout << "writing to " << pname << endl;
FILE *fp = fopen( pname, "w" );
Point3D point;
- for ( int k = 0; k < poly.contour_size( j ); ++k ) {
- point = poly.get_pt( j, k );
+ for ( int k = 0; k < gpc_poly.contour_size( j ); ++k ) {
+ point = gpc_poly.get_pt( j, k );
fprintf( fp, "%.6f %.6f\n", point.x(), point.y() );
}
- point = poly.get_pt( j, 0 );
+ point = gpc_poly.get_pt( j, 0 );
fprintf( fp, "%.6f %.6f\n", point.x(), point.y() );
fclose(fp);
char hname[256];
sprintf(hname, "hole%02d-%02d-%02d", i, debug_counter, j);
FILE *fh = fopen( hname, "w" );
- point = poly.get_point_inside( j );
+ point = gpc_poly.get_point_inside( j );
fprintf( fh, "%.6f %.6f\n", point.x(), point.y() );
fclose(fh);
}
-#endif
-
- polylist[i].push_back( poly );
+ // cout << "type a letter + enter to continue: ";
+ // string input;
+ // cin >> input;
+#endif
+
++debug_counter;
}
}
int i1, i2;
Point3D p1, p2;
point_list node_list = in_nodes.get_node_list();
+ FGPolygon poly;
+
for ( int i = 0; i < FG_MAX_AREA_TYPES; ++i ) {
cout << "area type = " << i << endl;
poly_list_iterator tp_current, tp_last;