Home > C++ Classes, Programming > Point Class

Point Class

August 1st, 2009 Leave a comment Go to comments

To download:

1
bzr branch http://bazaar.enseed.com/demo/point/

This is a C++ point class (or vector if you prefer). Features were added as I needed them. I use it regularly so it’s got pretty much everything one needs.

There is a general point class (see Point.h) and four specialized classes (Point1, Point2, Point3 and Point4). Once you have included Point.h, you don’t need to ask for a specialized version, the compiler will choose it automatically. For example:

1
2
3
4
5
6
7
Point<float, 1> p1d;  // will use Point1
Point<float, 2> p2d;  // will use Point2
Point<float, 3> p3d;  // will use Point3
Point<float, 4> p4d;  // will use Point4
Point<float, 5> p5d;  // will use Point
Point<float, 6> p6d;  // will use Point
...

The class has no direct dependency on the Matrix class, but it has a forward declaration and will let you convert a Point into aMatrix or a Matrix into a Point. However, as long as you don’t use the Matrix class, you don’t need to include it and the Pointclass will not use it.

1
2
3
4
5
6
Point4d p4d(1);  // makes a point with {1, 1, 1, 1}
p4d.asMatrix();  // conversion to Matrix<double, 4, 1> (at no cost)
p4d.asTMatrix(); // conversion to a Matrix<double, 1, 4> (at no cost)

Matrix<double, 1, 3> m;
Point3d p3d(m);  // initialize from a Matrix

In addition to the standard methods you’d expect from a Point class (length, dot product, etc…), you can also easily build points from existing points:

1
2
3
4
5
6
7
Point3d rgb(0.5, 0.1, 0.2);
Point4d rgba(rgb, 1.0);
Point3d rrr = rgb.xxx();
Point3d bgr = rgb.zyx();

Point4d p4d;
Point3d = p4d.xyz()/p4d.w(); // projection of a 4d point

There are more constructors and accessors (yx(), xxy(), yxy(), xyxy(), …) than you will probably ever need…

Categories: C++ Classes, Programming Tags:
  1. No comments yet.
  1. No trackbacks yet.
*

Please leave these two fields as-is:

Protected by Invisible Defender. Showed 403 to 153,475 bad guys.