Advertisement
Guest User

ddalinedrownling

a guest
Apr 9th, 2020
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.25 KB | None | 0 0
  1. /*
  2. Shamsuddin Ahmed
  3. 161-15-1027
  4.  
  5. Draw Line using Digital Differential Analyzer
  6. */
  7.  
  8.  
  9.  
  10. #include<stdio.h>
  11. #include <GL/gl.h>
  12. #include <GL/glut.h>
  13. float x1,y1,x2,y2,m,i,j;
  14. float dx,dy;
  15. void display(void)
  16. {
  17. /* clear all pixels */
  18. glClear (GL_COLOR_BUFFER_BIT);
  19. /* draw white polygon (rectangle) with corners at
  20. * (0.25, 0.25, 0.0) and (0.75, 0.75, 0.0)
  21. */
  22. glEnd();
  23.  
  24. glColor3f (0.0, 1.0, 0.0);
  25. glBegin(GL_POINTS);
  26. //write your code here
  27.  
  28. if(m>0 && m<=1)
  29. {
  30. while(x1<=x2 && y1<=y2)
  31. {
  32.     x1=x1+1;
  33.     y1=y1+m;
  34.     glVertex3f(x1/100,y1/100,0.0);
  35.    printf("%f %f",x1,y1);
  36.  
  37. }
  38. }
  39. else if(m>1)
  40. {
  41.     while(x1<=x2 && y1<=y2)
  42. {
  43.     x1=x1+(1/m);
  44.     y1=y1+1;
  45.    glVertex3f(x1/100,y1/100,0.0);
  46.    printf("%f %f",x1,y1);
  47. }
  48. }
  49.  
  50. else if(m>-1 && m<=0)
  51. {
  52.     while(x1>=x2 && y1>=y2)
  53. {
  54.     x1=x1-1;
  55.     y1=y1-m;
  56.    glVertex3f(x1/100,y1/100,0.0);
  57.    printf("%f %f",x1,y1);
  58. }
  59. }
  60. else if(m<-1)
  61.  
  62.   {
  63.  
  64.     while(x1>=x2 && y1>=y2)
  65. {
  66.     x1=x1-(1/m);
  67.     y1=y1-1;
  68.     glVertex3f(x1/100,y1/100,0.0);
  69.     printf("%f %f",x1,y1);
  70. }
  71.   }
  72.  
  73. glEnd();
  74.  
  75.  
  76. /* don't wait!
  77. * start processing buffered OpenGL routines
  78. */
  79. glFlush ();
  80. }
  81. void init (void)
  82. {
  83. /* select clearing (background) color */
  84. glClearColor (0.0, 0.0, 0.0, 0.0);
  85. /* initialize viewing values */
  86. glMatrixMode(GL_PROJECTION);
  87. glLoadIdentity();
  88. glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
  89. }
  90. /*
  91. * Declare initial window size, position, and display mode
  92. * (single buffer and RGBA). Open window with "hello"
  93. * in its title bar. Call initialization routines.
  94. * Register callback function to display graphics.
  95. * Enter main loop and process events.
  96. */
  97. int main(int argc, char** argv)
  98. {
  99.  
  100.     //glVertex3f(x1/100,y1/100,0.0);write your code here
  101.     printf("Enter value of X1 :");
  102.     scanf("%f",&x1);
  103.     printf("Enter value of y1 :");
  104.     scanf("%f",&y1);
  105.     printf("Enter value of X2 :");
  106.     scanf("%f",&x2);
  107.     printf("Enter value of Y2 :");
  108.     scanf("%f",&y2);
  109.     dx=x2-x1;
  110.     dy=y2-y1;
  111.     m=dy/dx;
  112.  
  113.  
  114. glutInit(&argc, argv);
  115. glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
  116. glutInitWindowSize (500, 500);
  117. glutInitWindowPosition (100, 100);
  118. glutCreateWindow ("hello");
  119. init ();
  120. glutDisplayFunc(display);
  121. glutMainLoop();
  122. return 0; /* ISO C requires main to return int. */
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement