//2D Rotation
#include
#include
#include
#include
void main()
{
int gd=DETECT,gm;
double ang,th,pi=3.14,x1,x2,x3,y1,y2,y3;
double a1=400,a2=350,a3=450,b1=100,b2=200,b3=200;
initgraph(&gd,&gm,"C:\\tc\\bgi");
clrscr();
line(a1,b1,a2,b2);
line(a1,b1,a3,b3);
line(a3,b3,a2,b2);
printf("Enter rotational angle :");
scanf("%lf",&ang);
th=ang*(pi/180);
x1=a1*cos(th)-b1*sin(th);
y1=a1*sin(th)+b1*cos(th);
x2=a2*cos(th)-b2*sin(th);
y2=a2*sin(th)+b2*cos(th);
x3=a3*cos(th)-b3*sin(th);
y3=a3*sin(th)+b3*cos(th);
line(x1,y1,x2,y2);
line(x1,y1,x3,y3);
line(x3,y3,x2,y2);
getch();
}
------------------------------------------------------------------------------------------------------------------------------
//2D Scaling
#include
#include
#include
#include
#include
int x1,y1,x2,y2,x3,y3,mx,my;
void draw();
void scale();
void main()
{
int gd=DETECT,gm;
int c;
initgraph(&gd,&gm," ");
printf("Enter the 1st point for the triangle:");
scanf("%d%d",&x1,&y1);
printf("Enter the 2nd point for the triangle:");
scanf("%d%d",&x2,&y2);
printf("Enter the 3rd point for the triangle:");
scanf("%d%d",&x3,&y3);
draw();
scale();
}
void draw()
{
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
}
void scale()
{
int x,y,a1,a2,a3,b1,b2,b3;
int mx,my;
printf("Enter the scalling coordinates");
scanf("%d%d",&x,&y);
mx=(x1+x2+x3)/3;
my=(y1+y2+y3)/3;
cleardevice();
a1=mx+(x1-mx)*x;
b1=my+(y1-my)*y;
a2=mx+(x2-mx)*x;
b2=my+(y2-my)*y;
a3=mx+(x3-mx)*x;
b3=my+(y3-my)*y;
line(a1,b1,a2,b2);
line(a2,b2,a3,b3);
line(a3,b3,a1,b1);
draw();
getch();
}
------------------------------------------------------------------------------------------------------------------------------
//2D Translation
#include
#include
#include
void main()
{
int gd=DETECT,gm;
int tx,ty;
initgraph(&gd,&gm,"C:\\tc\\bgi");
printf("Enter translation points (x,y) : ");
scanf("%d%d",&tx,&ty);
line(100,100,50,200);
line(100,100,150,200);
line(50,200,150,200);
line(100+tx,100+ty,50+tx,200+ty);
line(100+tx,100+ty,150+tx,200+ty);
line(50+tx,200+ty,150+tx,200+ty);
getch();
}
------------------------------------------------------------------------------------------------------------------------------
// 3D Translation:--
#include
#include
#include
void main()
{
int a[1][4],i,j,tx,ty,tz,k,b[4][4],c[1][4];
int left,top,right,bottom,gd=DETECT,gm;
clrscr();
initgraph(&gd,&gm,"c:\\tc\\bgi");
setfillstyle(XHATCH_FILL,225);
printf("\n Enter the left most co-ordinates for object(Left Top co-ordinates) :-");
scanf("%d %d",&left,&top);
printf("\n Enter the Right Bottom co-ordinates for object(Right Bottom co-ordi) :-");
scanf("%d %d",&right,&bottom);
bar3d(left,top,right,bottom,25,1);
i=0;
for(j=0;j<3;j++)
c[i][j]=0;
printf("\nEnter the Translating points(Tx,Ty,Tz): :->");
scanf("%d %d %d",&tx,&ty,&tz);
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
if(i==j)
b[i][j]=1;
else
b[i][j]=0;
}
}
b[3][0]=tx;
b[3][1]=ty;
b[3][2]=tz;
a[0][0]=left;
a[0][1]=top;
a[0][2]=1;
a[0][3]=1;
i=0;
for(j=0;j<4;j++)
{
for(k=0;k<4;k++)
c[i][j]=c[i][j]+a[i][k]*b[k][j];
if(j==0)
left=c[i][j];
if(j==1)
top=c[i][j];
c[i][j]=0;
}
a[0][0]=right;
a[0][1]=bottom;
a[0][2]=1;
a[0][3]=1;
i=0;
for(j=0;j<4;j++)
{
for(k=0;k<4;k++)
c[i][j]=c[i][j]+a[i][k]*b[k][j];
if(j==0)
right=c[i][j];
if(j==1)
bottom=c[i][j];
c[i][j]=0;
}
bar3d(left,top,right,bottom,25,1);
getch();
}
------------------------------------------------------------------------------------------------------------------------------
//Bezier Curve:--
#include
#include
#include
int gd,gm,maxx,maxy;
float a[4][2];
void line1(float x2, float y2)
{
line(a[0][0],a[0][1],x2,y2);
a[0][0]=x2;
a[0][1]=y2;
}
void bezier(float xb,float yb,float xc,float yc,float xd,float yd,int n)
{
float xab,yab,xbc,ybc,xcd,ycd;
float xabc,yabc,xbcd,ybcd;
float xabcd,yabcd;
if(n==0)
{
line1(xb,yb);
line1(xc,yc);
line1(xd,yd);
}
else
{
xab=(a[0][0]+xb)/2;
yab=(a[0][1]+yb)/2;
xbc=(xb+xc)/2;
ybc=(yb+yc)/2;
xcd=(xc+xd)/2;
ycd=(yc+yd)/2;
xabc=(xab+xbc)/2;
yabc=(yab+ybc)/2;
xbcd=(xbc+xcd)/2;
ybcd=(ybc+ycd)/2;
xabcd=(xabc+xbcd)/2;
yabcd=(yabc+ybcd)/2;
n=n-1;
bezier(xab,yab,xabc,yabc,xabcd,yabcd,n);
bezier(xbcd,ybcd,xcd,ycd,xd,yd,n);
}
}
void igraph()
{
detectgraph(&gd,&gm);
if(gd<0)
{
puts("cannot detect a graphics card");
exit(1);
}
initgraph(&gd,&gm,"c:\\tc\\bgi");
}
void main()
{
int i;
float temp1,temp2;
//clrscr();
igraph();
for(i=0;i<4;i++)
{
printf("enter (x,y)coordinates of point %d :->", i+1);
scanf("%f%f",&temp1,&temp2);
a[i][0]=temp1;
a[i][1]=temp2;
}
bezier(a[1][0],a[1][1],a[2][0],a[2][1],a[3][0],a[3][1],8);
getch();
closegraph();
}
------------------------------------------------------------------------------------------------------------------------------
//Boundry-Fill Algorithm
#include
#include
#include
void bf(int x,int y,int fc,int bc)
{
int ic;
ic=getpixel(x,y);
if((ic!=bc)&&(ic!=fc))
{
putpixel(x,y,fc);
bf(x+1,y,fc,bc);
bf(x-1,y,fc,bc);
bf(x,y+1,fc,bc);
bf(x,y-1,fc,bc);
}
}
void main()
{
int gd,gm;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:\\tc\\bgi");
rectangle(50,50,100,100);
bf(55,55,4,15);
getch();
closegraph();
}
------------------------------------------------------------------------------------------------------------------------------
//Bresenhams_circle
# include
# include
# include
# include
void main()
{
int gd=DETECT,gm;
int r,x,y,p,xc=320,yc=240;
initgraph(&gd,&gm,"C:\\TC\\BGI");
cleardevice();
printf("Enter the radius ");
scanf("%d",&r);
x=0;
y=r;
putpixel(xc+x,yc-y,1);
p=3-(2*r);
for(x=0;x<=y;x++)
{
if (p<0)
{
y=y;
p=(p+(4*x)+6);
}
else
{
y=y-1;
p=p+((4*(x-y)+10));
}
putpixel(xc+x,yc-y,1);
putpixel(xc-x,yc-y,2);
putpixel(xc+x,yc+y,3);
putpixel(xc-x,yc+y,4);
putpixel(xc+y,yc-x,5);
putpixel(xc-y,yc-x,6);
putpixel(xc+y,yc+x,7);
putpixel(xc-y,yc+x,8);
}
getch();
closegraph();
}
------------------------------------------------------------------------------------------------------------------------------
//Bresenhams_line
# include
# include
void main()
{
int dx,dy,x,y,p,x1,y1,x2,y2;
int gd,gm;
clrscr();
printf("\n\n\tEnter the co-ordinates of first point : ");
scanf("%d %d",&x1,&y1);
printf("\n\n\tEnter the co-ordinates of second point : ");
scanf("%d %d",&x2,&y2);
dx = (x2 - x1);
dy = (y2 - y1);
p = 2 * (dy) - (dx);
x = x1;
y = y1;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"e:\\tc\\bgi");
putpixel(x,y,WHITE);
while(x <= x2)
{
if(p < 0)
{
x=x+1;
y=y;
p = p + 2 * (dy);
}
else
{
x=x+1;
y=y+1;
p = p + 2 * (dy - dx);
}
putpixel(x,y,WHITE);
}
getch();
closegraph();
}
------------------------------------------------------------------------------------------------------------------------------
//Cohen-Sutherland Line Clipping Algorithm:
#include
#include
#include
int xwmin=100, ywmin=100, xwmax=300, ywmax=300,x,y;
float dx,dy,m;
void encode();
void main()
{
int gd=DETECT,gm,x1,y1,x2,y2,flag=0;
clrscr();
printf("Enter the coords of line\n");
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
initgraph(&gd,&gm,"C:\\tc\\bgi");
rectangle(xwmin,ywmin,xwmax,ywmax);
line(x1,y1,x2,y2);
cleardevice();
rectangle(xwmin,ywmin,xwmax,ywmax);
if((y1>ywmax) && (y2>ywmax))
flag=1;
if((y1
flag=1;
if((x1>xwmax) && (x2>xwmax))
flag=1;
if((x1
flag=1;
if(flag==0)
{
dy=y2-y1;
dx=x2-x1;
if(dx==0)
dx=1;
m=dy/dx;
x=x1;
y=y1;
encode();
moveto(x,y);
x=x2;
y=y2;
encode();
if(x>=xwmin && x<=xwmax && y>=ywmin && y<=ywmax)
lineto(x,y);
}
getch();
closegraph();
}
void encode()
{
if(x
{
y=y+(xwmin-x)*m;
x=xwmin;
}
if(x>xwmax)
{
y=y+(xwmax-x)*m;
x=xwmax;
}
if(y
{
if(m!=0)
x=x+(ywmin-y)/m;
else
x=x+(ywmin-y);
y=ywmin;
}
if(y>ywmax)
{
if(m!=0)
x=x+(ywmax-y)/m;
else
x=x+(ywmax-y);
y=ywmax;
}
}
/* OUTPUT:
Enter the coords of line
50 50 200 200 */
------------------------------------------------------------------------------------------------------------------------------
//DDA
#include
#include
#include
#include
#include
#include
void draw(int x1,int y1,int x2,int y2);
void main()
{
int x1,y1,x2,y2;
int gdriver=DETECT,gmode,gerror;
initgraph(&gdriver,&gmode,"c:\\tc\\bgi:");
printf("\n Enter the x and y value for starting point:\n");
scanf("%d%d",&x1,&y1);
printf("\n Enter the x and y value for ending point:\n");
scanf("%d%d",&x2,&y2);
printf("\n The Line is shown below: \n");
draw(x1,y1,x2,y2);
getch();
}
void draw(int x1,int y1,int x2,int y2)
{
float x,y,xinc,yinc,dx,dy;
int k;
int step;
dx=x2-x1;
dy=y2-y1;
if(abs(dx)>abs(dy))
step=abs(dx);
else
step=abs(dy);
xinc=dx/step;
yinc=dy/step;
x=x1;
y=y1;
putpixel(x,y,1);
for(k=1;k<=step;k++)
{
x=x+xinc;
y=y+yinc;
putpixel(x,y,2);
}
}
------------------------------------------------------------------------------------------------------------------------------
//Flood-Fill Algorithm
#include
#include
#include
void ff(int x,int y,int oc,int nc)
{
int ic;
ic=getpixel(x,y);
if(ic==oc)
{
putpixel(x,y,nc);
ff(x+1,y,oc,nc);
ff(x-1,y,oc,nc);
ff(x,y+1,oc,nc);
ff(x,y-1,nc,oc);
ff(x+1,y+1,oc,nc);
ff(x-1,y-1,oc,nc);
ff(x+1,y-1,oc,nc);
ff(x-1,y+1,oc,nc);
}
}
void main()
{
int gd,gm;
clrscr();
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:\\tc\\bgi");
rectangle(50,50,100,100);
ff(55,55,4,15);
getch();
closegraph();
}
-----------------------------------------------------------------------------------------------------------------------------
//MidPoint_circle
#include
#include
#include
void main()
{
int gd=DETECT,gm;
int x,y,r;
void Drawcircle(int,int,int);
printf("Enter the Mid points and Radious:");
scanf("%d%d%d",&x,&y,&r);
initgraph(&gd,&gm,"");
Drawcircle(x,y,r);
getch();
closegraph();
}
void Drawcircle(int x1,int y1,int r)
{
int x=0,y=r,p=1-r;
void cliplot(int,int,int,int);
cliplot(x1,y1,x,y);
while(x
{
x++;
if(p<0)
p+=2*x+1;
else
{
y--;
p+=2*(x-y)+1;
}
cliplot(x1,y1,x,y);
}
}
void cliplot(int xctr,int yctr,int x,int y)
{
putpixel(xctr +x,yctr +y,1);
putpixel(xctr -x,yctr +y,1);
putpixel(xctr +x,yctr -y,1);
putpixel(xctr -x,yctr -y,1);
putpixel(xctr +y,yctr +x,1);
putpixel(xctr -y,yctr +x,1);
putpixel(xctr +y,yctr -x,1);
putpixel(xctr -y,yctr -x,1);
getch();
}
------------------------------------------------------------------------------------------------------------------------------
//MidPoint_Ellipse
#include
#include
#include
#include
#include
#include
int main(void)
{
int gd=DETECT,gm;
int cenx,ceny;
float Pk,a,b,x,y;
clrscr();
printf("\n\n Enter 'a' and 'b': ");
scanf("%f%f",&a,&b);
initgraph(&gd,&gm,"c:\\tc\\bgi");
cenx=getmaxx()/2;
ceny=getmaxy()/2;
Pk=b*b-b*a*a+0.25*a*a;
x=0;
y=b;
putpixel(cenx+x,ceny+y,WHITE);
putpixel(cenx+x,ceny-y,WHITE);
putpixel(cenx-x,ceny+y,WHITE);
putpixel(cenx-x,ceny-y,WHITE);
while (2*x*b*b <= 2*y*a*a)
{
if (Pk<0)
{
x=x+1;
y=y;
Pk=Pk+2*x*b*b+3*b*b;
}
else
{
x=x+1;
y=y-1;
Pk=Pk+2*x*b*b+3*b*b-2*y*a*a+2*a*a;
}
putpixel(cenx+x,ceny+y,WHITE);
putpixel(cenx+x,ceny-y,WHITE);
putpixel(cenx-x,ceny+y,WHITE);
putpixel(cenx-x,ceny-y,WHITE);
delay(40);
}
Pk=(x+0.5)*(x+0.5)*b*b+(y-1)*(y-1)*a*a-a*a*b*b;
putpixel(cenx+x,ceny+y,WHITE);
putpixel(cenx+x,ceny-y,WHITE);
putpixel(cenx-x,ceny+y,WHITE);
putpixel(cenx-x,ceny-y,WHITE);
while (y>0)
{
if (Pk>0)
{
x=x;
y=y-1;
Pk=Pk-2*y*a*a+3*a*a;
}
else
{
x=x+1;
y=y-1;
Pk=Pk-2*y*a*a+3*a*a+2*x*b*b+2*b*b;
}
putpixel(cenx+x,ceny+y,WHITE);
putpixel(cenx+x,ceny-y,WHITE);
putpixel(cenx-x,ceny+y,WHITE);
putpixel(cenx-x,ceny-y,WHITE);
delay(40);
}
gotoxy(1,25);
printf ("\npress any key to exit.");
getch();
closegraph();
return 0;
}
------------------------------------------------------------------------------------------------------------------------------
//Scanline_fill_polygon
#include
#include
#include
main()
{
int n,i,j,k,gd,gm,dy,dx;
int x,y,temp;
int a[20][2],xi[20];
float slope[20];
clrscr();
printf("\n\n\tEnter the no. of edges of polygon : ");
scanf("%d",&n);
printf("\n\n\tEnter the cordinates of polygon :\n\n\n ");
for(i=0;i
{
printf("\tX%d Y%d : ",i,i);
scanf("%d %d",&a[i][0],&a[i][1]);
}
a[n][0]=a[0][0];
a[n][1]=a[0][1];
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:\\tc\\bgi");
/*- draw polygon -*/
for(i=0;i
{
line(a[i][0],a[i][1],a[i+1][0],a[i+1][1]);
}
getch();
for(i=0;i
{
dy=a[i+1][1]-a[i][1];
dx=a[i+1][0]-a[i][0];
if(dy==0) slope[i]=1.0;
if(dx==0) slope[i]=0.0;
if((dy!=0)&&(dx!=0)) /*- calculate inverse slope -*/
{
slope[i]=(float) dx/dy;
}
}
for(y=0;y< 480;y++)
{
k=0;
for(i=0;i
{
if( ((a[i][1]<=y)&&(a[i+1][1]>y))||
((a[i][1]>y)&&(a[i+1][1]<=y)))
{
xi[k]=(int)(a[i][0]+slope[i]*(y-a[i][1]));
k++;
}
}
for(j=0;j
for(i=0;i
{
if(xi[i]>xi[i+1])
{
temp=xi[i];
xi[i]=xi[i+1];
xi[i+1]=temp;
}
}
setcolor(35);
for(i=0;i
{
line(xi[i],y,xi[i+1]+1,y);
getch();
}
}
}
//best of luck
No comments:
Post a Comment