enclosure_status QuadTree<T>::getEnclosureStatus (const vertex& center, const vertex& range, const vertex& minXY, const vertex& maxXY) { int enclosedPts = 0; enclosedPts += pointInRegion ({center.x-range.x, center.y-range.y}, minXY, maxXY); enclosedPts += pointInRegion ({center.x-range.x, center.y+range.y}, minXY, maxXY); enclosedPts += pointInRegion ({center.x+range.x, center.y-range.y}, minXY, maxXY); enclosedPts += pointInRegion ({center.x+range.x, center.y+range.y}, minXY, maxXY); if (enclosedPts == 4){ return NODE_CONTAINED_BY_REGION; } else if (enclosedPts > 0){ return NODE_PARTIALLY_IN_REGION; } else { vertex nodeMin (center.x-range.x, center.y-range.y); vertex nodeMax (center.x+range.x, center.y+range.y); enclosedPts += pointInRegion (minXY, nodeMin, nodeMax); enclosedPts += pointInRegion ({minXY.x, maxXY.y}, nodeMin, nodeMax); enclosedPts += pointInRegion (maxXY, nodeMin, nodeMax); enclosedPts += pointInRegion ({maxXY.x, minXY.y}, nodeMin, nodeMax); if (enclosedPts > 0){ return NODE_PARTIALLY_IN_REGION; } } return NODE_NOT_IN_REGION; }
COLLISION_STATUS Game::getCollisionStatus (Ball* b, vec3 &pos, const Brick* r){ if (b->velocity.x >= 0){ if (b->velocity.y >= 0){ if (pointInRegion(pos, r)){ b->velocity.y = -b->velocity.y; pos.y = r->origin.y; return COLLIDES_BOTTOM; } } else{ if (pointInRegion(pos, r)){ b->velocity.y = -b->velocity.y; pos.y = r->origin.y+r->size; return COLLIDES_TOP; } } } else{ if (b->velocity.y >= 0){ if (pointInRegion(pos, r)){ b->velocity.y = -b->velocity.y; pos.y = r->origin.y; return COLLIDES_BOTTOM; } } else{ if (pointInRegion(pos, r)){ b->velocity.y = -b->velocity.y; pos.y = r->origin.y+r->size; return COLLIDES_TOP; } } } return NO_COLLISION; }
void XUIObject::onMouseMove(int x, int y) { if (m_dndDragging) return; int _x = x; int _y = y; m_wnd->clientToScreen(&_x, &_y); if (pointInRegion(x, y)) { if (!m_mouseInArea) { m_mouseInArea = 1; beginCapture(CAPTURETYPE_AREA); onMouseEnter(); } } else { if (m_mouseInArea) { m_mouseInArea = 0; endCapture(CAPTURETYPE_AREA); onMouseLeave(); // prevent further mousemove event processing, because we would not have gotten here // had we not captured the mouse, and we only capture the mouse to implement the // mouseenter/mouseleave events, not to cause any more mousemove events than normal. return; } } eventSource_onMouseMove(x, y); if (m_draggingWindow) { int ox = _x - m_draganchor_x; int oy = _y - m_draganchor_y; RECT r; m_wnd->getRootParent()->getWindowRect(&r); m_wnd->getRootParent()->move(r.left + ox, r.top + oy); m_draganchor_x = _x; m_draganchor_y = _y; } if (m_dndlbuttondown && !m_trieddnd) { if (ABS(_x - m_draganchor_x) >= 4 || ABS(_y - m_draganchor_y) >= 4) { m_trieddnd = true; onBeginDrag(NULL); m_wnd->handleDrag(); } } }
vector <pair <vertex, T> > QuadTree<T>::getObjectsInRegion (vertex minXY, vertex maxXY) { vector <pair <vertex, T> > results; queue <QTNode<T>*> nodes; nodes.push (root); while (!nodes.empty()){ QTNode<T>* top = nodes.front(); if (top->leaf){ enclosure_status status = getEnclosureStatus(top->center, top->range, minXY, maxXY); switch (status){ // this node is completely contained within the search region case NODE_CONTAINED_BY_REGION: // add all elements to results results.insert (results.end(), top->bucket.begin(), top->bucket.end()); break; // this node is partially contained by the region case NODE_PARTIALLY_IN_REGION: // search through this leaf node's bucket for (int i=0; i < top->bucket.size(); ++i){ // check if this point is in the region if (pointInRegion(top->bucket[i].first, minXY, maxXY)){ results.push_back (top->bucket[i]); } } break; // this node definitely has no points in the region case NODE_NOT_IN_REGION: // do nothing break; } } else{ for (int i=0; i < 4; ++i){ if (top->child[i]){ // check if this nodes children could have points in the region enclosure_status status = getEnclosureStatus (top->child[i]->center, top->child[i]->range, minXY, maxXY); switch (status){ // this node is completely contained by region, add all points within case NODE_CONTAINED_BY_REGION: addAllPointsToResults (top->child[i], results); break; // this node might contain points in the region case NODE_PARTIALLY_IN_REGION: nodes.push (top->child[i]); break; // no points in region, discontinue searching this branch case NODE_NOT_IN_REGION: break; } } } } nodes.pop(); } return results; }