home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Frozen Fish 1: Amiga
/
FrozenFish-Apr94.iso
/
bbs
/
oct93
/
graphics
/
graphtal.lha
/
Graphtal
/
Vector.C
< prev
next >
Wrap
C/C++ Source or Header
|
1992-11-19
|
4KB
|
191 lines
/*
* Vector.C - methods for class Vector.
*
* Copyright (C) 1992, Christoph Streit (streit@iam.unibe.ch)
* University of Berne, Switzerland
* All rights reserved.
*
* This software may be freely copied, modified, and redistributed
* provided that this copyright notice is preserved on all copies.
*
* You may not distribute this software, in whole or in part, as part of
* any commercial product without the express consent of the authors.
*
* There is no warranty or other guarantee of fitness of this software
* for any purpose. It is provided solely "as is".
*
*/
#include "Vector.h"
//___________________________________________________________ Vector
Vector::Vector()
{ v[0] = v[1] = v[2] = 0.0; }
Vector::Vector(const real x1, const real x2, const real x3)
{ v[0] = x1; v[1] = x2; v[2] = x3; }
Vector::Vector(const Vector& vec)
{ v[0] = vec.v[0]; v[1] = vec.v[1]; v[2] = vec.v[2]; }
const Vector& Vector::operator=(const Vector& vec)
{
v[0] = vec.v[0]; v[1] = vec.v[1]; v[2] = vec.v[2];
return *this;
}
Vector& Vector::operator+=(const Vector& vec)
{
v[0] += vec[0]; v[1] += vec[1]; v[2] += vec[2];
return *this;
}
Vector& Vector::operator-=(const Vector& vec)
{
v[0] -= vec[0]; v[1] -= vec[1]; v[2] -= vec[2];
return *this;
}
Vector& Vector::operator*=(const Vector& vec)
{
static real tmp[3];
tmp[0] = v[0]; tmp[1] = v[1]; tmp[2] = v[2];
v[0] = tmp[1]*vec[2] - tmp[2]*vec[1];
v[1] = tmp[2]*vec[0] - tmp[0]*vec[2];
v[2] = tmp[0]*vec[1] - tmp[1]*vec[0];
return *this;
}
Vector& Vector::operator*=(const real x)
{
v[0] *= x; v[1] *= x; v[2] *= x;
return *this;
}
Vector& Vector::operator/=(const real x)
{
if (equal(x, 0))
Error(ERR_PANIC, "Vector::operator/= division by zero");
real d = 1/x;
v[0] *= d; v[1] *= d; v[2] *= d;
return *this;
}
int Vector::operator==(const Vector& vec) const
{
return (equal(v[0], vec[0]) && equal(v[1], vec[1]) && equal(v[2], vec[2]));
}
int Vector::operator!=(const Vector& vec) const
{
return !(*this == vec);
}
real Vector::sqr() const
{
return v[0]*v[0] + v[1]*v[1] + v[2]*v[2];
}
real Vector::length() const
{
return sqrt(v[0]*v[0] + v[1]*v[1] + v[2]*v[2]);
}
int Vector::zero() const
{
return (equal(fabs(v[0]), 0) &&
equal(fabs(v[1]), 0) &&
equal(fabs(v[2]), 0));
}
real Vector::distance(const Vector& vec) const
{
return (*this - vec).length();
}
real Vector::normalize()
{
real len = length();
if (equal(len, 0))
return 0;
else {
real d = 1/len;
v[0] *= d; v[1] *= d; v[2] *= d;
return len;
}
}
Vector Vector::normalized() const
{
real len = length();
if (equal(len, 0)) {
Error(ERR_WARN, "Vector::normalized length is zero");
return Vector(0,0,0);
}
else
return Vector(*this / len);
}
Vector Vector::operator-() const
{
return Vector(-v[0], -v[1], -v[2]);
}
Vector Vector::operator+(const Vector& vec) const
{
return Vector(v[0] + vec[0], v[1] + vec[1], v[2] + vec[2]);
}
Vector Vector::operator-(const Vector& vec) const
{
return Vector(v[0] - vec[0], v[1] - vec[1], v[2] - vec[2]);
}
Vector Vector::operator*(const Vector& vec) const
{
return Vector(v[1]*vec[2] - v[2]*vec[1],
v[2]*vec[0] - v[0]*vec[2],
v[0]*vec[1] - v[1]*vec[0]);
}
real Vector::operator^(const Vector& vec) const
{
return (v[0]*vec[0] + v[1]*vec[1] + v[2]*vec[2]);
}
Vector Vector::operator/(const real x) const
{
if (equal(x, 0))
Error(ERR_PANIC, "Vector::operator/ division by zero");
real d = 1/x;
return Vector(v[0]*d, v[1]*d, v[2]*d);
}
Vector operator*(real x, const Vector& vec)
{
return Vector(x*vec[0], x*vec[1], x*vec[2]);
}
Vector operator*(const Vector& vec, real x)
{
return Vector(vec[0]*x, vec[1]*x, vec[2]*x);
}
ostream& operator<<(ostream& os, const Vector& vec)
{
return os << "("
<< vec[0] << ", " << vec[1] << ", " << vec[2]
<< ")";
}
void Vector::swap(Vector& v1, Vector& v2)
{
Vector tmp = v1; v1 = v2; v2 = tmp;
}