patterncppMinor
QuadTree C++ Implementation
Viewed 0 times
implementationquadtreestackoverflow
Problem
I recently created a QuadTree implementation in C++. It varies slightly from most implementations.
Instead of storing elements it only organizes elements. This allows programmers to insert elements into the QuadTree and maintain access of those elements outside the QuadTree.
Nodes will only subdivide if an element is inserted and that element has a different position than other elements in that node. This allows multiple elements of the same position to exist within the QuadTree.
I am hoping to get some feedback on code. I haven't had many people criticize my code before, so I am really looking for ways that I could improve it.
Anyways, I've also put my code up on GitHub: https://github.com/bnpfeife/quadtree
quadtree.hpp:
```
#pragma once
#include
namespace bnp {
// When storing elements in the QuadTree, users may want to use unique/custom
// objects. To aid the QuadTree with interpreting different types of data,
// implement the qt_point_adapter on your objects.
class qt_point_adapter {
public:
virtual float get_x() const = 0; // Retrieves x position
virtual float get_y() const = 0; // Retrieves y position
// Determines if point equals another
virtual bool equals(const qt_point_adapter & point) const;
};
// This is used as the QuadTree's internal point structure. This prevents the
// QuadTree from explicitly using new/delete on points. If one wishes to use
// their own point objects, create an adapter function or use
// qt_point_adapter where applicable.
class qt_point : public qt_point_adapter {
private:
float mX; // Stores x position
float mY; // Stores y position
public:
void set_x(const float& x); // Assigns x position
void set_y(const float& y); // Assigns y position
float get_x() const; // Retrieves x position
float get_y() const; // Retrieves y position
// Constructor for initializing POD members
qt_point();
// Constructor for assigning POD members
qt_point(const float& x, const float& y);
};
Instead of storing elements it only organizes elements. This allows programmers to insert elements into the QuadTree and maintain access of those elements outside the QuadTree.
Nodes will only subdivide if an element is inserted and that element has a different position than other elements in that node. This allows multiple elements of the same position to exist within the QuadTree.
I am hoping to get some feedback on code. I haven't had many people criticize my code before, so I am really looking for ways that I could improve it.
Anyways, I've also put my code up on GitHub: https://github.com/bnpfeife/quadtree
quadtree.hpp:
```
#pragma once
#include
namespace bnp {
// When storing elements in the QuadTree, users may want to use unique/custom
// objects. To aid the QuadTree with interpreting different types of data,
// implement the qt_point_adapter on your objects.
class qt_point_adapter {
public:
virtual float get_x() const = 0; // Retrieves x position
virtual float get_y() const = 0; // Retrieves y position
// Determines if point equals another
virtual bool equals(const qt_point_adapter & point) const;
};
// This is used as the QuadTree's internal point structure. This prevents the
// QuadTree from explicitly using new/delete on points. If one wishes to use
// their own point objects, create an adapter function or use
// qt_point_adapter where applicable.
class qt_point : public qt_point_adapter {
private:
float mX; // Stores x position
float mY; // Stores y position
public:
void set_x(const float& x); // Assigns x position
void set_y(const float& y); // Assigns y position
float get_x() const; // Retrieves x position
float get_y() const; // Retrieves y position
// Constructor for initializing POD members
qt_point();
// Constructor for assigning POD members
qt_point(const float& x, const float& y);
};
Solution
A minor comment about your
It is somewhat confusing that you check equality by double negation. Just check for equality and remove the pointless comment while you're at it:
Also you might want to define those directly as operators:
equals method:// Determines if point equals another
bool qt_point_adapter::equals(const qt_point_adapter & point) const {
// Determines if point equals another
return !(get_x() != point.get_x() || get_y() != point.get_y());
}It is somewhat confusing that you check equality by double negation. Just check for equality and remove the pointless comment while you're at it:
bool qt_point_adapter::equals(const qt_point_adapter & point) const {
return (get_x() == point.get_x() && get_y() == point.get_y());
}Also you might want to define those directly as operators:
// Determines if point equals another
bool operator==(const qt_point_adapter &point1,
const qt_point_adapter &point2) const {
return (point1.get_x() == point2.get_x() && point1.get_y() == point2.get_y());
}
bool operator!=(const qt_point_adapter &point1,
const qt_point_adapter &point2) const {
return !(point1 == point2);
}Code Snippets
// Determines if point equals another
bool qt_point_adapter::equals(const qt_point_adapter & point) const {
// Determines if point equals another
return !(get_x() != point.get_x() || get_y() != point.get_y());
}bool qt_point_adapter::equals(const qt_point_adapter & point) const {
return (get_x() == point.get_x() && get_y() == point.get_y());
}// Determines if point equals another
bool operator==(const qt_point_adapter &point1,
const qt_point_adapter &point2) const {
return (point1.get_x() == point2.get_x() && point1.get_y() == point2.get_y());
}
bool operator!=(const qt_point_adapter &point1,
const qt_point_adapter &point2) const {
return !(point1 == point2);
}Context
StackExchange Code Review Q#143955, answer score: 2
Revisions (0)
No revisions yet.