bool Polygon2d::intersects(const Point2& origin, const Vector2& direction, RayIntersectionType it) const { Point2 endpoint = origin + direction; if( Polygon2d::encompasses(origin) || Polygon2d::encompasses(endpoint) ) { return true; } // We need to test each minor line segment to see if it intersects. // NOTE: There are more efficient algorithms for doing this, which // involve sorting a list of points. This was easy to write. blocxx::Array<Point2>::const_iterator first = m_points.begin(); blocxx::Array<Point2>::const_iterator previous = first; blocxx::Array<Point2>::const_iterator current = previous + 1; NumberType unused1, unused2; while( current != m_points.end() ) { if( segmentsIntersect(origin, direction, *previous, *current - *previous, unused1, unused2) ) { return true; } previous = current; ++current; } // CHECKME! I think this test is redundant for any enclosed polygon. if( segmentsIntersect(origin, direction, *previous, *first - *previous, unused1, unused2) ) { return true; } return false; }
// As above, but returns info about where along segment p1-p2 collision occurs. Returns 0 if segment is entirely contained in rect. bool Rect::intersects(const Point &p1, const Point &p2, member_type &collisionTime) const { collisionTime = 0; return ( segmentsIntersect(p1, p2, Point(min.x, min.y), Point(min.x, max.y), collisionTime) || segmentsIntersect(p1, p2, Point(min.x, max.y), Point(max.x, max.y), collisionTime) || segmentsIntersect(p1, p2, Point(max.x, max.y), Point(max.x, min.y), collisionTime) || segmentsIntersect(p1, p2, Point(max.x, min.y), Point(min.x, min.y), collisionTime) || contains(p1) || contains(p2) ); }
vector<coord> algs::visibleVertices(const vector<pair<coord, coord> > &edges, vector<coord> visible, const coord &vertex) { //cout << "visible vertices size: " << visible.size() << endl; //cout << "edges size: " << edges.size() << endl; for(vector<pair<coord, coord> >::size_type k = 0; k < edges.size(); ++k){ vector<coord>::iterator iter = visible.begin(); while(iter != visible.end()){ //if edge intersects with anything, remove from visible coords if(segmentsIntersect(vertex, *iter, edges[k].first, edges[k].second)) visible.erase(iter); else ++iter; } } return visible; }