- All Points are Vectors, and all Vectors are Points.
- All Directions are Vectors, NOT all Vectors are Directions (this shouldn’t mean both way conversion shouldn’t be allowed).
I want to have the operators overridden once for all preferably since they’re all completely identical. In C++ I can just define class Vector { float x,y,z; }, and do typedef Point = Vector, typedef Direction = Vector; In C# there is no equivalent (“using Point=Vector;” sucks as you have to place it in every single document you use, and it’s not enforced by the compiler).
I tried to define 3 different classes and override the operators for each, then do implicit type casting which would make the code run slower, etc.
I tried defining just Vector, then Point:Vector and Direction:Vector, this way I only write the operators once but then I can’t do implicit type casting Point <-> Vector or Direction <->Vector.
I could simply define the Vector class and use that everywhere, but that would create ambiguity as to weather a variable is supposed to be a position in space (Point), a relative position in space (Vector) or a unit vector (Direction). For example the function:
Vector GetOrthogon(Vector a, Vector b) {
// ....
}
You can’t know whether it’s expecting any vectors or unit vectors. In C++ you could do that, so why not in C#?
Note: having structs instead of classes would be ideal if possible.
Mathematically, Points are Vectors. There are no absolute points in space. Points are defined as vectors from some arbitrary origin. So, I use Vectors for both points and differences between points.
Because a direction is a unit vector, there’s no need for a distinction there either. It’s like trying to define different static types for the integer 1 and other integers. So I use vectors for both directions and differences between points.
So, define a single Vector type. It’ll make your life easier because you’ll have fewer classes and overloaded operators/functions to write and test and will be mathematically “purer” (if that matters to you).