00001 /* 00002 * Copyright (C) 2007 A.N. Yzelman 00003 * Released under LGPL, see license.txt 00004 * 00005 * Last modified at 2th of May, 2007, by A.N. Yzelman 00006 * 00007 * Point.cpp: Implementation of the functions described in Point.h 00008 * 00009 */ 00010 00011 #include "Point.h" 00012 00013 Point::Point( int dimension ) { m_coordinates.resize( dimension ); } 00014 00015 Point::Point( vector<double> &toCopy ) { m_coordinates = toCopy; } 00016 00017 double Point::getCoordinate( const int index ) const { 00018 return m_coordinates[ index ]; 00019 } 00020 00021 void Point::setCoordinates( vector<double> v ) { m_coordinates = v; } 00022 00023 unsigned int Point::getDimension() const { return m_coordinates.size(); } 00024 00025 Point* Point::operator-(const Point other) const { 00026 //get dimensionality 00027 int len = m_coordinates.size(); 00028 00029 //vector used to store the differences in 00030 vector<double> v; 00031 00032 //calculate the differences 00033 for (int i=0; i<len; i++) { 00034 v.push_back( m_coordinates[i] - other.getCoordinate(i) ); 00035 } 00036 00037 //Create the new point 00038 Point *ret = new Point(len); 00039 00040 //set the coordinates 00041 ret->setCoordinates(v); 00042 00043 //return the point 00044 return ret; 00045 } 00046 00047 string Point::toString() const { 00048 ostringstream os; 00049 00050 os << " < "; 00051 for ( unsigned int i=0; i<m_coordinates.size(); i++ ) 00052 os << m_coordinates[i] << " "; 00053 os << ">"; 00054 return os.str(); 00055 } 00056 00057 //non-member functions 00058 00059 ostream& operator <<( ostream &os, const Point point ) { 00060 os << point.toString(); 00061 return os; 00062 } 00063