home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / oct93 / graphics / graphtal.lha / Graphtal / Vector.C < prev    next >
C/C++ Source or Header  |  1992-11-19  |  4KB  |  191 lines

  1. /*
  2.  * Vector.C - methods for class Vector.
  3.  *
  4.  * Copyright (C) 1992, Christoph Streit (streit@iam.unibe.ch)
  5.  *                     University of Berne, Switzerland
  6.  * All rights reserved.
  7.  *
  8.  * This software may be freely copied, modified, and redistributed
  9.  * provided that this copyright notice is preserved on all copies.
  10.  *
  11.  * You may not distribute this software, in whole or in part, as part of
  12.  * any commercial product without the express consent of the authors.
  13.  *
  14.  * There is no warranty or other guarantee of fitness of this software
  15.  * for any purpose.  It is provided solely "as is".
  16.  *
  17.  */
  18.  
  19. #include "Vector.h"
  20.  
  21. //___________________________________________________________ Vector
  22.  
  23. Vector::Vector()
  24. { v[0] = v[1] = v[2] = 0.0; }
  25.  
  26. Vector::Vector(const real x1, const real x2, const real x3) 
  27. { v[0] = x1; v[1] = x2; v[2] = x3; }
  28.  
  29. Vector::Vector(const Vector& vec)
  30. { v[0] = vec.v[0]; v[1] = vec.v[1]; v[2] = vec.v[2]; }
  31.  
  32. const Vector& Vector::operator=(const Vector& vec)
  33. {
  34.   v[0] = vec.v[0]; v[1] = vec.v[1]; v[2] = vec.v[2];
  35.   return *this;
  36. }
  37.  
  38. Vector& Vector::operator+=(const Vector& vec)
  39. {
  40.   v[0] += vec[0];  v[1] += vec[1];  v[2] += vec[2];
  41.   return *this;
  42. }
  43.  
  44. Vector& Vector::operator-=(const Vector& vec)
  45. {
  46.   v[0] -= vec[0];  v[1] -= vec[1];  v[2] -= vec[2];
  47.   return *this;
  48. }
  49.  
  50. Vector& Vector::operator*=(const Vector& vec)
  51. {
  52.   static real tmp[3];
  53.  
  54.   tmp[0] = v[0]; tmp[1] = v[1]; tmp[2] = v[2]; 
  55.  
  56.   v[0] = tmp[1]*vec[2] - tmp[2]*vec[1];
  57.   v[1] = tmp[2]*vec[0] - tmp[0]*vec[2];
  58.   v[2] = tmp[0]*vec[1] - tmp[1]*vec[0];
  59.  
  60.   return *this;
  61. }
  62.  
  63. Vector& Vector::operator*=(const real x)
  64. {
  65.   v[0] *= x; v[1] *= x; v[2] *= x;
  66.   return *this;
  67. }
  68.  
  69. Vector& Vector::operator/=(const real x)
  70. {
  71.   if (equal(x, 0))
  72.     Error(ERR_PANIC, "Vector::operator/= division by zero");
  73.  
  74.   real d = 1/x;
  75.   v[0] *= d; v[1] *= d; v[2] *= d;
  76.   return *this;
  77. }
  78.  
  79. int Vector::operator==(const Vector& vec) const
  80. {
  81.   return (equal(v[0], vec[0]) && equal(v[1], vec[1]) && equal(v[2], vec[2]));
  82. }
  83.  
  84. int Vector::operator!=(const Vector& vec) const
  85. {
  86.   return !(*this == vec);
  87. }
  88.  
  89. real Vector::sqr() const
  90. {
  91.   return v[0]*v[0] + v[1]*v[1] + v[2]*v[2];
  92. }
  93.  
  94. real Vector::length() const
  95. {
  96.   return sqrt(v[0]*v[0] + v[1]*v[1] + v[2]*v[2]);
  97. }
  98.  
  99. int Vector::zero() const
  100. {
  101.   return (equal(fabs(v[0]), 0) &&
  102.       equal(fabs(v[1]), 0) &&
  103.       equal(fabs(v[2]), 0));
  104. }
  105.  
  106. real Vector::distance(const Vector& vec) const
  107. {
  108.   return (*this - vec).length();
  109. }
  110.  
  111. real Vector::normalize()
  112. {
  113.   real len = length();
  114.   if (equal(len, 0))
  115.     return 0;
  116.   else {
  117.     real d = 1/len;
  118.     v[0] *= d; v[1] *= d; v[2] *= d;
  119.     return len;
  120.   }
  121. }
  122.  
  123. Vector Vector::normalized() const
  124. {
  125.   real len = length();
  126.   if (equal(len, 0)) {
  127.     Error(ERR_WARN, "Vector::normalized length is zero");
  128.     return Vector(0,0,0);
  129.   }
  130.   else
  131.     return Vector(*this / len);
  132. }
  133.  
  134. Vector Vector::operator-() const
  135. {
  136.   return Vector(-v[0], -v[1], -v[2]);
  137. }
  138.  
  139. Vector Vector::operator+(const Vector& vec) const
  140. {
  141.   return Vector(v[0] + vec[0], v[1] + vec[1], v[2] + vec[2]);
  142. }
  143.  
  144. Vector Vector::operator-(const Vector& vec) const
  145. {
  146.   return Vector(v[0] - vec[0], v[1] - vec[1], v[2] - vec[2]);
  147. }
  148.  
  149. Vector Vector::operator*(const Vector& vec) const
  150. {
  151.   return Vector(v[1]*vec[2] - v[2]*vec[1],
  152.         v[2]*vec[0] - v[0]*vec[2],
  153.         v[0]*vec[1] - v[1]*vec[0]);
  154. }
  155.  
  156. real Vector::operator^(const Vector& vec) const
  157. {
  158.   return (v[0]*vec[0] + v[1]*vec[1] + v[2]*vec[2]);
  159. }
  160.  
  161. Vector Vector::operator/(const real x) const
  162. {
  163.   if (equal(x, 0))
  164.     Error(ERR_PANIC, "Vector::operator/ division by zero");
  165.  
  166.   real d = 1/x;
  167.   return Vector(v[0]*d, v[1]*d, v[2]*d);
  168. }
  169.  
  170. Vector operator*(real x, const Vector& vec)
  171. {
  172.   return Vector(x*vec[0], x*vec[1], x*vec[2]);
  173. }
  174.  
  175. Vector operator*(const Vector& vec, real x) 
  176. {
  177.   return Vector(vec[0]*x, vec[1]*x, vec[2]*x);
  178. }
  179.  
  180. ostream& operator<<(ostream& os, const Vector& vec)
  181. {
  182.   return os << "(" 
  183.             << vec[0] << ", " << vec[1] << ", " << vec[2]
  184.         << ")";
  185. }
  186.  
  187. void Vector::swap(Vector& v1, Vector& v2)
  188. {
  189.   Vector tmp = v1; v1 = v2; v2 = tmp;
  190. }
  191.