You are on page 1of 13

Write a Program To Moving A String Along X-axis

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
void main()
{
int gd=DETECT,gm;
int y,x,I;
char z1[100],z2[100];
initgraph(&gd,&gm,””);
outtextxy(100,100,”enter the initial position :”);
scanf(“%d%d”,&x,&y);
outtextxy(110,110,”enter the first string to be moved :”);
scanf(“%s”,z1);
outtextxy(120,120,”enter the second string to be moved :”);
scanf(“%s”,z2);
for(i=x;i<590-x;i++
{
delay(100);
clrscr();
outtextxy(x+1,y,z1);
outtextxy(590-x-I,y,z2);
}
getch();
closegraph();
}
Write a Program To Print a Line Using DDA Algorithm

#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
voidlinedda(int xa,int ya,int xb,int yb)
{
int dx,dy,steps,k;
float xinc,ynics,x,y;
dx=xb-xa;
dy=yb-ya;
if(abs(dx),abs(dy))
step=abs(dx);
else
steps=abs(dy);
xinc=1.0*dx/steps;
yinc=1.0*dx/steps;
x=xa;
y=ya;
putpixel(int(x),int(y),3);
for(k=1;k<=steps;k++)
{
x+=xinc;
y+=yinc;
putpixel(int(x),int(y),3);
}
}
void main()
{
int x1,y1,x2,y2;
clrscr();
cout<<”Enter the x co-ordinates of first point =”;
cin>>x1;
cout<<”Enter the y co-ordinates of first point =”;
cin>>y1;
cout<<”Enter the x co-ordinates of 2nd point =”;
cin>>x2;
cout<<”Enter the y co-ordinates of 2nd point =”;
cin>>y2;
int gd=DETECT,gm;
initgraph(&gd,&gm,”c:\\tc\\bgi”);
linedda(x1,y1,x2,y2);
getch();
closegraph();
}
OUTPUT

Enter the x co-ordinates of first point =


Enter the y co-ordinates of first point =
Enter the x co-ordinates of 2nd point=
Enter the y co-ordinates of 2nd point =
Write a Program To Scaling of Objects

#include<iostream.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
#include<stdlib.h>
#include<dos.h>
int x();
int y();
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,”c;\\tc\\bgi”);
int
mx,my,th,I,j,sx,sy,h,k,ka,s,a[3][4],b[3][3],c[3][4];
int tmp,tmp1;
cout<<”enter the co-ordinates of A=”;
cin>>a[0][0]>>a[1][0];
cout<<”enter the co-ordinates of B=”;
cin>>a[0][1]>>a[1][1];
cout<<”enter the co-ordinates of C=”;
cin>>a[0][2]>>a[1][2];
cout<<”enter the co-ordinates of D=”;
cin>>a[0][3]>>a[1][3];
a[2][0]=a[2][1]=a[2][3]=a[2][2]=1;
line(0,my/2,mx,my/2);
line(mx/2,0,mx/2,my);
line(x([0][0]),y(a[1][0],x(a[0][1]),y(a[1][1])); line(x([0][0]),y(a[1][0],x(a[0][3]),y(a[1][3]));
line(x([0][1]),y(a[1][1],x(a[0][2]),y(a[1][2]));
line(x([0][3]),y(a[1][3],x(a[0][2]),y(a[1][2]));
cout<<”enter the value of scaling factor sx,sy=”;
cin>>sx>>sy;
cout<<”enter the value of h and k=”;
cin>>h>>k;
tmp=(-h*sx)+h;
b[0][0]=sx;
b[0][1]=0;
b[0][2]=tmp;
b[1][0]=0;b[1][1]=sy;b[1][2]=temp1;
b[2][0]=0;b[2][2]=0;b[2][2]=1;
for(i=0;i<3;i++)
{
cout<<”\n\n”;
{
for(k=0;k<4;k++)
{
c[i][j]=b[i][j]*a[j][k];
}}}
getch();
closegraph();
line(0,my/2,mx,my/2);
line(mx/2,0,mx/2,my);
line(x([0][0]),y(a[1][0],x(a[0][1]),y(a[1][1])); line(x([0][0]),y(a[1][0],x(a[0][3]),y(a[1][3]));
line(x([0][1]),y(a[1][1],x(a[0][2]),y(a[1][2]));
line(x([0][3]),y(a[1][3],x(a[0][2]),y(a[1][2]));
}
int x(int x)
{
return(get max x()/2+x);
}
Int y(int y)
{
return(get max y()/2-y);
}
Write a Program To Print a Circle Using Bresenham

#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#include<stdlib.h>
#include<dos.h>
void main()
{
Clrscr();
int gdrivers=DETECT,gmodes;
initgraph(&gdrivers,&gmode,”c:\\tc\\bgi”);
float x,y,dx,dy,xend,yend,p,x1,y1,r;
cout<<”enter the value of x=”;
cin>>x;
cout<<”enter the value of y=”;
cin>>y;
cout<<”enter the value of radius=”;
cin>>r;
x=0;
y=r;
p=1-r;
while(x,y)
{
x=x+1;
if(p<0)
{
p=p+2*x+1;
}
Else
{
y=y-1;
p=p+2*(x-y)+1;
}
delay(50);
sound(1000+(x+y));
putpixel(x1+y,y1+x,1);
putpixel(x1+x,y1+y,2);
putpixel(x1+x,y1-y,3);
putpixel(x1-y,y1-x,4);
putpixel(x1-y,y1-x,5);
putpixel(x1-x,y1-y,6);
putpixel(x1-x,y1+y,7);
putpixel(x1-y,y1+x,8);
nosound();
}
getch();
closegraph();
}

OUTPUT
Enter the value of x
Enter the value of y
Enter the value of radius
Write a Program To Print a line using Bresenham

#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#include<stdlib.h>
#include<dos.h>
void main()
{
Clrscr();
int gd=DETECT,gm;
initgraph(&gd,&gm,”c:\\tc\\bgi”);
float dx,dy,x,y,xend,yend,p,x1,y1,x2,y2;
cout<<”enter the value of x1=”;
cin>>x1;
cout<<”enter the value of y1=”;
cin>>y1;
cout<<”enter the value of x2=”;
cin>>x2;
cout<<”enter the value of y2=”;
cin>>y2;
dx=abs(x1-x2);
dy=abs(y1-y2);
if(dx>=dy)
{
p=2*dy-dx;
if(x1>x2)
{
x=x2;
y=y2;
xend=x1;
yend=y1;
}
else
{
x=x1;
y=y1;
xend=x2;
yend=y2;
}
putpixel(x,y,3)
for(int i=0;i<dx;i++)
{
x=x+1;
if(p<0)
{
p=p+2*dy;
}
else
{
If(yend<y)
{
y=y-1;
}
p=p+2*(dy-dx);
}
putpixel(x,y,3);
}}
else
{
if(dx<dy)
{
p=2*dx-dy;
if(y1>y2)
{
x=x2;
y=y2;
xend=x1;
yend=y1;
}
else
{
x=x1;
y=y1;
xend=x2;
yend=y2;
putpixel(x,y,3);
for(int i=0;i<dy;i++)
{
y=y+1;
if(p<0)
{
p=p+2*dx;
}
else
{
if(xend<x)
{
x=x+1;
}
p=p+2*(dx-dy);
}
putpixel(x,y,3);
}}}}
getch();
closegraph();
}

OUTPUT

Enter the value of x1=


Enter the value of y1=
Enter the value of x2=
Enter the value of y2=
Write a Program To Print Rectangle Using Bresenham

#include<iostream.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
#include<stdlib.h>
#include<dos.h>
void linebres(int xa,int ya,int xb,int yb)
{
it dx,dy,x,y,xend,yend,p,flag;
float m;
d=abs(xa-xb);
d=abs(ya-yb0;
i(dx&&abs(m=1.0*(ya-yb)/(xa-xb))<1)
fag=0;
ese
fag=1;
i(flag)
{
p2*dy-dx;
i(xa>xb)
{
x=xb;
y=yb;
xend=xa;
}
else
{
x=xa;
y=ya;
xend=xb
}
putpixel(x,y,7);
do
(
x++
if(p<0)
p+=2*dy;
else
{
if(m<0)
y--;
else
y++;
p+2*(dy-dx);
}
putpixel(x,y,7);
}
while(x<xend);
}
else
{
p=2*dx-dy;
if(ya>yb)
{
y=yb;
x=xb;
yend=ya;
}
else
{
y=ya;
x=xa;
yend=yb;
}
putpixel(x,y,7);
do
{
y++;
if(p<0)
p+=2*dx;
else
{
If(m<0)
x--;
else
x++;
p+=2*(dx-dy);
}
putpixel(x,y,7);
}
while(y<yend);
}}}
void main(void)
{
int x1,y1,x2,y2,x3,y3,x4,y4;
clrscr();
cout<<”enter the co-ordinates of a Rectangle ABCD\n”;
cout<<”enter the value of x1,y1 of point A=”;
cin>>x1>>y1;
cout<<”enter the value of x2.y2 of point B=”;
cin>>x2>>y2;
cout<<”enter the value of x3.y3 of point C=”;
cin>>x3>>y3;
cout<<”enter the value of x4.y4 of point D=”;
cin>>x4>>y4;
if(x1-x2==x4-x4-x3&&y1-y2==y4-y3&&
(x1==x2&&y2=y3||x2==x3&&y1==y2||(y1-y2)*
(y2-y3)/(x1-x2)/(x2-x3)==-1.0))
{
int gd=DETECT,gm;
initgraph(&gd,&gm,’c:\\tc\\bgi”);
linebres(x1,y1,x2,y2);
linebres(x2,y2,x3,y3);
linebres(x3,y3,x4,y4);
linebres(x4,y3,x1,y1);
}
else
cout<<”These are not the co-ordinates of a rectangle”;
getch();
closegraph();
}

OUTPUT

Enter the value of x1,y1 of point A=


Enter the value of x1,y1 of point B=
Enter the value of x1,y1 of point C=
Enter the value of x1,y1 of point D=

You might also like