Advertisement
Guest User

Untitled

a guest
Dec 12th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.86 KB | None | 0 0
  1. void bug(Mat image) {
  2.     Mat tmp(image.rows, image.cols, CV_8UC1, Scalar(255));
  3.     int startx = 0;
  4.     int starty = 0;
  5.     int posx;
  6.     int posy;
  7.  
  8.     while (image.at<uchar>(starty, startx) != 255) {
  9.         startx++;
  10.         if (startx > image.cols - 1) {
  11.             startx = 0;
  12.             starty++;
  13.         }
  14.     }
  15.     startx--;
  16.     tmp.at<uchar>(starty, startx) = 0;
  17. #define RIGTH 1
  18. #define LEFT -1
  19.     int dir = RIGTH;
  20.     posx = startx;
  21.     posy = starty;
  22.     int movx = 1;
  23.     int movy = 0;
  24.     do {
  25.  
  26.         posx += movx;
  27.         posy -= movy;
  28.         /*if (posx > image.cols - 1)
  29.         {
  30.             posx = 0;
  31.             posy++;
  32.         }
  33.         if (posx == 256 || posy == 256)
  34.         {
  35.             break;
  36.         }*/
  37.         tmp.at<uchar>(posy, posx) = 0;
  38.  
  39.         if (image.at<uchar>(posy, posx) == 255) {
  40.             dir = LEFT;
  41.             if (movx == 1 && movy == 0) {
  42.                 movx = 0;
  43.                 movy = 1;
  44.             } else if (movx == 0 && movy == 1) {
  45.                 movx = -1;
  46.                 movy = 0;
  47.             } else if (movx == -1 && movy == 0) {
  48.                 movx = 0;
  49.                 movy = -1;
  50.             } else if (movx == 0 && movy == -1) {
  51.                 movx = 1;
  52.                 movy = 0;
  53.             }
  54.  
  55.         } else {
  56.             dir = RIGTH;
  57.             if (movx == 1 && movy == 0) {
  58.                 movx = 0;
  59.                 movy = -1;
  60.             } else if (movx == 0 && movy == 1) {
  61.                 movx = 1;
  62.                 movy = 0;
  63.             } else if (movx == -1 && movy == 0) {
  64.                 movx = 0;
  65.                 movy = 1;
  66.             } else if (movx == 0 && movy == -1) {
  67.                 movx = -1;
  68.                 movy = 0;
  69.             }
  70.         }
  71.  
  72.     } while (!(posx == startx && posy == starty && dir == RIGTH));
  73.  
  74.     imshow("Bug", tmp);
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement