Friday, August 17, 2007

Mid-point Ellipse Algorithm


/* Midpoint ellipse algorithm - Subhranath Chunder */

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>

void ellipseMidPoint(int,int,int,int);
void setOthers(int,int,int,int);
void setPixel(int,int);
int equ(float,float,int,int);

void main()
{
int x,y,rx,ry,driver=DETECT,mode;
clrscr();

printf("Enter the co-ordinate of the center of the elipse: ");
scanf("%d %d",&x,&y);
printf("Enter the radius along x-axis: ");
scanf("%d",&rx);
printf("Enter the radius along y-axis: ");
scanf("%d",&ry);
printf("<Press any key to continue>");

initgraph(&driver,&mode,"f:\\tc\\bgi");
ellipseMidPoint(x,y,rx,ry);
getch();
}

void ellipseMidPoint(int xCenter,int yCenter,int rx,int ry)
{
int x=0,y=ry,a,b;
setOthers(x,y,xCenter,yCenter);
while(pow(ry,2)*x<pow(rx,2)*y)
{
++x;
if( equ(x,y-(float)1/2,rx,ry)>=0)
--y;
setOthers(x,y,xCenter,yCenter);
}
while(y!=0)
{
--y;
if( equ(x+(float)1/2,y,rx,ry)<=0)
++x;
setOthers(x,y,xCenter,yCenter);
}
}

void setOthers(int x,int y,int xCenter,int yCenter)
{
setPixel(xCenter + x,yCenter + y);
setPixel(xCenter - x,yCenter + y);
setPixel(xCenter + x,yCenter - y);
setPixel(xCenter - x,yCenter - y);
}

int equ(float x,float y,int rx,int ry)
{
int res;
if( pow(ry,2)*pow(x,2)+pow(rx,2)*pow(y,2)-pow(rx,2)*pow(ry,2) == 0 )
res=0;
if( pow(ry,2)*pow(x,2)+pow(rx,2)*pow(y,2)-pow(rx,2)*pow(ry,2) < 0 )
res=-1;
if( pow(ry,2)*pow(x,2)+pow(rx,2)*pow(y,2)-pow(rx,2)*pow(ry,2) > 0 )
res=1;
return res;
}

void setPixel(int x,int y)
{
putpixel(x,y,2);
}





Mid-point Circle Scaling


/* WAP to generate a circle.
Write a function to scale the circle by sx amount in x-direction and sy amount in y-direction, where sx and sy are integers provided by the user.
Use matrix method. - Subhranath Chunder */

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>

void circleMidPoint(int,int,int,int,int);
int equ(float,float,int);
void setOthers(int,int,int,int,int,int);
void scalePoint(int,int,int,int,int*,int*);
void setPixel(int,int);

void main()
{
int x,y,radius,driver=DETECT,mode,sx,sy;
clrscr();

/* Mid-point and radius of the circle are accepted */
printf("Enter the co-ordinate of the centre: ");
scanf("%d %d",&x,&y);
printf("Enter the radius of the circle: ");
scanf("%d",&radius);
printf("<Press any key to continue>");

/* Graphics mode initialized */
initgraph(&driver,&mode,"F:\\tc\\bgi");

/* Circle drawn with no scaling */
circleMidPoint(x,y,radius,1,1);

/* Amount of scaling */
printf("Enter the amount of scaling in x-direction: ");
scanf("%d",&sx);
printf("Enter the amount of scaling in y-direction: ");
scanf("%d",&sy);

/* Translated circle is redrawn */
circleMidPoint(x,y,radius,sx,sy);
getch();
closegraph();
}

void circleMidPoint(int xCenter,int yCenter,int radius,int sx,int sy)
{
int x=0,y=radius;

/* Sets the points for all octants */
setOthers(x,y,xCenter,yCenter,sx,sy);

/* First octant is drawn */
while(x<y)
{
++x;

/* Checking whether y or y-1 is closer to the circle, by taking their mid-point */
if( equ(x,y-(float)1/2,radius) >=0 )
--y;

setOthers(x,y,xCenter,yCenter,sx,sy);
}

}

