Guest User

Untitled

a guest
Apr 20th, 2018
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.61 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <iostream>
  3. #include <cstdio>
  4. #include <GL/glut.h>
  5.  
  6. using namespace std;
  7.  
  8. int x1,x2,y1,y2 = 0;
  9.  
  10. void drawLine ( int x1 , int y1 , int x2 , int y2 )
  11. {
  12. int dx = x2 - x1 ;
  13. int dy = y2 - y1 ;
  14. float m = dy / dx;
  15. if ( m>=0 && m <= 1 )
  16. {
  17. int y = y1;
  18. int p = 2*dx - dy ;
  19. glBegin ( GL_POINTS );
  20. for (int x = x1 ; x <= x2 ; x++ )
  21. {
  22. glVertex2f ( (GLint) x , (GLint) y );
  23.  
  24. if ( p < 0 )
  25. {
  26. p = p + 2*dy;
  27. }else
  28. {
  29. p = p + 2 * (dy - dx);
  30. y = y + 1;
  31. }
  32.  
  33. }
  34. glEnd ();
  35. }else
  36. {
  37. int x = x1;
  38. int p = 2*dy - dx ;
  39. glBegin ( GL_POINTS );
  40. for (int y = y1 ; y <= y2 ; y++ )
  41. {
  42. glVertex2f ( (GLint) x , (GLint) y );
  43.  
  44. if ( p < 0 )
  45. {
  46. p = p + 2*dx;
  47. }else
  48. {
  49. p = p + 2 * (dx - dy);
  50. x = x + 1;
  51. }
  52.  
  53. }
  54. glEnd ();
  55. }
  56.  
  57. }
  58.  
  59. void Display( void )
  60. {
  61. glClear ( GL_COLOR_BUFFER_BIT);
  62. drawLine ( x1, y1, x2 ,y2);
  63. glFlush ();
  64. }
  65.  
  66. void reshape ( int w , int h )
  67. {
  68. glViewport (0,0,(GLsizei) w, (GLsizei) h) ;
  69. glMatrixMode (GL_PROJECTION);
  70. glLoadIdentity ();
  71. gluOrtho2D ( 0.0 , 160.0 , 0.0 , 120.0 );
  72.  
  73. }
  74.  
  75. void init ( void )
  76. {
  77. glClearColor ( 0.0 , 0.8 , 0.0 , 1.0 );
  78. glPointSize ( 5.0 );
  79. }
  80.  
  81. int main (int argc, char *argv[] )
  82. {
  83.  
  84. x1 = 0 ;
  85. y1 = 0;
  86. x2 = 1 ;
  87. y2 = 100;
  88. glutInit (&argc , argv );
  89. glutInitDisplayMode ( GLUT_SINGLE | GLUT_RGB );
  90. glutInitWindowSize ( 320 , 240 ) ;
  91. glutCreateWindow ( " Bresenham's Algorithm " ) ;
  92. init ();
  93. glutDisplayFunc ( Display );
  94. glutReshapeFunc ( reshape ) ;
  95. glutMainLoop ();
  96.  
  97. return 0;
  98. }
Add Comment
Please, Sign In to add comment