SHARE
TWEET

Bresnham line algorithm v1

a guest Sep 15th, 2019 95 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Windows.h>
  2. #include <GL\glew.h>
  3. #include <GL\freeglut.h>
  4. #include <iostream>
  5.  
  6. using namespace std;
  7. int x00;
  8. int y00;
  9. int xEnd;
  10. int yEnd;
  11.  
  12. void init(){
  13.     glClearColor(1.0,1.0,1.0,1.0);
  14.     glMatrixMode( GL_PROJECTION );
  15.     gluOrtho2D(0,500,0,500);
  16. }
  17.  
  18. void myline()
  19. {    
  20.     int dx = fabs(xEnd - x00), dy = fabs(yEnd - y00);
  21.     int p = 2*dy-dx;
  22.     int x, y;
  23.  
  24.     if(x00>xEnd){
  25.         x=xEnd;
  26.         y=yEnd;
  27.         xEnd=x00;
  28.     }
  29.     else{
  30.         x=x00;
  31.         y=y00;
  32.     }
  33.     glBegin(GL_POINTS);
  34.         glColor3f(0,0,0);
  35.         glVertex2i(x,y);
  36.     glEnd();
  37.  
  38.     while(x<xEnd){
  39.         x++;
  40.         if(p<0){
  41.             p = p + 2*dy;
  42.         }
  43.         else{
  44.             y++;
  45.             p= p + 2*dy - 2*dx;
  46.        }
  47.         glBegin(GL_POINTS);
  48.             glColor3f(0,0,0);
  49.             glVertex2i(x,y);
  50.         glEnd();
  51.  
  52.     }
  53.  
  54. }
  55.  
  56. void display()
  57. {
  58.     glClear(GL_COLOR_BUFFER_BIT);
  59.     myline();
  60. glFlush();
  61.  
  62. }
  63.  
  64. int main(int argc, char* argv[])
  65. {
  66.     cout<<"Enter the co ordinates for 2 points: ";
  67.     cin>>x00>>y00>>xEnd>>yEnd;
  68.  
  69.     glutInit(&argc, argv);
  70.     glutInitWindowSize(600,600);
  71.     glutInitWindowPosition(10,10);
  72.     glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE | GLUT_DEPTH);
  73.     glutCreateWindow("Bresenham's Algo");
  74.  
  75.     init();
  76.     glutDisplayFunc(display);
  77.     glutMainLoop();
  78.  
  79.     return 0;
  80. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top