I felt like just pursuing the point slope form for kicks n such, and thought you guys might get a kick out of the way I tried to solve it. I take like any of you're guys' advice in doing it, so it's definitely pretty comical with all the special cases and stuff. I intend to try the hyperplane and parametric forms of this stuff -- once I finish applying to colleges. I'm trying not to let school get in the way of my education, but I gotta make an exception for stupid colleges.
Anyways, get ready for a laugh.
bool RotatingPong::CLineSegment::intersects( const CLineSegment & other ){ // Same (note: overloaded equality operator) if( *this == other ) { dout << "*this == other\n"; return true; } // Both vertical if( m_vertical && other.m_vertical ) { // On same line, check containing/overlap if( m_end.x == other.m_end.x && ( ( m_end.y <= max( other.m_end.y, other.m_start.y ) && m_end.y >= min( other.m_end.y, other.m_start.y ) ) || ( m_start.y <= max( other.m_end.y, other.m_start.y ) && m_start.y >= min( other.m_end.y, other.m_start.y ) ) || ( other.m_end.y <= max( m_end.y, m_start.y ) && other.m_end.y >= min( m_end.y, m_start.y ) ) || ( other.m_start.y <= max( m_end.y, m_start.y ) && other.m_start.y >= min( m_end.y, m_start.y ) ) ) ) { dout << "verticals\n"; return true; } else return false; } // One vertical if( m_vertical && !other.m_vertical ) { double y = other.m_slope * m_end.x - other.m_slope * other.m_end.x + other.m_end.y; double vmin_y = min( m_start.y, m_end.y ); double vmax_y = max( m_start.y, m_end.y ); if( y >= vmin_y && y <= vmax_y && m_end.x < max( other.m_end.x, other.m_start.x ) && m_end.x > min( other.m_end.x, other.m_start.x ) ) { return true; } else return false; } // Other vertical else if( other.m_vertical && !m_vertical ) { double y = m_slope * other.m_end.x - m_slope * m_end.x + m_end.y; double vmin_y = min( other.m_start.y, other.m_end.y ); double vmax_y = max( other.m_start.y, other.m_end.y ); if( y >= vmin_y && y <= vmax_y && other.m_end.x < max( m_end.x, m_start.x ) && other.m_end.x > min( m_end.x, m_start.x ) ) { return true; } else return false; } // Same slope else if( abs(m_slope - other.m_slope) < 0.001 ) { // Special case checking intersection of horizontals if( abs(m_slope) < 0.001 && abs(other.m_slope) < 0.001 ) { if( m_end.y == other.m_end.y && ( ( m_end.x <= max( other.m_end.x, other.m_start.x ) && m_end.x >= min( other.m_end.x, other.m_start.x ) ) || ( m_start.x <= max( other.m_end.x, other.m_start.x ) && m_start.x >= min( other.m_end.x, other.m_start.x ) ) || ( other.m_end.x <= max( m_end.x, m_start.x ) && other.m_end.x >= min( m_end.x, m_start.x ) ) || ( other.m_start.x <= max( m_end.x, m_start.x ) && other.m_start.x >= min( m_end.x, m_start.x ) ) ) ) { return true; } else return false; } // General case checking intersection of lines with same slope if( m_end.y - other.m_end.y == m_slope * ( m_end.x - other.m_end.x ) || m_end.y - other.m_start.y == m_slope * ( m_end.x - other.m_start.y ) ) { return true; } else return false; } // // General case for line intersection // o Solves for point slope representations // // // // yb - ya + ma * xa - mb * xb // x = -------------------------- // ma - mb // // ma * mb * ( xb - xa ) + mb * ya - ma * yb // y = ---------------------------------------- // mb - ma double x = ( other.m_end.y - m_end.y - other.m_slope * other.m_end.x + m_slope * m_end.x ) / ( m_slope - other.m_slope ); double y = ( other.m_slope * m_end.y - m_slope * other.m_end.y + m_slope * other.m_slope * ( other.m_end.x - m_end.x ) ) / ( other.m_slope - m_slope ); // x I [x1, x2] && x I [x3, x4] // y I [y1, y2] && y I [y3, y4] if( x >= min( m_end.x, m_start.x ) && x <= max( m_end.x, m_start.x ) && x >= min( other.m_end.x, other.m_start.x ) && x <= max( other.m_end.x, other.m_start.x ) && y >= min( m_end.y, m_start.y ) && y <= max( m_end.y, m_start.y ) && y >= min( other.m_end.y, other.m_start.y ) && y <= max( other.m_end.y, other.m_start.y ) ) { return true; } else return false;}
I use this method many times as I check every single border of a quadrilateral for a line intersection! I am very thankful that technology these days makes fast computers, because I'm being very brutal!
bool RotatingPong::CRotatingSquare::intersects( const CRotatingSquare & o ) const{ CPoint ( CRotatingSquare::* vertices [ 4 ] )( void ) const = { &CRotatingSquare::tl, &CRotatingSquare::tr, &CRotatingSquare::br, &CRotatingSquare::bl }; const int v_size = sizeof(vertices) / sizeof(vertices[0]); for( int i = 0; i < v_size; ++i ) { CLineSegment c( (*this.*vertices)(), (*this.*vertices[(i + 1) % v_size])() ); for( int j = 0; j < v_size; ++j ) { CLineSegment d( (o.*vertices[j])(), (o.*vertices[(j + 1) % v_size])() ); if( c.intersects(d) ) { return true; } } } return false;}