DDA Algorithm

#include <stdio.h>
#include <math.h>
#include <GL/glut.h>
//#include <GL/glu.h>
//#include <GL/gl.h>

double X1, Y1, X2, Y2;

float round_value(float v)
{
  floor(v + 0.5);// This function is used to roundup the values
  return v;
}
void LineDDA(void)
{
  double dx=(X2-X1);
  double dy=(Y2-Y1);
  double steps;
  float xInc,yInc,x=X1,y=Y1;
  /* Find out whether to increment x or y */
  //steps=(abs(dx)>abs(dy))?(abs(dx)):(abs(dy));
  if(abs(dx)>abs(dy))
  steps=abs(dx);
  else
  steps=abs(dy);
  xInc=dx/(float)steps;
  yInc=dy/(float)steps;

  /* Clears buffers to preset values */
  glClear(GL_COLOR_BUFFER_BIT);//glClear is predefine function used to clear buffer and
                  //GL_COLOR_BUFFER_BIT Indicates the buffers currently enabled for color writing.
                   

  /* Plot the points */
  glBegin(GL_POINTS);   //The basic shapes are referred to as primitives.and glbegin is used to delimit these primitives
                       //GL_POINTS is an argument of glBegin which consider each vertex as a single points
  /* Plot the first point */
  glVertex2d(x,y);  //it is use to specify vertex
  int k;
  /* For every step, find an intermediate vertex */
  for(k=0;k<steps;k++)
  {
    x+=xInc;
    y+=yInc;
    /* printf("%0.6lf %0.6lf\n",floor(x), floor(y)); */
    glVertex2d(round_value(x), round_value(y));
  }
  glEnd();

  glFlush();
}
void Init()
{
  /* Set clear color to white */
  glClearColor(1.0,1.0,1.0,0); 
  /* Set fill color to black */
  glColor3f(0.0,0.0,0.0);        //used to specify color & 3f stand for float red , green, blue
  /* glViewport(0 , 0 , 640 , 480); */      //set viewport (intx, int y, size width, size of height)
  /* glMatrixMode(GL_PROJECTION); */        //use to set  which matrix is the current matrix
  /* glLoadIdentity(); */                   //
  gluOrtho2D(0 , 640 , 0 , 480);          //multiply the current matrix with an orthographic matrix (3 dimentional object into two dimensonal)
}
void main(int argc, char **argv)
{
  printf("Enter two end points of the line to be drawn:\n");
  printf("\n************************************");
  printf("\nEnter Point1( X1 , Y1):\n");
  scanf("%lf%lf",&X1,&Y1);
  printf("\n************************************");
  printf("\nEnter Point1( X2 , Y2):\n");
  scanf("%lf%lf",&X2,&Y2);
 
  /* Initialise GLUT library */
  glutInit(&argc,argv);
  /* Set the initial display mode */
  glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);  //single buffer window
  /* Set the initial window position and size */
  glutInitWindowPosition(0,0);
  glutInitWindowSize(640,480);
  /* Create the window with title "DDA_Line" */
  glutCreateWindow("DDA_Line");
  /* Initialize drawing colors */
  Init();
  /* Call the displaying function */
  glutDisplayFunc(LineDDA);
  /* Keep displaying untill the program is closed */
  glutMainLoop();
}

Comments

Popular posts from this blog

Installation steps for openGL on ubuntu

Second Assignment DDA algorithm for line drawing using Mouse