/* A function to find whether a given point is inside, outside, or on the circle */
int equ(float x,float y,int r)
{
int res;
if( pow(x,2)+pow(y,2)-pow(r,2) == 0)
res=0;
else if( pow(x,2)+pow(y,2)-pow(r,2) < 0)
res=-1;
else if( pow(x,2)+pow(y,2)-pow(r,2) > 0)
res=1;
return res;
}

/* A function to set all other points symmetric to the point (x,y) in all the octants */
void setOthers(int x,int y,int xCenter,int yCenter,int sx,int sy)
{
int xd,yd;

/* If sx==1 and sy==1 then no scaling is required. Otherwise, scale each point. */
if(sx==1 && sy==1)
{
setPixel(xCenter+x,yCenter+y);
setPixel(xCenter-x,yCenter+y);
setPixel(xCenter+x,yCenter-y);
setPixel(xCenter-x,yCenter-y);
setPixel(xCenter+y,yCenter+x);
setPixel(xCenter-y,yCenter+x);
setPixel(xCenter+y,yCenter-x);
setPixel(xCenter-y,yCenter-x);
}
else
{
scalePoint(xCenter+x,yCenter+y,sx,sy,&xd,&yd);
setPixel(xd,yd);
scalePoint(xCenter-x,yCenter+y,sx,sy,&xd,&yd);
setPixel(xd,yd);
scalePoint(xCenter+x,yCenter-y,sx,sy,&xd,&yd);
setPixel(xd,yd);
scalePoint(xCenter-x,yCenter-y,sx,sy,&xd,&yd);
setPixel(xd,yd);
scalePoint(xCenter+y,yCenter+x,sx,sy,&xd,&yd);
setPixel(xd,yd);
scalePoint(xCenter-y,yCenter+x,sx,sy,&xd,&yd);
setPixel(xd,yd);
scalePoint(xCenter+y,yCenter-x,sx,sy,&xd,&yd);
setPixel(xd,yd);
scalePoint(xCenter-y,yCenter-x,sx,sy,&xd,&yd);
setPixel(xd,yd);
}
}

/* Scales the point (x,y) by sx and sy amount, and stores the new point in (xd,yd) */
void scalePoint(int x,int y,int sx,int sy,int *xd,int *yd)
{
int i,j,a[3][3],b[3],c[3];

a[0][0]=sx; a[0][1]=0; a[0][2]=0;
a[1][0]=0; a[1][1]=sy; a[1][2]=0;
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];
}
}

/* Scaled point */
*xd=c[0]/c[2];
*yd=c[1]/c[2];
}

void setPixel(int x,int y)
{
putpixel(x,y,2);
}





Mid-point Circle Translation


/* Midpoint circle translation in homogeneous coordinate form - Subhranath Chunder */

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>

void circleMidPoint(int,int,int,int,int);
int equ(float,float,int);
void setOthers(int,int,int,int,int,int);
void translatePoint(int,int,int,int,int*,int*);
void setPixel(int,int);

void main()
{
int x,y,radius,driver=DETECT,mode,xt,yt;
clrscr();

/* Mid-point and radius of the circle are accepted */
printf("Enter the co-ordinate of the centre: ");
scanf("%d %d",&x,&y);
printf("Enter the radius of the circle: ");
scanf("%d",&radius);
printf("<Press any key to continue>");

/* Graphics mode initialized */
initgraph(&driver,&mode,"F:\\tc\\bgi");

/* Circle drawn with zero translation */
circleMidPoint(x,y,radius,0,0);

/* Amount of translation */
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 circle is redrawn */
circleMidPoint(x,y,radius,xt,yt);
getch();
closegraph();
}

void circleMidPoint(int xCenter,int yCenter,int radius,int xt,int yt)
{
int x=0,y=radius;

/* Sets the points for all octants */
setOthers(x,y,xCenter,yCenter,xt,yt);

/* First octant is drawn */
while(x<y)
{
++x;

/* Checking whether y or y-1 is closer to the circle, by taking their mid-point */
if( equ(x,y-(float)1/2,radius) >=0 )
--y;

setOthers(x,y,xCenter,yCenter,xt,yt);
}

}

