Created
November 17, 2012 19:59
-
-
Save learning2learn/4099484 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
#include <sstream> | |
#include <vector> | |
#include <math.h> | |
#include <iomanip> | |
#include <list> | |
#include <set> | |
using namespace std; | |
class Point2D{ | |
protected: | |
int x,y; | |
double distFrOrigin; | |
void setDistFrOrigin(); | |
public: | |
Point2D(){}; | |
Point2D(int x, int y); | |
int getX(); | |
int getY(); | |
double getScalarValue(); | |
void setX(int x); | |
void setY(int y); | |
virtual string toString(); | |
Point2D operator - (Point2D); | |
bool operator == (Point2D); | |
bool operator < (Point2D); | |
}; | |
Point2D::Point2D(int x, int y){ | |
setX(x); | |
setY(y); | |
setDistFrOrigin(); | |
} | |
void Point2D::setX(int x) { | |
this->x = x; | |
} | |
void Point2D::setY(int y) { | |
this->y = y; | |
} | |
void Point2D::setDistFrOrigin() { | |
double compute=0.000f; | |
compute = sqrt(pow(x,2) + pow(y,2)); | |
distFrOrigin = compute; | |
} | |
int Point2D::getX() { | |
return x; | |
} | |
int Point2D::getY() { | |
return y; | |
} | |
double Point2D::getScalarValue() { | |
setDistFrOrigin(); | |
return distFrOrigin; | |
} | |
string Point2D::toString(){ | |
stringstream ss; | |
ss << "["; | |
ss.width(4); | |
ss << right << x; | |
ss << ", "; | |
ss.width(4); | |
ss << right << y; | |
ss << "]\t"; | |
//ss.precision(3) << fixed; | |
ss << setprecision(3) << fixed << right << distFrOrigin <<endl; | |
return ss.str(); | |
} | |
Point2D Point2D::operator- (Point2D param) { | |
Point2D temp(getX() - param.getX(), getY() - param.getY()); | |
return (temp); | |
} | |
bool Point2D::operator== (Point2D param) { | |
return ( (getX() == param.getX()) && (getY() == param.getY()) ); | |
} | |
bool Point2D::operator< (Point2D param) { | |
return (getScalarValue() < param.getScalarValue()); | |
} | |
struct SortByDist //this struct is used by the sort method from <algorithm> to sort the p2dVect vector | |
{ | |
bool operator() (Point2D a, Point2D b) { | |
return a.getScalarValue() > b.getScalarValue(); | |
} | |
}; | |
int main (){ | |
list <Point2D> PointList; | |
PointList.push_back( Point2D(-9, -9)); | |
PointList.push_back( Point2D(-99, -99)); | |
PointList.push_back( Point2D(-999, -999)); | |
PointList.push_back( Point2D(-999, -999)); | |
PointList.push_back( Point2D(-999, -999)); | |
PointList.push_back( Point2D(-99, -99)); | |
PointList.push_back( Point2D(-9, -9)); | |
PointList.push_back( Point2D(3, 3)); | |
PointList.push_back( Point2D(23, 23)); | |
PointList.push_back( Point2D(123, 123)); | |
cout << "\n X Y Dist. Fr Origin" << endl; | |
cout << "- - - - - - - - - - - - - - - - - - -"<< endl; | |
list<Point2D>::iterator p = PointList.begin(); | |
while (p != PointList.end()){ | |
cout << p->toString(); | |
p++; | |
} | |
PointList.sort(SortByDist()); | |
cout << "\n X Y Dist. Fr Origin" << endl; | |
cout << "- - - - - - - - - - - - - - - - - - -"<< endl; | |
p = PointList.begin(); | |
while (p != PointList.end()){ | |
cout << p->toString(); | |
p++; | |
} | |
PointList.unique(); | |
cout << "\n X Y Dist. Fr Origin" << endl; | |
cout << "- - - - - - - - - - - - - - - - - - -"<< endl; | |
p = PointList.begin(); | |
while (p != PointList.end()){ | |
cout << p->toString(); | |
p++; | |
} | |
system("pause"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment