# DDA Algorithm

Sep 30th, 2020
717
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. void display(void)
4. {
5. /* clear all pixels */
6. glClear (GL_COLOR_BUFFER_BIT);
7. /* draw white polygon (rectangle) with corners at
8. * (0.25, 0.25, 0.0) and (0.75, 0.75, 0.0)
9. */
10. // Lab3 Task: Draw a line with edndpoints (100, 130) and (355, 450)
11.
12. glColor3f (1.0, 0.25, 1.0);
13.
14. int x1=100, y1=130, x2=355, y2=450;
15.
16. float dy,dx,step,x,y,k,Xin,Yin;
17. dx=x2-x1;
18. dy=y2-y1;
19.
20.
21. if(abs(dx)> abs(dy))
22. {
23. step = abs(dx);
24. }
25. else
26. step = abs(dy);
27.
28. Xin = dx/step;
29. Yin = dy/step;
30.
31. x= x1;
32. y=y1;
33. glBegin(GL_POINTS);
34. glVertex2i(x,y);
35. glEnd();
36.
37. for (k=1 ;k<=step;k++)
38. {
39. x= x + Xin;
40. y= y + Yin;
41.
42. glBegin(GL_POINTS);
43. glVertex2i(x,y);
44. glEnd();
45. }
46.
47. /* don't wait!
48. * start processing buffered OpenGL routines
49. */
50. glFlush ();
51. }
52. void init (void)
53. {
54. /* select clearing (background) color */
55. glClearColor (0.0, 0.0, 0.0, 0.0);
56. /* initialize viewing values */
57. glMatrixMode(GL_PROJECTION);
59. // glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
60. gluOrtho2D(0,600,0,600);        //fix the windows size
61. }
62. /*
63. * Declare initial window size, position, and display mode
64. * (single buffer and RGBA). Open window with "hello"
65. * in its title bar. Call initialization routines.
66. * Register callback function to display graphics.
67. * Enter main loop and process events.
68. */
69. int main(int argc, char** argv)
70. {
71.
72. glutInit(&argc, argv);
73. glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
74. glutInitWindowSize (1000, 1000);
75. glutInitWindowPosition (50, 50);
76. glutCreateWindow ("172-15-9804");
77. init ();
78. glutDisplayFunc(display);
79. glutMainLoop();
80. return 0; /* ISO C requires main to return int. */
81. }
RAW Paste Data