Showing posts with label dda. Show all posts
Showing posts with label dda. Show all posts

Sunday, June 11, 2017

DDA Line Drawing Algorithm

DDA Line Drawing Algorithm



PROGRAM:
using namespace std;
#include<iostream>
#include<graphics.h>
#include<math.h>

class DDALINE
{
    int xmid,ymid;

    float x1,y1,x2,y2;
public :
    void quad();
    void simple(float,float,float,float);
    void dot(float,float,float,float);
    void dash(float,float,float,float);
    void dashdot(float,float,float,float);
    void thick(float,float,float,float,int);

};

void DDALINE :: quad()
{
    int xmax=640,ymax=480;
    int gd=DETECT,gm;
    initgraph(&gd,&gm,NULL);
    xmid=xmax/2;
    ymid=ymax/2;
    line(0,ymid,xmax,ymid);
    line(xmid,0,xmid,ymax);
}

void DDALINE :: simple(float x1,float y1,float x2,float y2)
{
    int i,steps;
    float dx,dy,xinc,yinc;
    dx=x2-x1;
    dy=y2-y1 ;

    if(dx<dy)
    {
        steps=abs(dy);
    }
    else
    {
        steps=abs(dx);
    }
    xinc=(x2-x1)/steps;
    yinc=(y2-y1)/steps;
    putpixel(x1,y1,15);
    for(i=1;i<=steps;i++)
                {
                x1=x1+xinc;
                y1=y1+yinc;
                       putpixel(x1,y1,18);
              }
}
     
void DDALINE :: dot(float x1,float y1,float x2,float y2)
{
int i,steps;
    float dx,dy,xinc,yinc;

    dx=x2-x1;
    dy=y2-y1 ;

    if(dx<dy)
    {
        steps=abs(dy);
    }
    else
    {
        steps=abs(dx);
    }

    xinc=(x2-x1)/steps;
    yinc=(y2-y1)/steps;
    putpixel(x1,y1,15);
    for(i=1;i<=steps;i++)
                {
                x1=x1+xinc;
                y1=y1+yinc;
    if(i%2==0)                     
    {putpixel(x1,y1,18);}
              }  

}

void DDALINE :: dash(float x1,float y1,float x2,float y2)
{
    int i,steps;
    float dx,dy,xinc,yinc;
    dx=x2-x1;
    dy=y2-y1 ;
    if(dx<dy)
    {
        steps=abs(dy);
    }
    else
    {
        steps=abs(dx);
    }

    xinc=(x2-x1)/steps;
    yinc=(y2-y1)/steps;
    putpixel(x1,y1,15);
    for(i=1;i<=steps;i++)
                {
                x1=x1+xinc;
                y1=y1+yinc;
    if(i%5<3)                     
    {putpixel(x1,y1,18);}
              }  

}

void DDALINE :: dashdot(float x1,float y1,float x2,float y2)
{
    int i,steps;
    float dx,dy,xinc,yinc;
    dx=x2-x1;
    dy=y2-y1 ;
    if(dx<dy)
    {
        steps=abs(dy);
    }
    else
    {
        steps=abs(dx);
    }
    xinc=(x2-x1)/steps;
    yinc=(y2-y1)/steps;
    putpixel(x1,y1,15);
    for(i=1;i<=steps;i++)
                {
                x1=x1+xinc;
                y1=y1+yinc;
                if(i%6<3 || i%6==4)                     
                {putpixel(x1,y1,18);}
              }  

}

void DDALINE :: thick(float x1,float y1,float x2,float y2,int t)
{
    int i,j,steps;
    float dx,dy,xinc,yinc;
    dx=x2-x1;
    dy=y2-y1 ;
    if(dx<dy)
    {
        steps=abs(dy);
    }
    else
    {
        steps=abs(dx);
    }
    xinc=(x2-x1)/steps;
    yinc=(y2-y1)/steps;
    putpixel(x1,y1,15);
    for(i=1;i<=steps;i++)
                {
                x1=x1+xinc;
                y1=y1+yinc;
                for(j=0;j<t;j++)                     
                {putpixel(x1,y1+j,18);}
              }  

}



