Go to Overview over all GrAL packages.
Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

src/Geometry/point.C

Go to the documentation of this file.
00001 
00002 #include <math.h>
00003 #include "Geometry/point.h"
00004 
00005 
00006 
00007 // CONSTRUCTORS
00008 
00009 point::point(const double x1): n(1) {x=new double[n]; x[0]=x1;}
00010 
00011 point::point(const double x1, const double y1) : n(2) 
00012 {x=new double[n]; x[0]=x1;x[1]=y1;}
00013 
00014 point::point(const double x1, const double y1, const double z1) : n(3)
00015 {x=new double[n]; x[0]=x1;x[1]=y1;x[2]=z1;}
00016 
00017 point::point(const point& v) : n(v.n)
00018 {
00019   x=new double[n]; 
00020   for(int i=0;i<=n-1;i++) 
00021     x[i]=v.x[i]; 
00022 }
00023 
00024 
00025 point::point(const double  a[], int nn) : n(nn)
00026 {
00027   if (n > 0) {
00028   x=new double[n]; 
00029   for(int i=0;i<=n-1;i++) 
00030     x[i]=a[i];
00031   } 
00032 }
00033 
00034 // ASSIGNMENT OPERATORS
00035 
00036 point& point::operator = (const point& v)
00037 {
00038   n = v.n;
00039   delete[] x; 
00040   x=new double[n]; 
00041   for(int i=0;i<=n-1;i++) 
00042     x[i]=v.x[i];
00043   return *this; 
00044 }
00045  
00046 
00047 point& point::operator += ( const point& v )
00048 { 
00049   for (int i= 0;i<=n-1;i++)
00050     x[i] += v.x[i];
00051   return *this;
00052 }
00053 
00054 point& point::operator -= ( const point& v )
00055 { 
00056   for (int i= 0;i<=n-1;i++)
00057     x[i] -= v.x[i];
00058   return *this;
00059 }
00060 
00061 
00062 point& point::operator *= ( const double d )
00063 { 
00064   for (int i= 0;i<=n-1;i++)
00065     x[i] *= d;
00066   return *this;
00067 }
00068 
00069 
00070 point& point::operator /= ( const double d )
00071 { 
00072   for (int i= 0;i<=n-1;i++)
00073     x[i] /= d;
00074   return *this;
00075 }
00076 
00077 
00078 // SPECIAL FUNCTIONS
00079 
00080 double point::length() const
00081 { return sqrt(length2()); }
00082 
00083 double point::length2() const
00084 { 
00085   double sum = 0;
00086   for(int i=0;i<=n-1;i++)
00087     sum += x[i]*x[i];
00088   return sum;
00089 }
00090 
00091 point  point::normalize() const
00092 { 
00093  double l = length();
00094  REQUIRE((l!=0.0),"point::normalize: point is zero!\n",1);
00095 
00096  point p(*this);
00097  p /= l; 
00098  return p; 
00099 }
00100 
00101 // FRIENDS
00102 
00103 point operator - (const point& a)
00104 { 
00105  point p(a.n,point::no_init);
00106  for(int i=1;i<=a.n;i++)
00107    p[i] = -a[i];
00108  return p;
00109 }
00110 
00111 point operator + (const point& a, const point& b)
00112 { 
00113   REQUIRE((a.n == b.n),
00114           "point a+b: dim a == " << a.n << " != dim b == " << b.n << "!",1);
00115  point p(a.n,point::no_init);
00116  for(int i=1;i<=a.n;i++)
00117    p[i] = a[i]+b[i];
00118  return p;
00119 }
00120 
00121 point operator - (const point& a, const point& b)
00122 { 
00123   REQUIRE((a.n == b.n),
00124           "point a-b: dim a == " << a.n << " != dim b == " << b.n << "!",1);
00125  point p(a.n,point::no_init);
00126  for(int i=1;i<=a.n;i++)
00127    p[i]=a[i]-b[i];
00128  return p;
00129 }
00130 
00131 //point operator , (const point& a, const point& b)
00132 point combine(const point& a, const point& b)
00133 { 
00134   point p(a.n+b.n,point::no_init);
00135  for(int i=1;i<=a.n;i++)
00136    p[i]=a[i];
00137  for(int k=1;k<=b.n;k++)
00138    p[k+a.n]=b[k];
00139  return p;
00140 }
00141 
00142 point operator * (const point& a, const double d)
00143 { 
00144  point p(a.n,point::no_init);
00145  for(int i=1;i<=a.n;i++)
00146    p[i]=d*a[i];
00147  return p;
00148 }
00149 
00150 point operator * (const double d, const point& a)
00151 { return a*d; }
00152 
00153 point operator / (const point& a, const double d)
00154 { return(a*(1.0/d));}
00155 
00156 
00157 double operator * (const point& a, const point& b)
00158 {
00159   REQUIRE((a.n == b.n),
00160           "point a*b: dim a == " << a.n << " != dim b == " << b.n << "!",1);
00161   double sum = 0;
00162   for(int i=1;i<=a.n;i++)
00163     sum += a[i]*b[i];
00164   return sum;
00165 }
00166 
00167 ostream& operator << (ostream& s, const point& v)
00168 { 
00169   for(int i=1;i<=v.n;i++)
00170     s << v[i] << " ";
00171   return s;
00172 }
00173 
00174 istream& operator >> (istream& s, point& v) {
00175   for(int i =1;i<=v.n;i++)
00176     s >> v[i];
00177   return s;
00178 }
00179 
00180 void swap(point& a, point& b)
00181 { point tmp(a); a = b; b = tmp; }
00182 
00183 

Copyright (c) Guntram Berti 1997-2002. See the GrAL Homepage for up-to-date information.

Generated at Tue Feb 26 15:57:27 2002 for Geometry by doxygen 1.2.11-20011104 written by Dimitri van Heesch, © 1997-2000