首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在我的QuadTree实现中遇到问题

在我的QuadTree实现中遇到问题
EN

Stack Overflow用户
提问于 2018-10-06 03:58:46
回答 1查看 116关注 0票数 1

我正在C++中实现一个四叉树,在过去的一段时间里,我一直无法解决我的分割错误。问题是,我到处都有指点,不太清楚我的问题出在哪里。我已经用不同的语言实现了我的代码,只想测试我的C++技能,但这阻碍了我的工作。

下面是代码:

四叉树h:

代码语言:javascript
复制
#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

代码语言:javascript
复制
#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);
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-10-06 04:22:53

构造函数将递归地调用自己,直到堆栈空间用完为止。

您已经为qt指定了一个默认值,它将分配另一个QuadTree项。由于2参数构造函数没有为qt提供不同的初始值,因此将分配另一个QuadTree对象,构造函数将被递归调用,直到堆栈空间或内存用完为止。

您需要重新考虑qt正在做什么。有必要吗?

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52675572

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档