Vector::Vector(Vector tail, Vector head): x(head.x - tail.x), y(head.y - tail.y), z(head.z - tail.z)
{
}
Vector::Vector(Vector tail, Vector head): x(head.x - tail.x), y(head.y - tail.y), z(head.z - tail.z)
{
}
// Create vector from angle + length (2D; z is set to zero)
void Vector::SetAngleAndLength(double angle, double length)
// Create vector from angle + length (2D; z is set to zero)
void Vector::SetAngleAndLength(double angle, double length)
Vector Vector::operator=(Vector const v)
{
x = v.x, y = v.y, z = v.z;
Vector Vector::operator=(Vector const v)
{
x = v.x, y = v.y, z = v.z;
Vector Vector::operator+(Vector const v)
{
return Vector(x + v.x, y + v.y, z + v.z);
}
Vector Vector::operator+(Vector const v)
{
return Vector(x + v.x, y + v.y, z + v.z);
}
Vector Vector::operator-(Vector const v)
{
return Vector(x - v.x, y - v.y, z - v.z);
}
Vector Vector::operator-(Vector const v)
{
return Vector(x - v.x, y - v.y, z - v.z);
}
// Vector x constant, self assigned
Vector& Vector::operator*=(double const v)
// Vector x constant, self assigned
Vector& Vector::operator*=(double const v)
// Vector / constant, self assigned
Vector& Vector::operator/=(double const v)
// Vector / constant, self assigned
Vector& Vector::operator/=(double const v)
// Vector + constant, self assigned
Vector& Vector::operator+=(double const v)
// Vector + constant, self assigned
Vector& Vector::operator+=(double const v)
// Vector - vector, self assigned
Vector& Vector::operator-=(Vector const v)
// Vector - vector, self assigned
Vector& Vector::operator-=(Vector const v)
// Vector - constant, self assigned
Vector& Vector::operator-=(double const v)
// Vector - constant, self assigned
Vector& Vector::operator-=(double const v)
- // acos returns a value between zero and PI, which means we don't know which
- // quadrant the angle is in... Though, if the y-coordinate of the vector is
- // negative, that means that the angle is in quadrants III - IV.
+ // acos returns a value between zero and TAU/2, which means we don't know
+ // which quadrant the angle is in... However, if the y-coordinate of the
+ // vector is negative, that means that the angle is in quadrants III - IV.
bool Vector::isZero(double epsilon/*= 1e-6*/)
{
return (fabs(x) < epsilon && fabs(y) < epsilon && fabs(z) < epsilon ? true : false);
}
bool Vector::isZero(double epsilon/*= 1e-6*/)
{
return (fabs(x) < epsilon && fabs(y) < epsilon && fabs(z) < epsilon ? true : false);
}
// Returns the parameter of a point in space to this vector. If the parameter
// is between 0 and 1, the normal of the vector to the point is on the vector.
// Note: v1 is the tail, v2 is the head of the line (vector).
// Returns the parameter of a point in space to this vector. If the parameter
// is between 0 and 1, the normal of the vector to the point is on the vector.
// Note: v1 is the tail, v2 is the head of the line (vector).
double magnitude = lineSegment.Magnitude();
Vector pointSegment = p - tail;
double t = lineSegment.Dot(pointSegment) / (magnitude * magnitude);
double magnitude = lineSegment.Magnitude();
Vector pointSegment = p - tail;
double t = lineSegment.Dot(pointSegment) / (magnitude * magnitude);
// Return the 2D normal to the linesegment formed by the passed in points.
// The normal thus calculated should rotate anti-clockwise.
/*static*/ Vector Vector::Normal(Vector tail, Vector head)
{
Vector v = (head - tail).Unit();
// Return the 2D normal to the linesegment formed by the passed in points.
// The normal thus calculated should rotate anti-clockwise.
/*static*/ Vector Vector::Normal(Vector tail, Vector head)
{
Vector v = (head - tail).Unit();
/*static*/ double Vector::AngleBetween(Vector a, Vector b)
{
// This is done using the following formula:
// (a . b) = ||a|| ||b|| cos(theta)
// However, have to check for two degenerate cases, where a = cb:
/*static*/ double Vector::AngleBetween(Vector a, Vector b)
{
// This is done using the following formula:
// (a . b) = ||a|| ||b|| cos(theta)
// However, have to check for two degenerate cases, where a = cb:
- // 1, if c > 0, theta = 0; 2, if c < 0, theta = 180°.
+ // 1) if c > 0, theta = 0; 2) if c < 0, theta = 180°.
// Also, the vectors a & b have to be non-zero.
// Also, have to check using an epsilon because acos will not return an
// Also, the vectors a & b have to be non-zero.
// Also, have to check using an epsilon because acos will not return an