/* A function to find whether a given point is inside, outside, or on the circle */
int equ(float x,float y,int r)
{
int res;
if( pow(x,2)+pow(y,2)-pow(r,2) == 0)
res=0;
else if( pow(x,2)+pow(y,2)-pow(r,2) < 0)
res=-1;
else if( pow(x,2)+pow(y,2)-pow(r,2) > 0)
res=1;
return res;
}

/* A function to set all other points symmetric to the point (x,y) in all the octants */
void setOthers(int x,int y,int xCenter,int yCenter,int xt,int yt)
{
int xd,yd;

/* If xt==0 and yt==0 then no translation is required. Otherwise, translate each point. */
if(xt==0 && yt==0)
{
setPixel(xCenter+x,yCenter+y);
setPixel(xCenter-x,yCenter+y);
setPixel(xCenter+x,yCenter-y);
setPixel(xCenter-x,yCenter-y);
setPixel(xCenter+y,yCenter+x);
setPixel(xCenter-y,yCenter+x);
setPixel(xCenter+y,yCenter-x);
setPixel(xCenter-y,yCenter-x);
}
else
{
translatePoint(xCenter+x,yCenter+y,xt,yt,&xd,&yd);
setPixel(xd,yd);
translatePoint(xCenter-x,yCenter+y,xt,yt,&xd,&yd);
setPixel(xd,yd);
translatePoint(xCenter+x,yCenter-y,xt,yt,&xd,&yd);
setPixel(xd,yd);
translatePoint(xCenter-x,yCenter-y,xt,yt,&xd,&yd);
setPixel(xd,yd);
translatePoint(xCenter+y,yCenter+x,xt,yt,&xd,&yd);
setPixel(xd,yd);
translatePoint(xCenter-y,yCenter+x,xt,yt,&xd,&yd);
setPixel(xd,yd);
translatePoint(xCenter+y,yCenter-x,xt,yt,&xd,&yd);
setPixel(xd,yd);
translatePoint(xCenter-y,yCenter-x,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(int x,int y,int xt,int yt,int *xd,int *yd)
{
int i,j,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);
}





Mid-point Circle Algorithm


/* Midpoint circle algorithm - Subhranath Chunder */

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>

void circleMidPoint(int,int,int);
int equ(float,float,int);
void setPixel(int,int);
void setOthers(int x,int y,int xCenter,int yCenter);

void main()
{
int x,y,radius,driver=DETECT,mode;
clrscr();

printf("Enter the co-ordinate of the centre: ");
scanf("%d %d",&x,&y);
printf("Enter the radius of the circle: ");
scanf("%d",&radius);
printf("<Press any key to continue>");

initgraph(&driver,&mode,"F:\\tc\\bgi");
circleMidPoint(x,y,radius);
getch();
closegraph();
}

void circleMidPoint(int xCenter,int yCenter,int radius)
{
int x=0,y=radius;
setOthers(x,y,xCenter,yCenter);
while(x<y)
{
++x;
if( equ(x,y-(float)1/2,radius) >=0 )
--y;
setOthers(x,y,xCenter,yCenter);
}
}

int equ(float x,float y,int r)
{
int res;
if( pow(x,2)+pow(y,2)-pow(r,2) == 0)
res=0;
else if( pow(x,2)+pow(y,2)-pow(r,2) < 0)
res=-1;
else if( pow(x,2)+pow(y,2)-pow(r,2) > 0)
res=1;
return res;
}

void setOthers(int x,int y,int xCenter,int yCenter)
{
setPixel(xCenter+x,yCenter+y);
setPixel(xCenter-x,yCenter+y);
setPixel(xCenter+x,yCenter-y);
setPixel(xCenter-x,yCenter-y);
setPixel(xCenter+y,yCenter+x);
setPixel(xCenter-y,yCenter+x);
setPixel(xCenter+y,yCenter-x);
setPixel(xCenter-y,yCenter-x);
}

void setPixel(int x,int y)
{
putpixel(x,y,2);
}





Line Translation (DDA used here)


/* 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);
}





Rectangle using Bresenham's line


/* WAP to draw a rectangle whose lower left corner, length and breadth are provided by the user.
Use Bresenham’s algorithm to draw the sides of the rectangle - Subhranath Chunder */

#include<stdio.h>
#include<conio.h>
#include<graphics.h>

void lineBres(int,int,int,int);
void setPixel(int,int);

void main()
{
int x,y,length,breadth;
int driver=DETECT,mode;

clrscr();
printf("Enter the co-ordinates of the lower left corner (a b): ");
scanf("%d %d",&x,&y);
printf("Enter the length of the rectangle: ");
scanf("%d",&length);
printf("Enter the breadth of the rectangle: ");
scanf("%d",&breadth);
printf("<Press any key to continue>");
getch();

initgraph(&driver,&mode,"F:\\TC\\BGI");
lineBres(x,y,x+length,y);
lineBres(x+length,y,x+length,y-breadth);
lineBres(x+length,y-breadth,x,y-breadth);
lineBres(x,y-breadth,x,y);
getch();
closegraph();
}

void lineBres(int x1,int y1,int x2,int y2)
{
float error,m;
int x,y;
x=x1;
y=y1;
if(x1==x2)
{
while(y!=y2)
{
if(y2-y1>0)
++y;
else
--y;
setPixel(x,y);
}
}
else
{
m=(float)(y2-y1)/(x2-x1);
error=0;
setPixel(x,y);
while(x!=x2)
{
error+=m;
if(error>.5)
{
if(x2-x1>0)
y+=1;
else
y-=1;
--error;
}
if(x2-x1>0)
++x;
else
--x;
setPixel(x,y);
}
}
}

void setPixel(int x,int y)
{
putpixel(x,y,2);
}





Bresenham's Line (for slope 0 to 1)

/* Bresenham's Line Implementation for 0<=m<=1 and drawing from left to right. - Subhranath Chunder */

#include<stdio.h>
#include<conio.h>
#include<graphics.h>

void lineBres(int,int,int,int);
void setPixel(int,int);

void main()
{
int x1,y1,x2,y2;
int driver=DETECT,mode;

clrscr();
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();

initgraph(&driver,&mode,"F:\\TC\\BGI");
lineBres(x1,y1,x2,y2);
getch();
closegraph();
}

void lineBres(int x1,int y1,int x2,int y2)
{
float error,m;
int x,y;
m=(float)(y2-y1)/(x2-x1);
error=0;
y=y1;
x=x1;
setPixel(x,y);
while(x<=x2)
{
error+=m;
if(error>.5)
{
y+=1;
--error;
}
++x;
setPixel(x,y);
}
}

void setPixel(int x,int y)
{
putpixel(x,y,2);
}





DDA Line Implementation

/* DDA Line Implementation - Subhranath Chunder */

#include<stdio.h>
#include<conio.h>
#include<graphics.h>

void lineDDA(int,int,int,int);
void setPixel(int,int);

void main()
{
int x1,y1,x2,y2;
int driver=DETECT,mode;

clrscr();
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();

initgraph(&driver,&mode,"F:\\TC\\BGI");
lineDDA(x1,y1,x2,y2);
getch();
closegraph();
}

void lineDDA(int x1,int y1,int x2,int y2)
{
float m,x,y;
x=x1;
y=y1;
setPixel(x,y);
if(x1==x2)
{
while(y!=y2)
{
if(y2-y1>0)
++y;
else
--y;
setPixel(x,y);
}
}
else
{
m=(float)(y2-y1)/(x2-x1);
if(m<=1 && m>=-1)
{
while(x!=x2)
{
if(x2-x1>0)
{
y=y+m;
++x;
}
else
{
y=y-m;
--x;
}
setPixel(x,y);
}
}
else
{
while(y!=y2)
{
if((y2-y1)>0)
{
x=x+(1/m);
++y;
}
else
{
x=x-(1/m);
--y;
}
setPixel(x,y);
}
}
}
}

void setPixel(int x,int y)
{
putpixel(x,y,2);
}