When calculating the normal of a vector, which is considered canon:
Returning a copy:
Vector2D Vector2D::Normalize() const {
double a1 = GetX();
double a2 = GetY();
double a3 = GetZ();
double length = GetLength();
assert(Math::IsEqual(length, 0.0) == false);
if(Math::IsEqual(length, 0.0)) {
throw Exception("Can not normalize a Null Vector.");
}
return Vector2D(a1 / length, a2 / length, a3 / length);
}
Or altering the object itself?:
void Vector2D::Normalize() {
double a1 = GetX();
double a2 = GetY();
double a3 = GetZ();
double length = GetLength();
assert(Math::IsEqual(length, 0.0) == false);
if(Math::IsEqual(length, 0.0)) {
throw Exception("Can not normalize a Null Vector.");
}
SetTerminal(a1 / length, a2 / length, a3 / length);
}
P.S.
IsEqual verifies the following: (std::fabs(a - b) <= 0.0001)