Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define WINDOW_H 500
- #define WINDOW_W 500
- #define ORTHO_X WINDOW_W/2
- #define ORTHO_Y WINDOW_H/2
- float ya;
- float yb;
- float xa;
- float xb;
- int click = 0;
- double c; // coté du carré
- void drawFrom(void); // modélisation
- void keyboard(unsigned char key,int x,int y); // fonction clavier
- void mouse(int bouton,int etat,int x,int y); // fonction souris
- void drawFrom(){
- float x, y, m, p;
- x = 0;
- y = 0;
- m = (yb - ya)/(xb - xa);
- p = ya - (m * xa);
- glClear(GL_COLOR_BUFFER_BIT);
- // DRAW
- glBegin(GL_POINTS);
- glColor3f(0,1,1);
- for (x = xa; x < xb; x++) {
- y = floor(m * x + p);
- glVertex2f(x, y);
- }
- glEnd();
- // On force l'affichage du résultat
- glFlush();
- }
- void mouse(int button,int state,int x,int y)
- {
- // Si on appuie sur le bouton de gauche
- if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
- {
- if (click) {
- xa = x - ORTHO_X; //on sauvegarde la position de la souris
- ya = -y + ORTHO_Y;
- click = 0;
- }else{
- xb = x - ORTHO_X; //on sauvegarde la position de la souris
- yb = -y + ORTHO_Y;
- click = 1;
- }
- drawFrom();
- }
- }
- /* Evènement du clavier */
- void keyboard(unsigned char key,int x,int y){
- switch (key){
- case 'a':
- printf("Key %d pressed\n", key);;
- break;
- }
- }
- int main(int argc, char *argv[]) {
- glutInit(&argc, argv); // Initialisation
- glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH); // mode d'affichage RGB, et test prafondeur
- glutInitWindowSize(WINDOW_W, WINDOW_H); // dimension fenêtre
- glutInitWindowPosition (100, 100); // position coin haut gauche
- glutCreateWindow(""); // nom
- /* Repère 2D délimitant les abscisses et les ordonnées*/
- // left right bottom top
- gluOrtho2D(-(double)ORTHO_Y,(double)ORTHO_Y,-(double)ORTHO_X,(double)ORTHO_X);
- /* Initialisation d'OpenGL */
- glClearColor(0.0, 0.0, 0.0, 0.0);
- glColor3f(1.0, 1.0, 1.0); // couleur: blanc
- glPointSize(2.0); // taille d'un point: 2px
- glutDisplayFunc(drawFrom);
- glutKeyboardFunc(keyboard);
- glutMouseFunc(mouse);
- glutMainLoop(); // lancement de la boucle de réception des évènements
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement