我正在用C++实现四叉树,在过去的一段时间里,我一直无法解决细分错误。问题是,我到处都有指针,但不确定我的问题在哪里。我已经用不同的语言实现了我的代码,只是想测试我的C++技能,但这已经成为问题。
这是代码:
quadtree.h:
#ifndef QUADTREE_QUADTREE_H
#define QUADTREE_QUADTREE_H
#endif //QUADTREE_QUADTREE_H
#include <vector>
using namespace std;
class Point
{
public:
Point(double x, double y);
double x;
double y;
};
class Rectangle
{
public:
Rectangle(double x, double y, double width, double height);
double x;
double y;
double width;
double height;
bool contains(Point* p);
};
class QuadTree
{
public:
QuadTree();
QuadTree(Rectangle* boundary, int capacity);
Rectangle* boundary = new Rectangle(200,200,200,200);
QuadTree* qt = new QuadTree(boundary, 4);
// Rectangle* boundary;
Point* p;
int capacity;
vector<Point*> points;
QuadTree* NE;
QuadTree* NW;
QuadTree* SW;
QuadTree* SE;
bool divided;
void subdivide();
void buildTree();
void insertToNode(Point* p);
};
quadtree.cpp
#include "quadtree.h"
#include <random>
Point::Point(double x, double y)
{
this->x = x;
this->y = y;
}
Rectangle::Rectangle(double x, double y, double width, double height)
{
this->x = x;
this->y = y;
this->width = width;
this->height = height;
}
bool Rectangle::contains(Point* p)
{
return (p->x > this->x - this->width && p->x < this->x + this->width && p->y > this->y - this->height && p->y < this->y + this->height);
}
QuadTree::QuadTree() {}
QuadTree::QuadTree(Rectangle* boundary, int capacity)
{
this->boundary = boundary;
this->capacity = capacity;
this->divided = false;
}
void QuadTree::subdivide()
{
Rectangle* nw = new Rectangle(this->boundary->x + this->boundary->width/2, this->boundary->y - this->boundary->height/2, this->boundary->width/2, this->boundary->height/2);
Rectangle* ne = new Rectangle(this->boundary->x - this->boundary->width/2, this->boundary->y - this->boundary->height/2, this->boundary->width/2, this->boundary->height/2);
Rectangle* sw = new Rectangle(this->boundary->x + this->boundary->width/2, this->boundary->y + this->boundary->height/2, this->boundary->width/2, this->boundary->height/2);
Rectangle* se = new Rectangle(this->boundary->x - this->boundary->width/2, this->boundary->y + this->boundary->height/2, this->boundary->width/2, this->boundary->height/2);
this->NE = new QuadTree(ne, 1);
this->NW = new QuadTree(nw, 1);
this->SW = new QuadTree(sw, 1);
this->SE = new QuadTree(se, 1);
this->divided = true;
}
void QuadTree::buildTree()
{
std::random_device rd; // obtain a random number from hardware
std::mt19937 eng(rd()); // seed the generator
std::uniform_int_distribution<> distr(0, 199); // define the range
for(int i = 0; i < 1; i++)
{
Point* p = new Point(distr(eng), distr(eng));
this->qt->insertToNode(p);
}
}
void QuadTree::insertToNode(Point* p)
{
if(!this->boundary->contains(p))
{
return;
}
if(this->points.size() < this->capacity)
{
this->points.push_back(p);
}
else
{
if(!this->divided)
{
this->subdivide();
}
}
this->NE->insertToNode(p);
this->NW->insertToNode(p);
this->SW->insertToNode(p);
this->SE->insertToNode(p);
}
最佳答案
您的构造函数将递归调用自己,直到耗尽堆栈空间为止。
您已经为qt
指定了默认值,它将分配另一个QuadTree
项。由于2参数构造函数不会为qt
提供不同的初始值,因此将分配另一个QuadTree
对象,并且将递归调用该构造函数,直到耗尽堆栈空间或内存为止。
您需要重新考虑qt
在做什么。甚至有必要吗?