int main()
{
    int gd=DETECT,gm;
    float x1,y1,x2,y2;
    int ans, ch,t;
    DDALINE p;
    do
    {
        cout<<" Enter X1 : ";
        cin>>x1;
        cout<<" Enter Y1 : ";
        cin>>y1;
        cout<<" Enter X2 : ";
        cin>>x2;
        cout<<" Enter Y2 : ";
        cin>>y2;
        x1=x1+320;
        y1=240-y1;
        x2=x2+320;
        y2=240-y2;
      cout<<" Menu:";
      cout<<" 1.Simple line 2.Dashed line 3.Dotted line 4.Center line 5.thick";
      cout<<" Enter choice : ";
      cin>>ch;
      switch(ch)
      {

          case 1:
        p.quad();
        p.simple(x1,y1,x2,y2);  
         break;
         case 2:
        p.quad();
        p.dash(x1,y1,x2,y2);           
        break;
        case 3:
        p.quad();
        p.dot(x1,y1,x2,y2);
         break;
        case 4:
        p.quad();
        p.dashdot(x1,y1,x2,y2);
              break;
        case 5:
        printf(" Enter the thick ness of line");      
        scanf("%d",&t);
        p.quad();
        p.thick(x1,y1,x2,y2,t);
              break;
    }      
    getch();  
    closegraph();
        cout<<"Do You Want To Continue : ";
        cin>>ans;
    getch();  
    closegraph();
    }while(ans==1);
    getch();
    return 0;
}
Read more »

Wednesday, May 10, 2017

DDA algorithm implementation using OpenGL in C

DDA algorithm implementation using OpenGL in C


#include<GL/glut.h>
#include<windows.h>
#include<stdlib.h>
#include<stdio.h>
GLint x0,y0,xEnd,yEnd;
inline GLint round(const GLfloat a)
{
    return GLint(a+0.5);
}
void myInit(void)
{
    glClearColor(0.0,1.0,1.0,0.0);
    glColor3f(1.0f,0.0f,0.0f);
    glPointSize(3.0);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0.0,640.0,0.0,480.0);
}
void readInput()
{

    printf("Enter x0, y0, xEnd, yEnd(resp): ");
    scanf("%i %i %i %i",&x0,&y0,&xEnd,&yEnd);

}
void setPixel(GLint xcoordinate, GLint ycoordinate)
{
    glBegin(GL_POINTS);
    glVertex2i(xcoordinate,ycoordinate);
    glEnd();
    glFlush();
}

void lineDDA(GLint x0,GLint y0,GLint xEnd,GLint yEnd)
{
    GLint dx = abs(xEnd-x0);
    GLint dy = abs(yEnd-y0);
    GLint steps,k;
    GLfloat xIncrement,yIncrement,x=x0,y=y0;

    if(dx>dy)
        steps = dx;
    else
        steps = dy;

    xIncrement = GLfloat(dx) / GLfloat(steps);
    yIncrement = GLfloat(dy) / GLfloat(steps);
    setPixel(round(x),round(y));

    for(k=1;k<steps;k++)
    {
        x+= xIncrement;
        y+= yIncrement;
        setPixel(round(x),round(y));
    }
}

void Display(void)
{
    glClear(GL_COLOR_BUFFER_BIT);
    lineDDA(x0,y0,xEnd,yEnd);
}

int main(int argc,char *argv[])
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(600,600);
    glutInitWindowPosition(50,50);
    glutCreateWindow("DDA Line Algorithum");
    readInput();
    glutDisplayFunc(Display);
    myInit();
    glutMainLoop();
    return EXIT_SUCCESS;
}
Read more »

Friday, April 28, 2017

DDA Circle

DDA Circle


#include<iostream>
#include<math.h>
#include<graphics.h>
using namespace std;
void dda_circle(int xc,int yc,int r);
int main()
{
   int xc,yc,r,gd=DETECT,gm;
  cout<<" Enter the Center point (xc,yc) :";
   cin>>xc>>yc;
   cout<<" Enter the Radius :";
   cin>>r;
  initgraph(&gd,&gm,"");
   setcolor(10);
   line(0,240,640,240);
   line(320,0,320,480);
   setcolor(5);
   xc = 320 + xc;
   yc = 240 - yc;
   dda_circle(xc,yc,r);
   getch();
closegraph();  
return 0;
}
void dda_circle(int xc,int yc,int r)
{
   float xc1,xc2,yc1,yc2,eps,sx,sy;
  int val,i;
  xc1=r;
  yc1=0;
  sx=xc1;
  sy=yc1;
  i=0;
  do{
      val=pow(2,i);
      i++;
      }while(val<r);
  eps = 1/pow(2,i-1);
  do{
      xc2 = xc1 + yc1*eps;
      yc2 = yc1 - eps*xc2;
      putpixel(xc+xc2+r,yc-yc2,3);
      putpixel(xc+xc2-r,yc-yc2,4);
      putpixel(xc+xc2,yc-yc2+r,5);
      putpixel(xc+xc2,yc-yc2-r,6);
      putpixel(xc+xc2,yc-yc2,15);
      xc1=xc2;
      yc1=yc2;
     }while((yc1-sy)<eps || (sx-xc1)>eps);
}
Read more »