Advertisement
Guest User

Untitled

a guest
Nov 21st, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.27 KB | None | 0 0
  1. #define WINDOW_H 500
  2. #define WINDOW_W 500
  3.  
  4. #define ORTHO_X WINDOW_W/2
  5. #define ORTHO_Y WINDOW_H/2
  6.  
  7. float ya;
  8. float yb;
  9. float xa;
  10. float xb;
  11.  
  12. int click = 0;
  13.  
  14. double c; // coté du carré
  15.  
  16. void drawFrom(void); // modélisation
  17. void keyboard(unsigned char key,int x,int y); // fonction clavier
  18. void mouse(int bouton,int etat,int x,int y); // fonction souris
  19.  
  20. void drawFrom(){
  21. float x, y, m, p;
  22. x = 0;
  23. y = 0;
  24.  
  25. m = (yb - ya)/(xb - xa);
  26. p = ya - (m * xa);
  27.  
  28. glClear(GL_COLOR_BUFFER_BIT);
  29.  
  30. // DRAW
  31. glBegin(GL_POINTS);
  32. glColor3f(0,1,1);
  33.  
  34. for (x = xa; x < xb; x++) {
  35. y = floor(m * x + p);
  36. glVertex2f(x, y);
  37. }
  38.  
  39. glEnd();
  40.  
  41. // On force l'affichage du résultat
  42. glFlush();
  43. }
  44.  
  45.  
  46. void mouse(int button,int state,int x,int y)
  47. {
  48. // Si on appuie sur le bouton de gauche
  49. if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
  50. {
  51. if (click) {
  52. xa = x - ORTHO_X; //on sauvegarde la position de la souris
  53. ya = -y + ORTHO_Y;
  54. click = 0;
  55. }else{
  56. xb = x - ORTHO_X; //on sauvegarde la position de la souris
  57. yb = -y + ORTHO_Y;
  58. click = 1;
  59. }
  60. drawFrom();
  61. }
  62.  
  63. }
  64.  
  65. /* Evènement du clavier */
  66. void keyboard(unsigned char key,int x,int y){
  67. switch (key){
  68. case 'a':
  69. printf("Key %d pressed\n", key);;
  70. break;
  71. }
  72. }
  73.  
  74. int main(int argc, char *argv[]) {
  75. glutInit(&argc, argv); // Initialisation
  76. glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH); // mode d'affichage RGB, et test prafondeur
  77. glutInitWindowSize(WINDOW_W, WINDOW_H); // dimension fenêtre
  78. glutInitWindowPosition (100, 100); // position coin haut gauche
  79. glutCreateWindow(""); // nom
  80.  
  81. /* Repère 2D délimitant les abscisses et les ordonnées*/
  82. // left right bottom top
  83. gluOrtho2D(-(double)ORTHO_Y,(double)ORTHO_Y,-(double)ORTHO_X,(double)ORTHO_X);
  84.  
  85. /* Initialisation d'OpenGL */
  86. glClearColor(0.0, 0.0, 0.0, 0.0);
  87. glColor3f(1.0, 1.0, 1.0); // couleur: blanc
  88. glPointSize(2.0); // taille d'un point: 2px
  89.  
  90. glutDisplayFunc(drawFrom);
  91. glutKeyboardFunc(keyboard);
  92. glutMouseFunc(mouse);
  93.  
  94. glutMainLoop(); // lancement de la boucle de réception des évènements
  95.  
  96. return 0;
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement