Vector2 and Point2

Started by
3 comments, last by Zakwayda 19 years, 5 months ago
Which are the differences between a point and a vector,since the point of view of programming ? The only thing that I know is that (point - point = vector), so, I would make a class Vector2 and a class Point2 ? or only Vector2 (the same for Vector3) ? thnx
Advertisement
I tend to define two separate classes Point and Vector. The allowed operations are:
Vector + Vector = Vector
Vector - Vector = Vector
Vector * Number = Vector
Number * Vector = Vector
Point + Vector = Point
Point - Vector = Point
Point - Point = Vector
a_1*Point + a_2*Point + ... + a_n*Point = Point if a_1+a_2+...+a_n=1 (baricenter)

A lot of people would just have a single class for both, although that is not as type-safe as having separate classes. For instance, adding two points is not defined, and the compiler will complain if you have separate classes and try to do it.

if the point doesnt add any behavior over the vector, its not needed to give it its seperate class. imho.

if it holds other information, you could derive a point class from vector, or just create a point class with a member 'vector position'

it does make sense to create a point class with some extended behavior, like storing position in local and worldcoordinates or other properties.
Conceptually, I like to think of a point being non-representative, with vectors representing the actual point. (just because most of the math involves vectors, and doesn't make sense for points). For a point class, I put a place vector in to describe the point, that way my algorithms make sense. Something like:

class xPoint3D{private:   xVector3D placeVector;public:   xVector3D operator- ( xPoint3D );  // retrieve vector from subtraction   xPoint3D operator+ ( xVector3D );  // translate point by vector   xPoint3D operator* ( xScalar );    // scale point (from origin)   xPoint3D operator* ( xMatrix3x3 ); // transform point through matrix   // etc.};

and in my vector class ...
class xVector3D{private:   double compX, compY, compZ; // x,y and z components representing vectorpublic:   // vector related functions};

Hope this helps and good luck!
You might do a search for 'homogenous coordinates' and read some of the threads on that topic. With homogenous coordinates you can represent points with w = 1 and vectors with w = 0. Then, all the math comes out correctly (for example the properties in alvaro's post are enforced, among other things).

However, I haven't seen much use of homogenous coordinates in production code (except for projection, shadow volumes, or other techniques that use 4x4 matrices). You end up with a lot of pointless operations (multiplication by 0 and 1, etc.), so in practice, people seem to just stick with a 3d vector class and use it for both points and vectors.

With this approach, it's up to the programmer to avoid operations that don't make sense, such as translating a normal.

This topic is closed to new replies.

Advertisement