Advertisement
IcaroPeretti

CircleColission

Jun 26th, 2021
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.09 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <GL/glut.h>
  4. #include <math.h>
  5. #define janela_altura 400
  6. #define janela_largura 600
  7. #define PI 3.1415926535898
  8. float trans = 110;
  9. void display(void);
  10. void tela(GLsizei w, GLsizei h);
  11. void keyboard(unsigned char tecla, int x, int y);
  12. int main(int argc, char** argv)
  13. {
  14. glutInit(&argc, argv); // suporte a janelas
  15. glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
  16. // PADRAO DE CORES
  17. glutInitWindowSize(janela_largura, janela_altura); // tamanho da janela
  18. glutInitWindowPosition(100, 100); // posicao que surge a janela
  19. glutCreateWindow("colisao_circulos"); // cria janela
  20. //glutFullScreen();
  21. glutKeyboardFunc(&keyboard); // chama teclado
  22. glutReshapeFunc(tela); // configura a tela
  23. glutDisplayFunc(display);
  24. glutMainLoop(); // redesenhar
  25. return(0);
  26. }
  27. void keyboard(unsigned char tecla, int x, int y)
  28. {
  29. printf("\ntecla %c\n", tecla);
  30. printf("\n\nDigite 1 para esquerda: ");
  31. printf("\ntecla %c\n", tecla);
  32. printf("\no mouse estava em %d x %d\n", x, y);
  33. if (tecla == '1')
  34. {
  35. trans = trans - 1;
  36. printf("\n o valor de translacao e %.2f\n", trans);
  37. printf("\n a distancia entre o raio deles e 110");
  38. printf("\n o raio de cada circulo e 50 (50+50=100)");
  39. printf("\n portanto....");
  40. printf("\n o segundo circulo esta a %.2f de distancia", (trans - 100));
  41. }
  42. if (tecla == '2')
  43. {
  44. trans = trans + 1;
  45. printf("\n o valor de translacao e %.2f\n", &trans);
  46. }
  47.  
  48. glutPostRedisplay();
  49. }
  50.  
  51. void desenhar()
  52. {
  53. glLineWidth(2); // grossura da linha
  54. GLfloat circ_pnt = 100;
  55. GLfloat ang, raioX, raioY;
  56. glTranslatef(-100, 0, 0);
  57. circ_pnt = 100;
  58. raioX = 50.0f;
  59. raioY = 50.0f;
  60. glBegin(GL_LINE_LOOP);
  61. for (int i = 0; i < circ_pnt; i++)
  62. {
  63. glColor3f(1.0, 0.0, 0.0); // cor
  64. ang = (2 * PI * i) / circ_pnt;
  65. glVertex2f(cos(ang) * raioX, sin(ang) * raioY);
  66. }
  67. glEnd();
  68. glBegin(GL_LINES); /// linha para ter a base
  69. glVertex3f(0, 0, 0);
  70. glVertex3f(raioX, 0, 0);
  71. glEnd();
  72.  
  73. glTranslatef(trans, 0, 0);
  74. circ_pnt = 100;
  75. raioX = 50.0;
  76. raioY = 50.0;
  77. glBegin(GL_LINE_LOOP);
  78. for (int i = 0; i < circ_pnt; i++)
  79. {
  80. glColor3f(1.0, 1.0, 0.0); // cor
  81. ang = (2 * PI * i) / circ_pnt;
  82. glVertex2f(cos(ang) * raioX, sin(ang) * raioY);
  83. }
  84. glEnd();
  85. glBegin(GL_LINES); // linha para ter a base
  86. glVertex3f(0, 0, 0);
  87. glVertex3f((raioX * (-1)), 0, 0);
  88. glEnd();
  89. }
  90.  
  91. void display()
  92. {
  93. glMatrixMode(GL_MODELVIEW); //coordenadas de desenho
  94. glLoadIdentity();
  95. if (trans - 100 > 0)
  96. {
  97. glClearColor(0.0f, 0.0f, 1.0f, 1.0f); // cor do fundo
  98. }
  99. else
  100. {
  101. glClearColor(1.0f, 1.0f, 1.0f, 1.0f); // cor do fundo
  102. }
  103. glClear(GL_COLOR_BUFFER_BIT); // EXECUTA LIMPESA
  104. // Especificar o local aonde o desenho acontece: bem no centro da janela
  105. glTranslatef(janela_largura / 2, janela_altura / 2, 0.0f);
  106. glViewport(0, 0, janela_largura, janela_altura);
  107. desenhar();
  108. glFlush(); // execute o desenho
  109. }
  110. void tela(GLsizei w, GLsizei h)
  111. {
  112. glMatrixMode(GL_PROJECTION);
  113. glLoadIdentity();
  114. // cria a janela (esq, direita, embaixo, em cima)
  115. gluOrtho2D(0, janela_largura, 0, janela_altura);
  116. glMatrixMode(GL_MODELVIEW);
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement