public
Last active

  • Download Gist
gistfile1.cpp
C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
#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;
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.