Graphics Program
Graphics Program
ROTATE AN IMAGE
#include<stdio.h>
#include<math.h>
#include<graphics.h>
int ch=0,n=0,gd=DETECT,gm,rx,ry;
float a,m1x1,m1x2,m1x3,m2x1,m2x2,m2x3,m3x1,m3x2,m3x3;
void main()
{
struct
{
int x;
int y;
}
pts[10],rpts[10];
int i;
clrscr();
printf("\n\n Enter Num of Points to Draw an Object:");
scanf("%d",&n);
printf("Enter the %d set(x,y) Points to Draw an Object:\n",n);
for(i=0;i<n;i++)
{
printf("\n Enter %d x,y Points:",i+1);
scanf("%d%d",&pts[i].x,&pts[i].y);
}
initgraph(&gd,&gm,"C:\\TC\\BGI");
setbkcolor(WHITE);
setcolor(RED);
printf("Original Object:");
fillpoly(n,pts);
printf("\n Enter The Angle to Rotate the Object:");
scanf("%f",&a);
a=a*3.142/180;
printf("\n Enter The Reference Point(x,y)";
scanf("%d%d",&rx,&ry);
m1x1=cos(a);
m1x2=-(sin(a));
m1x3=rx*(1-cos(a))+ry*sin(a);
m2x1=sin(a);
m2x2=cos(a);
m2x3=ry*(1-cos(a))-rx*sin(a);
for(i=0;i<n;i++)
{
rpts[i].x=(int)((float)pts[i].x*m1x1+(float)pts[i].y*m1x2+m1x3);
rpts[i].y=(int)((float)pts[i].x*m2x1+(float)pts[i].y*m2x2+m2x3);
}
fillpoly(n,rpts);
printf("After Rotations:");
getch();
closegraph();
}
LAB PROGRAM – 1 / ROTATE AN IMAGE / OUTPUT /
Original Object
#include<graphics.h>
#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main(void)
{
int gd=DETECT,gm;
int xmax,ymax,i=0,j=0,x=10,y=10,k=0,t,l;
char stringl[100],strl[20]={"\0"};
clrscr();
initgraph(&gd,&gm,"C:\\TC\\BGI");
printf("Enter A Sentence :\n");
gets(stringl);
xmax=getmaxx();
ymax=getmaxy();
l=strlen(stringl);
cleardevice();
outtextxy(10,0,stringl);
setviewport(0,15,xmax,ymax-15,0);
while(k<=l)
{
if(stringl[k]!=' '&&stringl[k]!='\0')
{
strl[j++]=stringl[k];
}
else
{
strl[j++]='\0';
j=0;
t=strlen(strl);
for(i=0;i<=ymax-20;i+=10)
{
outtextxy(x,y+i,strl);
delay(100);
}
clearviewport();
x=x+t*7+10;
strset(strl,' ');
}
k++;
}
getch();
closegraph();
return(0);
}
LAB PROGRAM - 2 / DROP EACH WORD OF SENTENCE / OUTPUT /
Enter A Sentence :
students
students
students
students
students
students
students
students
students
students
we are all royal bca students
LAB PROGRAM - 3 / DRAWING A LINE USING DDA / INPUT /
#include<graphics.h>
#include<iostream.h>
#include<conio.h>
#include<math.h>
#include<stdio.h>
void main()
{
clrscr();
int gd=DETECT;
int gm;
float xin,yin,x,y;
initgraph(&gd,&gm," C:\\TC\\BGI");
int dx,dy,xa,xb,ya,yb,steps,k;
cout<<"\n\t\t OUTPUT\n";
cout<<"\n\t Enter The Value For xa:";
cin>>xa;
cout<<"\n\t Enter The Value For xb:";
cin>>xb;
cout<<"\n\t Enter The Value For ya:";
cin>>ya;
cout<<"\n\t Enter The Value For yb:";
cin>>yb;
dx=xb-xa;
dy=yb-ya;
if(abs(dx)>abs(dy))
steps=abs(dx);
else
steps=abs(dy);
xin=dx/steps;
yin=dy/steps;
x=xa;
y=ya;
putpixel(floor(x),floor(y),7);
for(k=1;k<=steps;k++)
{
x=x+xin;
y=y+yin;
putpixel(floor(x),floor(y),7);
}
getch();
closegraph();
}
LAB PROGRAM - 3 / DRAWING A LINE USING DDA / OUTPUT /
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<stdlib.h>
void draw_car()
{
circle(50,200,20);
circle(200,200,20);
line(80,200,170,200);
line(25,200,0,200);
line(0,200,0,160);
line(0,160,40,160);
line(40,160,70,130);
line(70,130,170,130);
line(170,130,200,160);
line(200,160,260,160);
line(260,200,225,200);
line(175,130,75,130);
line(260,160,260,200);
}
void main()
{
int gd=DETECT,gm,i,j;
void *bitmap;
detectgraph(&gd,&gm);
initgraph(&gd,&gm," C:\\TC\\BGI");
draw_car();
bitmap=malloc(imagesize(0,130,270,230));
getimage(0,130,270,230,bitmap);
for(i=0;!kbhit();i++)
{
if(i>500)i=0;
putimage(i,130,bitmap,OR_PUT);
setpalette(7,RED);
sound(100);
delay(8);
putimage(i,130,bitmap,XOR_PUT);
nosound();
}
closegraph();
getch();
}
LAB PROGRAM 4 / MOVING A CAR WITH SOUND EFFECT / OUTPUT /
LAB PROGRAM – 5 / BOUNCING A BALL / INPUT /
#include<iostream.h>
#include<conio.h>
#include<alloc.h>
#include<dos.h>
#include<graphics.h>
class ani
{
int gd,gm,area,x,y,ch,r;
int xdirm,ydirm;
int maxx,maxy;
public:
void *butt;
void pro();
void dis();
};
void ani:: pro()
{
cout<<"Enter The Radius:";
cin>>r;
gd=DETECT;gm=0;x=25;y=25;xdirm=1;ydirm=1;
initgraph(&gd,&gm," C:\\TC\\BGI");
setfillstyle(SOLID_FILL,BLUE);
circle(50,50,r);
floodfill(50,50,WHITE);
area=imagesize(r,r,75,75);
butt=malloc(area);
getimage(r,r,75,75,butt);
}
void ani::dis()
{
maxx=getmaxx();
maxy=getmaxy();
rectangle(0,20,maxx,maxy);
cout<<"\t"<<"ANIMATION";
while(1)
{
if(kbhit())
{
if(ch=='/r')
{
xdirm*=-1;
ydirm*=-1;
}
else
{
if(ch==27)
break;
}
return;
}
putimage(x,y,butt,XOR_PUT);
delay(10);
x=x+(xdirm*5);
y=y+(ydirm*2);
putimage(x,y,butt,XOR_PUT);
if((x>maxx-50)||(x<0))
{
delay(10);
xdirm*=-1;
}
if((y>maxy-50)||(y<20))
{
delay(10);
ydirm*=-1;
}
}
}
void main()
{
clrscr();
ani a;
a.pro();
a.dis();
}
ANIMATION
LAB PROGRAM - 6 / TESTING BOUNDS OF A POLYGON
WITH PIXEL / INPUT /
#include<graphics.h>
#include<stdio.h>
#include<conio.h>
int polysides=5,c;
int polyx[6]={540,590,570,510,490,540};
int polyy[6]={220,270,320,320,270,220};
int x,y;
int pointinpoly(void);
void drawpolygon(void);
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TC\\BGI");
drawpolygon();
outtextxy(200,50,"Point Is Inside or Outside of a Polygon");
printf("\n\n\n\n\n Enter the x & y Values:");
scanf("%d%d",&x,&y);
putpixel(x,y,10);
c=pointinpoly();
if(c==0)
{
printf("\n\n Point is Outside The Polygon");
}
else
{
printf("\n\n Point is Inside The Polygon");
}
getch();
}
int pointinpoly()
{
int i,j=polysides-1;
int oddnodes=0;
for(i=0;i<polysides;i++)
{
if((polyy[i]<y&&polyy[j]>=y)||(polyy[j]<y&&polyy[i]>=y))
{
if(polyx[i]+(y-polyy[i])/(polyy[j]-polyy[i])*(polyx[j]-polyx[i])<x)
{
oddnodes=1;
}
}
j=1;
}
return oddnodes;
}
void drawpolygon()
{
int i;
for(i=0;i<polysides;i++)
{
if(i==(polysides-1))
{
line (polyx[i],polyy[i],polyx[0],polyy[0]);
}
else
{
line(polyx[i],polyy[i],polyx[i+1],polyy[i+1]);
}
}
}
LAB PROGRAM - 6/TESTING BOUNDS OF A POLYGON
WITH PIXEL/OUTPUT /