/* WAP to generate a line using DDA.
    Write a function to translate the line by tx amount in x-direction and ty amount in y-direction, where tx and ty are integers provided by the user.
    Use matrix method. - Subhranath Chunder */
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void lineDDA(int,int,int,int,int,int);
void translatePoint(float,float,int,int,float*,float*);
void setPixel(int,int);
void main()
{
int x1,y1,x2,y2,xt,yt;
int driver=DETECT,mode;
clrscr();
/* Two end-points of the line are accepted */
printf("Enter the co-ordinates of the first point (a b): ");
scanf("%d %d",&x1,&y1);
printf("Enter the co-ordinates of the second point (a b): ");
scanf("%d %d",&x2,&y2);
printf("<Press any key to continue>");
getch();
/* Initialize graphics mode */
initgraph(&driver,&mode,"F:\\TC\\BGI");
/* Line drawn with zero translation */
lineDDA(x1,y1,x2,y2,0,0);
/* Amount of translation, if any */
printf("Enter the amount of translation in x-direction: ");
scanf("%d",&xt);
printf("Enter the amount of translation in y-direction: ");
scanf("%d",&yt);
/* Translated line */
lineDDA(x1,y1,x2,y2,xt,yt);
getch();
closegraph();
}
void lineDDA(int x1,int y1,int x2,int y2,int xt,int yt)
{
float m,x,y,xd,yd;
x=x1;
y=y1;
/* If xt=0 and yt=0, then no translation. */
if(xt==0 && yt==0)
  setPixel(x,y);
/* Otherwise translate each point */
else
{
  translatePoint(x,y,xt,yt,&xd,&yd);
  setPixel(xd,yd);
}
/* For vertical lines */
if(x1==x2)
{
  while(y!=y2)
  {
     if(y2-y1>0)
        ++y;
     else
        --y;
     if(xt==0 && yt==0)
        setPixel(x,y);
     else
     {
        translatePoint(x,y,xt,yt,&xd,&yd);
        setPixel(xd,yd);
     }
  }
}
/* Non-vertical lines */
else
{
  m=(float)(y2-y1)/(x2-x1);
  /* When -1<=m<=1 we calculate the corresponding y's for unit intervals of x */
  if(m<=1 && m>=-1)
  {
     while(x!=x2)
     {
        /* To check whether the line is drawn from left to right, or the reverse */
        if(x2-x1>0)
        {
           y=y+m;
           ++x;
        }
        else
        {
           y=y-m;
           --x;
        }
        /* x and y is plotted, with or without translation */
        if(xt==0 && yt==0)
           setPixel(x,y);
        else
        {
           translatePoint(x,y,xt,yt,&xd,&yd);
           setPixel(xd,yd);
        }
     }
  }
  /* When m<-1 or m>1 we calculate the corresponding x's for unit intervals of y */
  else
  {
     while(y!=y2)
     {
        /* To check whether the line is drawn from bottom to top, or it's reverse */
        if((y2-y1)>0)
        {
           x=x+(1/m);
           ++y;
        }
        else
        {
           x=x-(1/m);
           --y;
        }
        /* x and y is plotted, with or without translation */
        if(xt==0 && yt==0)
           setPixel(x,y);
        else
        {
           translatePoint(x,y,xt,yt,&xd,&yd);
           setPixel(xd,yd);
        }
     }
  }
}
}
/* Translates the point (x,y) by xt and yt amount, and stores the new point in (xd,yd) */
void translatePoint(float x,float y,int xt,int yt,float *xd,float *yd)
{
int i,j;
float a[3][3],b[3],c[3];
a[0][0]=1; a[0][1]=0; a[0][2]=xt;
a[1][0]=0; a[1][1]=1; a[1][2]=yt;
a[2][0]=0; a[2][1]=0; a[2][2]=1;
b[0]=x;
b[1]=y;
b[2]=1;
for(i=0;i<3;++i)
{
  c[i]=0;
  for(j=0;j<3;++j)
  {
     c[i]+=a[i][j]*b[j];
  }
}
/* Translated point */
*xd=c[0]/c[2];
*yd=c[1]/c[2];
}
void setPixel(int x,int y)
{
putpixel(x,y,2);
}
Friday, August 17, 2007
Line Translation (DDA used here)
Subscribe to:
Post Comments (Atom)

No comments:
Post a Comment