Includes simple classes for points, rectangles, dimensions, lines, circles, etc.
The geometry classes are meant to be simple and efficient yet provide sufficient versatility. Most classes support the following:
- template typing, so that the geometric primitives can be instantiated for int, float, double, or many other underlying base types. Not all features may work for any underlying base type, depending on what that type supports (e.g., addition of geometry types requires that addition be defined for the underlying base type).
- explicit constructors, which eliminates the risks associated with implicit conversion and construction. This also means that one must be explicit when converting between geometric primitives that use different underlying base types. For example: Note that clamping and rounding may occur when converting, based on the behavior of nrt::clamped_convert which is used internally by converting geometry constructors, for example: Finally note that rounding is based on the C++11 std::round() function, which correctly handles both positive and negative numbers (see test-Point.C for examples).
- copy and move constructors and assignment so that it is inexpensive to return geometric primitives from functions.
- operator overloads where they make sense:
- All geometric primitives support equality and inequality operators.
- Most geometric primitives multiplication and division by a double-precision scalar, which scales all coordinates in the primitive.
- Many geometric primitives support a free function
nrt::scale()
, which differs from multiplication in that it will scale the size of the object without changing its position, while multiplication scales all coordinates (so multiplication is appropriate for, e.g., resolution changes while scale()
is for growing/shrinking an object without moving it).
- Many geometric primitives support a free function
nrt::rotate()
which rotates the primitive about its canonical center.
- Most geometric primitives support an
area()
function that returns the area as a double-precision number.
- Many geometric primitives support addition and subtraction with a Point2D of same template parameter, which corresponds to translation.
- Some geometric primitives support addition and subtraction with another object of their own (e.g., Dims<int> + Dims<int>) but usually product and division of that kind is not supported as it makes little sense.
- read-only access member functions, to fetch elements out of the geometric primitives (e.g., get the center coordinates and the radius of a Circle). No write member functions are provided, instead you should create a new geometric primitive. For example:
int radius = circle.radius();
nrt::point2D<int> center = circle.center();
circle.radius() + 5);
- a contains(Point2D<U> point) function which returns true if a point is inside the geometric primitive (for Circle, Rectangle, Dims, etc). Any point is ok for this function.
Many examples of use are provided in test-Geometry.C, test-Point.C and other example files.