Advertisement
Guest User

Untitled

a guest
Apr 29th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.75 KB | None | 0 0
  1. #include "graphics.hpp"
  2. #include <vector>
  3. #include <fstream>
  4. #include <iostream>
  5. #include <cstdlib>
  6.  
  7. using namespace std;
  8. using namespace genv;
  9.  
  10. typedef vector<vector<int> > mat_t;
  11.  
  12. int XX;
  13. int YY;
  14.  
  15. void beolvas(mat_t &v, string fnev="terkep.txt")
  16. {
  17. ifstream fin(fnev);
  18. if(!fin.good()){
  19. cerr << fnev << " nem talalhato\n";
  20. exit(1);
  21. }
  22. fin >> XX >> YY;
  23. v.resize(XX);
  24. for(size_t i = 0; i < XX; ++i)
  25. {
  26. v[i].resize(YY);
  27. for(size_t j = 0; j < YY; ++j)
  28. {
  29. fin >> v[i][j];
  30. }
  31. }
  32. }
  33.  
  34. void kirajzol(const mat_t &v, canvas& c)
  35. {
  36. for(size_t i = 0; i < v.size(); ++i)
  37. {
  38. for(size_t j = 0; j < v[i].size(); ++j)
  39. {
  40. int g,b;
  41. int norm_h = 127+v[i][j];
  42. g = norm_h;
  43. if(v[i][j] < 0) b = 255+v[i][j];
  44. else b = 0;
  45. c << move_to(i,j) << color(0,g,b) << dot;
  46. }
  47. }
  48. c << refresh;
  49. }
  50.  
  51. void tr(canvas& out, canvas& background){
  52. out<<stamp(background,0,0);
  53. }
  54.  
  55. struct koord{
  56. int x,y;
  57. koord():x(0),y(0){}
  58. koord(int _x, int _y):x(_x), y(_y){}
  59. };
  60.  
  61. void szigetbejaras(mat_t &terkep, int startx, int starty, int celx, int cely){ /// & többinél kell ?
  62. vector<vector<koord>> honnan(XX, vector<koord> (YY,koord(-1,-1))); ///létrehoz egy térkép méretű (-1,-1) koordinátákból álló vektort
  63. mat_t latogatottak(XX,vector<int>(YY,0));
  64. vector<koord> sor(1,koord(startx,starty));
  65.  
  66. while(!sor.empty()) {
  67. koord aktualis=sor.front();
  68. int x=aktualis.x, y=aktualis.y;
  69. sor.erase(sor.begin());
  70.  
  71. if(x>=0 && x<XX && y>=0 && y<YY && terkep[x][y]>0) {
  72. latogatottak[x][y]=1;
  73.  
  74. if(x-1>0 && x-1<XX && terkep[x-1][y]>0 && latogatottak[x-1][y]==0) {
  75. sor.push_back(koord(x-1,y));
  76. latogatottak[x-1][y]=1;
  77. honnan[x-1][y]=koord(x,y);
  78. }
  79. if(x+1>0 && x+1<XX && terkep[x+1][y]>0 && latogatottak[x+1][y]==0) {
  80. sor.push_back(koord(x+1,y));
  81. latogatottak[x+1][y]=1;
  82. honnan[x+1][y]=koord(x,y);
  83. }
  84. if(y-1>0 && y-1<YY && terkep[x][y-1]>0 && latogatottak[x][y-1]==0) {
  85. sor.push_back(koord(x,y-1));
  86. latogatottak[x][y-1]=1;
  87. honnan[x][y-1]=koord(x,y);
  88. }
  89. if(y+1>0 && y+1<YY && terkep[x][y+1]>0 && latogatottak[x][y+1]==0) {
  90. sor.push_back(koord(x,y+1));
  91. latogatottak[x][y+1]=1;
  92. honnan[x][y+1]=koord(x,y);
  93. }
  94. }
  95. }
  96. int X=celx,Y=cely;
  97. while(!(X==startx && Y==starty) || sor.size()==0) {
  98. gout << move_to(X,Y) << color(255,0,0) << dot << refresh;
  99. X=honnan[X][Y].x;
  100. Y=honnan[X][Y].y;
  101. }
  102. }
  103.  
  104. int main() {
  105.  
  106. mat_t terkep;
  107. beolvas(terkep);
  108.  
  109. gout.open(XX, YY);
  110. canvas hatter(XX,YY);
  111. kirajzol(terkep,hatter);
  112. tr(gout, hatter);
  113. gout << refresh;
  114.  
  115. event ev;
  116. int startx=-1;
  117. int starty=-1;
  118. int celx=-1;
  119. int cely=-1;
  120. while (genv::gin >> ev && ev.keycode != key_escape) {
  121. if(ev.type == ev_mouse && ev.button == btn_left && terkep[ev.pos_x][ev.pos_y]>0) {
  122. gout << move_to(ev.pos_x,ev.pos_y) << color(255,0,0) << dot <<refresh;
  123. startx=ev.pos_x;
  124. starty=ev.pos_y;
  125. }
  126. if(startx!=-1 && ev.type == ev_mouse && ev.button == btn_right && terkep[ev.pos_x][ev.pos_y]>0){
  127. gout << move_to(ev.pos_x,ev.pos_y) << color(255,0,0) << dot <<refresh;
  128. celx=ev.pos_x;
  129. cely=ev.pos_y;
  130. szigetbejaras(terkep, startx, starty, celx, cely);
  131. gout << refresh;
  132. }
  133. }
  134. cout << celx << ',' << cely << endl;
  135. return 0;
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement