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
|
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
* File: main.cpp
* Author: cmisip
*
* Created on June 6, 2017, 10:46 PM
*/
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
using namespace std;
struct Point {
int x;
int y;
Point(int ix, int iy): x(ix), y(iy){};
bool operator<(const Point &other) const{
return ( (this->y+5) < other.y) || ( (this->y-5) > other.y) && ( (this->x+5) < other.x) || ( (this->x-5) > other.x) ;
}
};
bool myfunction(Point a, Point b){
//sort according to manhattan distance
Point p(6,7);
return (( abs(a.x -p.x) + abs(a.y-p.y) ) < (abs(b.x -p.x) + abs(b.y-p.y)));
}
int main(int argc, char** argv) {
//Find out all values that are within 5 pixels of Point 6,7
//Range would be for the y 7-5=2 to 7+5=12
//Range would be for the x 6-5=3 to 6+5=11
//Answer: {11,3}, {7,3}, {8,5}, {3,9} //x from 3 to 11, and y from 2 to 12
Point pt( 6, 7 );
//First permutation
std::cout << "-------------------------------------------------------" << std::endl;
std::vector<Point> v = {{11,3}, {7,3}, {8,5}, {3,9}, {3,25}, {3,14}, {25,3}};
std::cout << "ORIGINAL" << std::endl;
for_each(v.begin(), v.end(), [](Point n){std::cout << n.x << "," << n.y << " "; });
sort(v.begin(), v.end(), [pt](Point a, Point b){return (( abs(a.x -pt.x) + abs(a.y-pt.y) ) < (abs(b.x -pt.x) + abs(b.y-pt.y))); }); //manhattan distance lambda
std::cout << std::endl<< "SORTED" << std::endl;
for_each(v.begin(), v.end(), [](Point n){std::cout << n.x << "," << n.y << " "; });
std::cout << std::endl;
auto p = std::equal_range(v.begin(),v.end(),pt);
std::cout << "Equal range" << std::endl;
for ( auto i = p.first; i != p.second; ++i )
std::cout << i->x << ',' << i->y << " " << std::endl;
//Second permutation
std::cout << "-------------------------------------------------------" << std::endl;
std::vector<Point> w = {{3,25}, {3,14}, {25,3}, {11,3}, {7,3}, {8,5}, {3,9} };
std::cout << "ORIGINAL" << std::endl;
for_each(w.begin(), w.end(), [](Point n){std::cout << n.x << "," << n.y << " "; });
sort(w.begin(), w.end(),myfunction); //manhattan distance in a function
std::cout << std::endl<< "SORTED" << std::endl;
for_each(w.begin(), w.end(), [](Point n){std::cout << n.x << "," << n.y << " "; });
std::cout << std::endl;
p = std::equal_range(w.begin(),w.end(),pt);
std::cout << "Equal range" << std::endl;
for ( auto i = p.first; i != p.second; ++i )
std::cout << i->x << ',' << i->y << " " << std::endl;
//Third permutation
std::cout << "-------------------------------------------------------" << std::endl;
std::vector<Point> x = {{25,3}, {11,3}, {7,3}, {3,25}, {3,14}, {8,5}, {3,9} };
auto it = std::partition(x.begin(), x.end(), [pt](Point i){ return ( (abs(pt.x - i.x) + abs(pt.y -i.y)) < 10 ); }); // predicate that prioritizes a manhattan distance of < 10
std::cout << "ORIGINAL" << std::endl;
for_each(x.begin(), x.end(), [](Point n){std::cout << n.x << "," << n.y << " "; });
std::cout << "\nPartitioned vector:\n ";
for_each(x.begin(), it, [](Point n){std::cout << n.x << "," << n.y << " "; });
std::cout << std::endl;
//Fourth permutation
std::cout << "-------------------------------------------------------" << std::endl;
std::vector<Point> z = {{3,14}, {8,5}, {25,3}, {11,3}, {7,3}, {3,25}, {3,9} };
it = std::partition(z.begin(), z.end(), [pt](Point i){ return ( (abs(pt.x - i.x) + abs(pt.y -i.y)) < 10 ); }); // just to see if I still get the same results
std::cout << "ORIGINAL" << std::endl;
for_each(z.begin(), z.end(), [](Point n){std::cout << n.x << "," << n.y << " "; });
std::cout << "\nPartitioned vector:\n ";
for_each(z.begin(), it, [](Point n){std::cout << n.x << "," << n.y << " "; });
std::cout << std::endl;
return 0;
}
|