patterncppMinor
Templated Point class of any dimension
Viewed 0 times
templatedpointanydimensionclass
Problem
I needed to make a point class for a term project that involved arbitrary dimensional points. I came up with this, and I know that it works fine, but I'm looking to improve it so I can reuse it in the future. Any criticisms or suggestions are very much appreciated.
#ifndef POINT_HPP
#define POINT_HPP
#include
#include
#include
#include
template
struct Point;
template
struct Point {
Point(const std::initializer_list& v) {
size_t i = 0;
for (auto e : v) {
coordinates_[i++] = e;
}
}
explicit Point(const std::array& arr) : coordinates_{arr} {}
double operator[](size_t dimension) const { return coordinates_[dimension]; }
Point& operator=(const Point& other) {
this->coordinates_ = other.coordinates_;
return *this;
}
std::array coordinates_;
};
/*
* Finds euclidian distance between two Point classes of same dimension
*/
template
double distance(const Point& lhs, const Point& rhs) {
double dist = 0;
for (size_t i = 0; i
double distance(const Point& point, double value, int axis) {
return std::abs(point[axis] - value);
}
/*
* Print the point components of a Point in ascending order.
* Prints in the form {x, y, z} with no newline.
*/
template
std::ostream& operator& point) {
os
bool operator==(const Point& lhs, const Point& rhs) {
return lhs.coordinates_ == rhs.coordinates_;
}
namespace std {
/*
* Make our Point class hashable
*/
template
struct hash> {
size_t operator()(const Point& x) const {
return hash>()(x.coordinates_);
}
};
/*
* Also make it swappable for sorting
*/
template
void swap(Point& lhs, Point& rhs) {
swap(lhs.coordinates_, rhs.coordinates_);
}
} // namespace std
#endif // POINT_HPPSolution
Overall, this seems to be a small but useful template. However, there are a few things that I think might be improved.
Sorting requires more than
While the provided
Consider default constructor
There is no simple way at the moment to create a generic templated origin point (all coordinates zero). This makes it a bit more difficult to create something like a
Consider projection operator
It's often useful to project, say, a 3D point onto a 2D canvas. This could also likely be made generic.
Do a range check
The
Sorting requires more than
swapWhile the provided
swap function would assist in some kind of sorting, without a comparison operator, we still can't actually sort. This may well be by design, in which case a comment in the code pointing this out would be helpful.Consider default constructor
There is no simple way at the moment to create a generic templated origin point (all coordinates zero). This makes it a bit more difficult to create something like a
distance_from_origin function that is generic.Consider projection operator
It's often useful to project, say, a 3D point onto a 2D canvas. This could also likely be made generic.
Do a range check
The
distance function that measures from an axis does no range checking on the axis number and there does not seem to be a good generic way to add one. Consider providing a range checking version.Context
StackExchange Code Review Q#150315, answer score: 5
Revisions (0)
No revisions yet.