void quadtree (int top, int left, int size) { switch (fgetc(input)) { case 'W': for (int i=top; i<top+size; i++) for (int j=left; j<left+size; j++) xbm[i][j/8] &= ~(1 << (j%8)); break; case 'B': for (int i=top; i<top+size; i++) for (int j=left; j<left+size; j++) xbm[i][j/8] |= 1 << (j%8); break; case 'Q': quadtree(top,left,size/2); quadtree(top,left+size/2,size/2); quadtree(top+size/2,left,size/2); quadtree(top+size/2,left+size/2,size/2); } }
int _tmain(int argc, _TCHAR* argv[]) { // Keyboard Input-1 int nTestCase; scanf("%d", &nTestCase); // Algorithm //picnic(nTestCase); quadtree(); return 0; }
void quadtree::_divide() { sf::FloatRect newBounds(_bound.left, _bound.top, _bound.width / 2.f, _bound.height / 2.f); _nWest = std::make_shared<quadtree>(quadtree(newBounds, this, _level + 1)); newBounds = { _bound.left + (_bound.width / 2.f), _bound.top, _bound.width / 2.f, _bound.height / 2.f}; _nEast = std::make_shared<quadtree>(quadtree(newBounds, this, _level + 1)); newBounds = { _bound.left , _bound.top + (_bound.height / 2.f), _bound.width / 2.f, _bound.height / 2.f }; _sWest = std::make_shared<quadtree>(quadtree(newBounds, this, _level + 1)); newBounds = { _bound.left + (_bound.width / 2.f), _bound.top + (_bound.height / 2.f), _bound.width / 2.f, _bound.height / 2.f }; _sEast = std::make_shared<quadtree>(quadtree(newBounds, this, _level + 1)); for (auto &obj : _objectsInNode) { add(obj); } _objectsInNode.clear(); }
int main() { input = fopen("quadtree.in","r"); assert(input!=NULL); fscanf(input,"%d ",&n); quadtree(0,0,n); printf("#define quadtree_width %d\n",n); printf("#define quadtree_height %d\n",n); printf("static char quadtree_bits[] = {\n"); for (int i=0; i<n; i++) { for (int j=0; j<n/8; j++) printf("0x%02x,",xbm[i][j]); printf("\n"); } printf("};\n"); fclose(input); return 0; }
void CollisionSystem::CheckCollisions() { float maxDistance = CFG_GETF("COLLISION_MAX_DISTANCE"); Quadtree<std::shared_ptr<Entity>> quadtree(Rectangle(CFG_GETF("LEVEL_MIN_X"), CFG_GETF("LEVEL_MIN_Y"), CFG_GETF("LEVEL_MAX_X") - CFG_GETF("LEVEL_MIN_X"), CFG_GETF("LEVEL_MAX_Y") - CFG_GETF("LEVEL_MIN_Y"))); auto cameraEntities = Engine::GetInstance().GetAllEntitiesWithComponentOfClass("CameraComponent"); collidableEntities = Engine::GetInstance().GetAllEntitiesWithComponentOfClass("ColliderComponent"); // Clear deleted entities from last iteration. deletedEntities.clear(); std::shared_ptr<Entity> cameraEntity; if (cameraEntities.size() > 0) cameraEntity = cameraEntities[0]; // Build quadtree for close enough entities. for (auto entity : collidableEntities) { auto particleComponent = std::static_pointer_cast<ParticleComponent>(Engine::GetInstance().GetSingleComponentOfClass(entity, "ParticleComponent")); auto position = particleComponent->GetPosition(); // Ignore collision from things that aren't visible. if (cameraEntity) { auto cameraComponent = std::static_pointer_cast<CameraComponent>(Engine::GetInstance().GetSingleComponentOfClass(cameraEntity, "CameraComponent")); auto cameraPosition = cameraComponent->GetPosition(); if (cameraPosition.CalculateDistance(position) > maxDistance) continue; else quadtree.Add(entity, position); } else { quadtree.Add(entity, position); } } // Detect and solve collisions. for (unsigned int i = 0; i < collidableEntities.size(); ++i) { auto entity = collidableEntities[i]; auto particleComponent = std::static_pointer_cast<ParticleComponent>(Engine::GetInstance().GetSingleComponentOfClass(entity, "ParticleComponent")); auto position = particleComponent->GetPosition(); auto quadtreeEntities = quadtree.Get(position); for (unsigned int j = 0; j < quadtreeEntities.size(); ++j) { auto otherEntity = quadtreeEntities[j]; if (std::find(deletedEntities.begin(), deletedEntities.end(), entity->GetId()) != deletedEntities.end()) continue; if (std::find(deletedEntities.begin(), deletedEntities.end(), otherEntity->GetId()) != deletedEntities.end()) continue; if (entity->GetId() != otherEntity->GetId() && IsColliding(entity, otherEntity)) SolveCollision(entity, otherEntity); } } }