Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void bug(Mat image) {
- Mat tmp(image.rows, image.cols, CV_8UC1, Scalar(255));
- int startx = 0;
- int starty = 0;
- int posx;
- int posy;
- while (image.at<uchar>(starty, startx) != 255) {
- startx++;
- if (startx > image.cols - 1) {
- startx = 0;
- starty++;
- }
- }
- startx--;
- tmp.at<uchar>(starty, startx) = 0;
- #define RIGTH 1
- #define LEFT -1
- int dir = RIGTH;
- posx = startx;
- posy = starty;
- int movx = 1;
- int movy = 0;
- do {
- posx += movx;
- posy -= movy;
- /*if (posx > image.cols - 1)
- {
- posx = 0;
- posy++;
- }
- if (posx == 256 || posy == 256)
- {
- break;
- }*/
- tmp.at<uchar>(posy, posx) = 0;
- if (image.at<uchar>(posy, posx) == 255) {
- dir = LEFT;
- if (movx == 1 && movy == 0) {
- movx = 0;
- movy = 1;
- } else if (movx == 0 && movy == 1) {
- movx = -1;
- movy = 0;
- } else if (movx == -1 && movy == 0) {
- movx = 0;
- movy = -1;
- } else if (movx == 0 && movy == -1) {
- movx = 1;
- movy = 0;
- }
- } else {
- dir = RIGTH;
- if (movx == 1 && movy == 0) {
- movx = 0;
- movy = -1;
- } else if (movx == 0 && movy == 1) {
- movx = 1;
- movy = 0;
- } else if (movx == -1 && movy == 0) {
- movx = 0;
- movy = 1;
- } else if (movx == 0 && movy == -1) {
- movx = -1;
- movy = 0;
- }
- }
- } while (!(posx == startx && posy == starty && dir == RIGTH));
- imshow("Bug", tmp);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement