Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
#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
Something went wrong with that request. Please try again.