Advertisement
axyd

lab_11

Apr 16th, 2019
375
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.54 KB | None | 0 0
  1. #include <GL/glut.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include <iostream>
  5.  
  6. float round_value(float v) {
  7.     return floor(v + 0.5);
  8. }
  9.  
  10. void LineDDA(void) {
  11.     //set line coordinates
  12.     double X1, Y1, X2, Y2;
  13.     X1 = Y1 = 50;
  14.     X2 = 300;
  15.     Y2 = 200;
  16.  
  17.     double dx=(X2-X1), dy = (Y2-Y1), steps;
  18.     float xInc, yInc, x=X1, y=Y1;
  19.    
  20.     /* Find out whether to increment x or y */
  21.     steps=(abs(dx)>abs(dy)) ? (abs(dx)) : (abs(dy));
  22.     xInc=dx/(float)steps;
  23.     yInc=dy/(float)steps;
  24.  
  25.     /* Clears buffers to preset values */
  26.     glClear(GL_COLOR_BUFFER_BIT);
  27.  
  28.     /* Plot the points */
  29.     glBegin(GL_POINTS);
  30.  
  31.     /* Plot the first point */
  32.     glVertex2d(x,y);
  33.     int k;
  34.  
  35.     /* For every step, find an intermediate vertex */
  36.     for(k=0; k<steps; k++)  {
  37.         x += xInc;
  38.         y += yInc;
  39.  
  40.         glVertex2d(round_value(x), round_value(y));
  41.     }
  42.     glEnd();
  43.  
  44.     glFlush();
  45. }
  46.  
  47. int main(int argc,char *argv[]) {
  48.     glutInit(&argc,argv);                   //prepare glut
  49.     glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);       //set the display mode
  50.     glutInitWindowSize (500, 500);          //the size of the program window
  51.     glutInitWindowPosition (100, 100);      //initial position for the window
  52.     glutCreateWindow ("CS334-lab11");  //create window with a title
  53.  
  54.     glClearColor(0,0,0,0.0);          //background color
  55.     glMatrixMode (GL_PROJECTION);           //2d envyronment
  56.     gluOrtho2D (0, 350.0, 0, 250.0);   //canvas area (LRBT)
  57.  
  58.     glutDisplayFunc(LineDDA);               //show thee actual program
  59.     glutMainLoop();
  60.  
  61.     return EXIT_SUCCESS;                    //exit the program
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement