JKawa
JKawa

Reputation: 179

Running into issues with my QuadTree Implementation

I'm working on implementing a Quad Tree in C++ and for the past amount of time, I haven't been able to solve my segmentation fault error. The thing is, I have pointers all over the place and not quite sure where my problem is. I already have implemented my code in different languages and simply wanted to test my C++ skills but this is getting in the way.

Here's the code:

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);
}

Upvotes: 1

Views: 139

Answers (1)

1201ProgramAlarm
1201ProgramAlarm

Reputation: 32727

Your constructors will call themselves recursively until you run out of stack space.

You've specified a default value for qt, which will allocate another QuadTree item. Since the 2 parameter constructor does not provide a different initial value for qt, another QuadTree object will be allocated, and the constructor will be called recursively until you run out of stack space or memory.

You need to rethink what qt is doing. Is it even necessary?

Upvotes: 2

Related Questions