Advertisement
Guest User

Untitled

a guest
May 23rd, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include <windows.h>
  5. #define QUADRADO 10
  6. #define robo_x 30
  7. #define robo_y 30
  8.  
  9. void pensar(float x, float y, float angulo, float distancia_sensor);
  10. void ocupados(int cateto_o, int cateto_a);
  11. void livre(int x0, int y0, int x1, int y1);
  12.  
  13. // cada quadrado == 10 cm
  14. int mapa[60][60] = {0}; // 6x6 metros
  15.  
  16. int main(void){
  17. pensar(robo_x, robo_y, 120.2,10.34);
  18. return 0;
  19. }
  20.  
  21. void pensar(float x, float y, float alfa, float distancia_sensor){
  22.  
  23. int largura, altura;
  24. largura = (int) (x/QUADRADO); // casting of float for int
  25. altura = (int) (y/QUADRADO); // casting of float for int
  26.  
  27. int cateto_oposto, cateto_adjacente = 0;
  28. cateto_oposto = (int) (sin(alfa) * distancia_sensor)/QUADRADO; // altura == linhas
  29. cateto_adjacente = (int) (cos(alfa) * distancia_sensor)/QUADRADO; // largura == colunas
  30.  
  31. ocupados(cateto_oposto, cateto_adjacente);
  32. livre(robo_x, robo_y, cateto_adjacente, cateto_oposto);
  33.  
  34. }
  35.  
  36. void ocupados(int cateto_o, int cateto_a){
  37.  
  38. mapa[cateto_o][cateto_a]+=2;
  39.  
  40. if(mapa[cateto_o][cateto_a] > 10)
  41. mapa[cateto_o][cateto_a]-=1;
  42. }
  43.  
  44. // x0 e y0 == posição do robo
  45. // x1 e x2 == posição do objeto
  46. void livre(int x0, int y0, int x1, int y1){
  47.  
  48. int dx = abs(x1-x0), sx = x0<x1 ? 1 : -1;
  49. int dy = abs(y1-y0), sy = y0<y1 ? 1 : -1;
  50. int err = (dx>dy ? dx : -dy)/2, e2;
  51.  
  52. for(;;){
  53. mapa[y0][x0]-=1;
  54.  
  55. if(mapa[y0][x0] < -10)
  56. mapa[y0][x0] + 1;
  57.  
  58. if (x0==x1 && y0==y1) break;
  59. e2 = err;
  60. if (e2 >-dx) { err -= dy; x0 += sx; }
  61. if (e2 < dy) { err += dx; y0 += sy; }
  62. }
  63.  
  64.  
  65.  
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement