/*! */ Vector2D Line2D::intersection( const Line2D & line1, const Line2D & line2 ) { double tmp = line1.a() * line2.b() - line1.b() * line2.a(); if ( std::fabs( tmp ) < EPSILON ) { return Vector2D::INVALIDATED; } return Vector2D( (line1.b() * line2.c() - line2.b() * line1.c()) / tmp, (line2.a() * line1.c() - line1.a() * line2.c()) / tmp ); }
bool Box2D::intersect(const Line2D& line) const { const double x1 = -(line.b()*topLeft_.v() + line.c())/line.a(); const double x2 = -(line.b()*bottomRight_.v() + line.c())/line.a(); if(x1 < topLeft_.u() && x2 < topLeft_.u()) return false; if(x1 > bottomRight_.u() && x2 > bottomRight_.u()) return false; const double y1 = -(line.a()*topLeft_.u() + line.c())/line.b(); const double y2 = -(line.a()*bottomRight_.u() + line.c())/line.b(); if(y1 < topLeft_.v() && y2 < topLeft_.v()) return false; if(y1 > bottomRight_.v() && y2 > bottomRight_.v()) return false; return true; }
/*! */ bool Segment2D::existIntersection( const Line2D & l ) const { double a0 = l.a() * origin().x + l.b() * origin().y + l.c(); double a1 = l.a() * terminal().x + l.b() * terminal().y + l.c(); return a0 * a1 <= 0.0; }
/*! */ int Circle2D::intersection( const Line2D & line, Vector2D * sol1, Vector2D * sol2 ) const { if ( std::fabs( line.a() ) < EPSILON ) { if ( std::fabs( line.b() ) < EPSILON ) { std::cerr << "Circle2D::intersection() illegal line." << std::endl; return 0; } // Line: By + C = 0 ---> y = -C/B // Circle: (x - cx)^2 + (y - cy)^2 = r^2 // ---> double x1 = 0.0, x2 = 0.0; int n_sol = QUADRATIC_FOMULA( 1.0, -2.0 * center().x, ( SQUARE( center().x ) + SQUARE( line.c() / line.b() + center().y ) - SQUARE( radius() ) ), x1, x2 ); if ( n_sol > 0 ) { double y1 = -line.c() / line.b(); if ( sol1 ) { sol1->assign( x1, y1 ); } if ( n_sol > 1 && sol2 ) { sol2->assign( x2, y1 ); } } return n_sol; } else { // include (fabs(l.b()) < EPSILON) case // use line & circle formula // Ax + By + C = 0 // (x - cx)^2 + (y - cy)^2 = r^2 // make y's quadratic formula using these fomula. double m = line.b() / line.a(); double d = line.c() / line.a(); double a = 1.0 + m * m; double b = 2.0 * ( -center().y + ( d + center().x ) * m ); double c = SQUARE( d + center().x ) + SQUARE( center().y ) - SQUARE( radius() ); double y1 = 0.0, y2 = 0.0; int n_sol = QUADRATIC_FOMULA( a, b, c, y1, y2 ); if ( n_sol > 0 && sol1 ) { sol1->assign( line.getX( y1 ), y1 ); } if ( n_sol > 1 && sol2 ) { sol2->assign( line.getX( y2 ), y2 ); } return n_sol; } }