daily pastebin goal
37%
SHARE
TWEET

Untitled

a guest Oct 20th, 2018 76 Never
Upgrade to PRO!
ENDING IN00days00hours00mins00secs
  1. #include <iostream>
  2. #include <vector>
  3. #include <queue>
  4.  
  5. #include "Image.h"
  6.  
  7. using namespace std;
  8.  
  9. enum Direction {
  10.     UP,
  11.     RIGHT,
  12.     DOWN,
  13.     LEFT
  14. };
  15.  
  16. struct Point {
  17.     int x;
  18.     int y;
  19.  
  20.     Point(){}
  21.     Point(int _x, int _y) : x(_x), y(_y) {}
  22.  
  23.     bool operator==(const Point& point){
  24.         return point.x == x && point.y == y;
  25.     }
  26.     bool operator!=(const Point& point){
  27.         return !operator==(point);
  28.     }
  29. };
  30.  
  31. ostream& operator<<(ostream& os, const Point& point){
  32.     return os << "(" << point.x << ", " << point.y << ")";
  33. }
  34.  
  35. bool isBuilding(const Image& image, Point position){
  36.     return position.x >= 0 && position.y >= 0
  37.         && position.x < image.getX() && position.y < image.getY()
  38.         && image.get(position.x, position.y) == sf::Color::White;
  39. }
  40.  
  41. vector<Point> getPath(const Image& image, Point initialPosition, Direction initialDirection){
  42.     vector<Point> path;
  43.  
  44.     path.push_back(initialPosition);
  45.  
  46.     Direction currentDirection = initialDirection;
  47.     Point currentPosition = initialPosition;
  48.  
  49.     do{
  50.         switch(currentDirection){
  51.         case UP:
  52.             if(isBuilding(image, Point{currentPosition.x, currentPosition.y - 1})){
  53.                 currentPosition.y -= 1;
  54.                 currentDirection = LEFT;
  55.             }else{
  56.                 currentDirection = RIGHT;
  57.             }
  58.             break;
  59.         case RIGHT:
  60.             if(isBuilding(image, Point{currentPosition.x + 1, currentPosition.y})){
  61.                 currentPosition.x += 1;
  62.                 currentDirection = UP;
  63.             }else{
  64.                 currentDirection = DOWN;
  65.             }
  66.             break;
  67.         case DOWN:
  68.             if(isBuilding(image, Point{currentPosition.x, currentPosition.y + 1})){
  69.                 currentPosition.y += 1;
  70.                 currentDirection = RIGHT;
  71.             }else{
  72.                 currentDirection = LEFT;
  73.             }
  74.             break;
  75.         case LEFT:
  76.             if(isBuilding(image, Point{currentPosition.x - 1, currentPosition.y})){
  77.                 currentPosition.x -= 1;
  78.                 currentDirection = DOWN;
  79.             }else{
  80.                 currentDirection = UP;
  81.             }
  82.             break;
  83.         }
  84.  
  85.         if(path.back() != currentPosition){
  86.             path.push_back(currentPosition);
  87.         }
  88.     }while(currentPosition != initialPosition || currentDirection != initialDirection);
  89.  
  90.     if(path.size() > 1){
  91.         path.pop_back();
  92.     }
  93.  
  94.     return path;
  95. }
  96.  
  97. void clearBuilding(Image& image, Point position){
  98.     queue<Point> toProcess;
  99.  
  100.     toProcess.push(position);
  101.  
  102.     do{
  103.         Point point = toProcess.front();
  104.         toProcess.pop();
  105.  
  106.         if(isBuilding(image, point)){
  107.             image.set(point.x, point.y, sf::Color::Red);
  108.  
  109.             toProcess.push(Point{point.x+1, point.y});
  110.             toProcess.push(Point{point.x-1, point.y});
  111.             toProcess.push(Point{point.x, point.y+1});
  112.             toProcess.push(Point{point.x, point.y-1});
  113.         }
  114.     }while(toProcess.size() > 0);
  115. }
  116.  
  117. vector<Point> getBuilding(Image& image, Point position, Direction direction){
  118.     vector<Point> path = getPath(image, position, direction);
  119.  
  120.     clearBuilding(image, position);
  121.  
  122.     return path;
  123. }
  124.  
  125. int main(int argc, char** argv){
  126.     if(argc != 2){
  127.         cout << "First argument: file path" << endl;
  128.  
  129.         return -1;
  130.     }
  131.  
  132.     Image image;
  133.  
  134.     cout << "Loading file: " << argv[1] << endl;
  135.     image.loadFromBMP(argv[1]);
  136.  
  137.     if(!image.isValid()){
  138.         cout << "ERROR: Couldn't load image" << endl;
  139.  
  140.         return 1;
  141.     }
  142.  
  143.     cout << "Processing buildings..." << endl;
  144.     for(int i=0; i<image.getX(); i++){
  145.         for(int j=0; j<image.getY(); j++){
  146.             if(image.get(i,j) == sf::Color::White){
  147.                 vector<Point> building = getBuilding(image, Point{i, j}, UP);
  148.  
  149.                 cout << "BUILDING FOUND:" << endl;
  150.                 for(Point point : building){
  151.                     cout << point << endl;
  152.                 }
  153.                 cout << endl;
  154.             }
  155.         }
  156.     }
  157. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top