Advertisement
mhdew

DDA Algorithm

Oct 15th, 2021
983
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.76 KB | None | 0 0
  1. #include <GL/glut.h>
  2. #include <stdio.h>
  3. #include <GL/gl.h>
  4. #include <math.h>
  5.  
  6. void init(void)
  7. {
  8.     glClearColor(0.0,0.0,0.0,0.0); //GLfloat red,green,blue,alpha initial value 0 alpha values used by glclear to clear the color buffers
  9.     glMatrixMode(GL_PROJECTION);  // To specify which matrix is the current matrix & projection applies subsequent matrix to projecton matrix stack
  10.     glLoadIdentity();
  11.     glOrtho(0.0, 300.0, 0.0, 300.0, -1.0, 1.0);
  12. }
  13.  
  14. void Draw()
  15. {
  16.  
  17.     int xs=150, ys=220, xe=90, ye=120;     //starting and ending point
  18.     //x1 is xs, x2 is xe, y1 is ys, y2 is ye
  19.     double xi, yi, m;
  20.     glClear(GL_COLOR_BUFFER_BIT);
  21.     glColor3f( 1,0, 0);
  22.  
  23.     m = (ye-ys)/(xe-xs);
  24.  
  25.     if(xe>xs){
  26.         if(m<=1){
  27.             xi = 1;
  28.             yi = m;
  29.         }   //case 1
  30.         else if(m>1){
  31.             yi = 1;
  32.             xi = 1/m;
  33.         }
  34.     }       //case 1 or case 2
  35.     else if(xs>xe){
  36.         if(m<=1){
  37.             xi = -1;
  38.             yi = -m;
  39.         }   //case 3
  40.         else if(m>1){
  41.             xi = (-1)/m;
  42.             yi = -1;
  43.         }   //case 4
  44.     }       //case 3 or case 4
  45.  
  46.     //here xi is change of x and yi is change of y
  47.  
  48.     glBegin(GL_POINTS);
  49.     glVertex2i(round(xs), round(ys));
  50.  
  51.     while(1){
  52.         xs = xs+xi;
  53.         ys = ys+yi;
  54.  
  55.         glVertex2i(round(xs), round(ys));
  56.  
  57.         if(xs==xe || ys==ye){
  58.             break;
  59.         }
  60.     }
  61.  
  62.     glEnd();
  63.  
  64.     glutSwapBuffers();
  65. }
  66.  
  67. int main(int argc,char **argv)
  68. {
  69.     glutInit(&argc,argv);
  70.     glutInitDisplayMode ( GLUT_RGB | GLUT_DOUBLE );
  71.     glutInitWindowPosition(0,0);
  72.     glutInitWindowSize(500,500);
  73.     glutCreateWindow("Lab Final");
  74.     init();
  75.     glutDisplayFunc(Draw);
  76.     glutMainLoop();
  77.     return 0;
  78. }
  79.  
  80.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement