Thursday, 3 December 2015

bresenham

# include <stdio.h>
  # include <conio.h>
 #include<GL/gl.h>
#include<GL/glu.h>
#include<GL/glut.h>
int x1, y1, x2, y2,p,x,y;
float m;

void disp ()
{
     glClear(GL_COLOR_BUFFER_BIT);
     glColor3f(1.0,0.0,0.0);
     glPointSize(5.0); // to set size to pixels
     glBegin(GL_POINTS);
     int dx= x2-x1;
     int dy=y2-y1;
     printf("%d\n",dx);
     printf("%d\n",dy);
m=dy/dx;
printf("%d\n",m);

   x = x1;
    y = y1;
    if(m<1)
    {
         
    p = 2 * (dy) - (dx);
printf("%d\n",p);
    while(x <= x2)
    {
      if(p < 0)
      {
        x=x+1;
        y=y;
        p = p + 2 * (dy);
        glVertex2f(x,y);
          printf("%d\t%d\n",x,y);
          printf("%d\n",p);
      }
      else
      {
        x=x+1;
        y=y+1;
        p = p + 2 * (dy - dx);
        glVertex2f(x,y);
          printf("%d\t%d\n",x,y);
          printf("%d\n",p);
     }
}
}
  else
  {
      p=2*dx-dy;
while(x <= x2)
    {
      if(p < 0)
      {
        x=x;
        y=y+1;
        p = p + 2 * (dx);
        glVertex2f(x,y);
          printf("%d\t%d\n",x,y);
          printf("%d\n",p);
      }
      else
      {
        x=x+1;
        y=y+1;
        p = p + 2 * (dx- dy);
        glVertex2f(x,y);
          printf("%d\t%d\n",x,y);
          printf("%d\n",p);
     }
}
}
     glEnd();
     glFlush();
     }
      void abc()
{
     glClearColor(0.0,0.0,0.0,1.0);
     glMatrixMode(GL_PROJECTION);
     glLoadIdentity();
     glOrtho(-100.0,100.0,-100.0,100.0,-1.0,1.0);
     }              
int  main()
{
       printf("Enter the starting points");
      scanf("%d%d",&x1,&y1);
      printf("Enter the ending points points");
      scanf("%d%d",&x2,&y2);
      glutInitDisplayMode(GLUT_RGB|GLUT_SINGLE);
      glutInitWindowSize(500,500);
      glutCreateWindow("LINE");
      glutInitWindowPosition(50,50);
          abc();
      glutDisplayFunc(disp);
      glutMainLoop();
      return 0;
      }
     

No comments:

Post a Comment