alamin54017

DDA Algorithm Forward & Backward

Jun 23rd, 2021 (edited)
836
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <GL/gl.h>
  2. #include <GL/glut.h>
  3. int i;
  4. float x1,y1,x2,y2,dx,dy,m,x,y,step;
  5. void display(void)
  6. {
  7.  
  8. if((x1<x2&&y1<y2)||(x1<x2&&y1>y2)||(x1==x2&&y1<y2)||(x1<x2&&y1==y2)){
  9.     dx=x2-x1;
  10.     dy=y2-y1;
  11.     m=dy/dx;
  12. if(m<=1){
  13.     step=dx;
  14. }
  15. else{
  16.     step=dy;
  17. }
  18. x=x1;
  19. y=y1;
  20. glClear (GL_COLOR_BUFFER_BIT);
  21. glColor3ub (0, 255, 0);
  22. glBegin(GL_POINTS);
  23. for(i=0; i<step; i++){
  24.     if(m<=1){
  25.         x=x+1;
  26.         y=y+m;
  27.     }
  28.     else{
  29.         x=x+1/m;
  30.         y=y+1;
  31.     }
  32.     glVertex2i(x,y);
  33.  
  34. }
  35. }
  36.  
  37.  
  38. else{
  39.     dx=x1-x2;
  40.     dy=y1-y2;
  41.     m=dy/dx;
  42. if(m<=1){
  43.     step=dx;
  44. }
  45. else{
  46.     step=dy;
  47. }
  48. x=x1;
  49. y=y1;
  50. glClear (GL_COLOR_BUFFER_BIT);
  51. glColor3ub (0, 255, 0);
  52. glBegin(GL_POINTS);
  53. for(i=0; i<step; i++){
  54.     if(m<=1){
  55.         x=x-1;
  56.         y=y-m;
  57.     }
  58.     else{
  59.         x=x-1/m;
  60.         y=y-1;
  61.     }
  62.     glVertex2i(x,y);
  63.  
  64. }
  65. }
  66.  
  67. glEnd();
  68. glFlush ();
  69. }
  70. void init (void)
  71. {
  72. /* select clearing (background) color */
  73. glClearColor (0.0, 0.0, 0.0, 0.0);
  74. /* initialize viewing values */
  75. glMatrixMode(GL_PROJECTION);
  76. glLoadIdentity();
  77. gluOrtho2D(0,600,0,600);
  78. }
  79.  
  80. int main(int argc, char** argv)
  81. {
  82.     scanf("%f%f%f%f",&x1,&y1,&x2,&y2);
  83. glutInit(&argc, argv);
  84. glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
  85. glutInitWindowSize (500, 500);
  86. glutInitWindowPosition (100, 100);
  87. glutCreateWindow ("DDA Algorithm");
  88. init ();
  89. glutDisplayFunc(display);
  90. glutMainLoop();
  91. return 0;
  92. }
  93.  
RAW Paste Data