Advertisement
iamyeasin

OpenGL Line Drawings using algoritgms

Oct 9th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.39 KB | None | 0 0
  1. #include<windows.h>
  2. #include <GL/glut.h>
  3. #include <GL/gl.h>
  4. #include <bits/stdc++.h>
  5.  
  6. using namespace std;
  7.  
  8. double x1,y11,x2,y2,delx,dely,delm,x,y,pk;
  9.  
  10. void display(void)
  11. {
  12. glClear(GL_COLOR_BUFFER_BIT);
  13. glColor3f(1.0,0.0,0.0);
  14.  
  15. // glBegin(GL_POLYGON);
  16. // glVertex2i(10,10);
  17. // glVertex2i(30,10);
  18. // glVertex2i(30,60);
  19. // glVertex2i(10,60);
  20. // glEnd();
  21.  
  22. /*x = x1, y= y11;
  23. delx = x2-x1 ,dely = y2-y11;
  24. delm = dely/delx;
  25.  
  26. if( abs( delm ) <= 1 ){
  27. while( x < x2 ){
  28. x++, y += delm;
  29. glBegin(GL_POINTS);
  30. glVertex2i(x,round(y));
  31. glEnd();
  32. }
  33. }
  34. else{
  35. while( y < y2 ){
  36. y++; x += ( 1/delm );
  37. glBegin(GL_POINTS);
  38. glVertex2i(round(x),y);
  39. glEnd();
  40. }
  41. }
  42. */
  43. x = x1, y= y11;
  44. delx = x2-x1 ,dely = y2-y11;
  45. delm = dely/delx;
  46. pk =( 2* dely) - delx;
  47.  
  48.  
  49.  
  50.  
  51. if( delm == 0.0000 ) {
  52. cout << delm << endl;
  53. while( x < x2 ){
  54. x++;
  55. glBegin(GL_POINTS);
  56. glVertex2i(x,y);
  57. glEnd();
  58. }
  59. }
  60. else if( delm < 1 ){
  61. while( x < x2 ){
  62. x++;
  63. if( pk > 0 ){
  64. pk +=( 2 * ( dely - delx) );
  65. y++;
  66. }
  67. else{
  68. pk += (dely*2);
  69. y++;
  70. }
  71. glBegin(GL_POINTS);
  72. glVertex2i(x,y);
  73. glEnd();
  74. }
  75. }
  76. else {
  77. while( y < y2 ){
  78. y++;
  79. glBegin(GL_POINTS);
  80. glVertex2i(x,y);
  81. glEnd();
  82. }
  83. }
  84.  
  85. glFlush();
  86.  
  87. }
  88.  
  89. void init(void)
  90. {
  91. glClearColor(1,1,1,5.0);
  92. glMatrixMode(GL_PROJECTION);
  93. // glClear(GL_COLOR_BUFFER_BIT);
  94. glLoadIdentity();
  95. glPointSize(5.0);
  96. glOrtho(0.0,100.0,0.0,100.0,-100.0,100.0);
  97.  
  98. }
  99. int main(int a,char **ar)
  100. {
  101. glutInit(&a,ar);
  102. glutInitDisplayMode(GLUT_RED | GLUT_SINGLE);
  103. glutInitWindowPosition(100,100);
  104. glutInitWindowSize(800,500);
  105. glutCreateWindow("Polygon");
  106. cin >> x1 >> y11 >> x2 >> y2 ;
  107.  
  108. init();
  109. glutDisplayFunc(display);
  110. glutMainLoop();
  111.  
  112. return 0;
  113.  
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement