0% found this document useful (0 votes)
12 views

graphics program coding

Graphics programs
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

graphics program coding

Graphics programs
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

1)ROTATION OF A TRIANGLE

#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
#include<dos.h>
#include<graphics.h>
#include<string.h>
void rotate(int *,int *,float,int,int);
void translate(int *,int *,int,int);
void main()
{
float theta;
int driver,mode,color,maxcolor,ch,x;
char answer;
int x1,y1,x2,y2,x3,y3,tx,ty;
float refx,refy;
detectgraph(&driver,&mode);
initgraph(&driver,&mode,"f:\\tc\\pr1");
do
{
x1=350,y1=300,x2=450,y2=300,x3=350,y3=200;
refx=350,refy=300;
cleardevice();
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
outtextxy(200,30,"MAIN MENU");
outtextxy(220,50,"1.ROTATION");
outtextxy(220,70,"2.EXIT");
gotoxy(1,1);
printf("\n\n enter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\n angle of rotation:");
scanf("%f",&theta);
rotate(&x1,&y1,theta,refx,refy);
rotate(&x2,&y2,theta,refx,refy);
rotate(&x3,&y3,theta,refx,refy);
break;
case2:
closegraph();
exit(0);
break;
}
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
outtextxy(80,180,"MOORE TRANSCATION (Y/N)");
answer=getch();
}while(answer=='y'||answer=='y');
closegraph();
}
void rotate(int *xnew,int *ynew,float angle,int refx,int refy)
{
int *xold,*yold;
translate(xnew,ynew,-refx,-refy);
*xold=*xnew;
*yold=*ynew;
angle=angle * 3.14159/18;
*xnew=*xold*cos(angle) - *yold*sin(angle);
*ynew=*yold*cos(angle) + *xold*sin(angle);
translate(xnew,ynew,refx,refy);
}
void translate(int *xnew,int *ynew,int tx,int ty)
{
*xnew=*xnew + tx;
*ynew=*ynew + ty;
}
2) DROP EACH WORD OF SENTENCE

#include<graphics.h>
#include<alloc.h>
#include<string.h>
#include<stdio.h>
void main()
{
char c,*buff,a[10][10];
int x,e,d,y,i=0,j=0;
int gd=DETECT,gm,q=30,l,area,p=8;
initgraph(&gd,&gm,"f:\\tc\\pro2.c");
setlinestyle(3,15,2);
rectangle(3,15,635,470);
settextstyle(2,HORIZ_DIR,4);
setcolor(BROWN);
outtextxy(200,3,"DROP A SENTENCE WORD BY WORD");
printf("\n");
setbkcolor(LIGHTBLUE);
settextstyle(3,HORIZ_DIR,2);
e=textheight("a");
d=textwidth("a");
while((c=getchar())!='\n')
{
if(c==' ')
{
a[i][j]='\0';
j=0;
i++;
}
else
{
a[i][j]=c;
j++;
}
}
for(x=0;x<=i;x++)
{
outtextxy(p,q,a[x]);
l=strlen(a[x]);
area=imagesize(p,q,p+(l*d+3),q+e+10);
buff=malloc(area);
getimage(p,q,p+(l*d+3),q+e+10,buff);
do
{
putimage(p,q,buff,XOR_PUT);
putimage(p,q,buff,COPY_PUT);
q=q+l;
delay(0);
}while(q<435);
putimage(p,q,buff,XOR_PUT);
putimage(p,q,buff,COPY_PUT);
p=p+(l*d+3);
q=30;
}
getch();
closegraph();
restorecrtmode();
}
3) DRAWING A LINE USING DDA ALGORITHM

#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
#include<graphics.h>
void LINEDDA(float,float,float,float);
main()
{
int drive=DETECT,mode,color;
float x1,x2,y1,y2;
clrscr();
printf("\n enter the value for x1,x2,y1,y2:");
scanf("%f %f %f %f",&x1,&x2,&y1,&y2);
initgraph(&drive,&mode,"f:\\tc\\pr3.c");
LINEDDA(x1,x2,y1,y2);
getch();
closegraph();
return 0;
}
void LINEDDA(float x1,float y1,float x2,float y2)
{
int steps,i;
int color;
float x,y,dx,dy;
steps=abs(x2-x1);
if(steps<abs(y2-y1))
steps=abs(y2-y1);
dx=(x2-x1)/steps;
dy=(y2-y1)/steps;
x=x1;
y=y1;
color=random(getmaxcolor());
putpixel(x,y,color);
for(i=1;i<=steps;i++)
{
x=x+dx;
y=y+dy;
color=random(getmaxcolor());
putpixel(x,y,color);
}
}
4) ANIMATION OF A CAR

# include<graphics.h>
#include<alloc.h>
void main()
{
int gd=DETECT,gm,area,x=25,y=25;
int ch,xdirn=1,maxx,maxy;
char *buff;
initgraph(&gd,&gm,"f:\\tc||pr4.c");
setcolor(WHITE);
setfillstyle(SOLID_FILL,YELLOW);
circle(200,176,WHITE);
circle(100,176,WHITE);
floodfill(200,176,WHITE);
rectangle(50,50,250,150);
rectangle(250,100,300,150);
area=imagesize(50,50,350,210);
buff=malloc(area);
getimage(50,50,350,210,buff);
while(1)
{
if(kbhit())
{
ch=getch();
if(ch=='\n')
{
xdirn*=-1;
}
else
{
if(ch==27)
break;
}
}
putimage(x,y,buff,COPY_PUT);
putimage(x,y,buff,XOR_PUT);
x=x+(xdirn*8);
sound(30);
delay(0);
nosound();
}
getch();
closegraph();
restorecrtmode();
}
5) BOUNCING A BALL

#include<conio.h>
#include<alloc.h>
#include<graphics.h>
#include<dos.h>
void main()
{
int gd=DETECT,gm,area,x=25,y=25,xdirn=1,ydirn=1,ch;
int maxx,maxy;
char *buff;
initgraph(&gd,&gm,"f:\\tc\\pr5.c");
setcolor(WHITE);
setfillstyle(SOLID_FILL,YELLOW);
circle(50,50,25);
floodfill(50,50,WHITE);
area=imagesize(25,25,75,75);
buff=malloc(area);
getimage(25,25,75,75,buff);
maxx=getmaxx();
maxy=getmaxy();
rectangle(0,20,maxx,maxy);
outtextxy(250,10,"animation");
while(1)
{
if(kbhit())
{
ch=getch();
if(ch=='\r')
{
xdirn*=-1;
ydirn*=-1;
}
else
{
if(ch==27)
break;
}
}
putimage(x,y,buff,XOR_PUT);
delay(0);
x=x+(xdirn*5);
y=y+(ydirn*2);
putimage( x,y,buff,XOR_PUT);
if(x>maxx-50 || x<0)
{
sound(50);
delay(0);
nosound();
xdirn*=-1;
}
if(y>maxy-50 || y<0)
{
sound(50);
delay(0);
nosound();
ydirn*=-1;
}
}
getch();
closegraph();
restorecrtmode();
}
6) TEST A GIVEN PIXEL

#include<conio.h>
#include<stdio.h>
#include<graphics.h>
#include<stdlib.h>
void main()
{
int gdrive=DETECT,gmode;
int i,j,maxx,maxy;
int x,y,flag=0,x1=100,y1=100,x2=200,y2=230;
clrscr();
initgraph(&gdrive,&gmode,"f:\\tc\\pr6");
maxx=getmaxx();
maxy=getmaxy();
rectangle(x1,y1,x2,y2);
printf("\n enter the co-ordinates:");
scanf("%d %d ",&x,&y);
if((x>maxx) || (y>maxy))
printf("co-ordinate are out of sum");
else
{
for(i=x1;i<=x2;i++)
if((x==i)&&(y==y1))
flag=1;
for(i=y1;i<=y2;i++)
if((x==x1)&&(y==y2))
flag=1;
for(i=x1;i<=x2;i++)
if(( x==i)&&(y==y2))
flag=1;
for(i=y1;i<=y2;i++)
if((x==x2)&&(y==i))
flag=1;
for(i=x1+1;i<=x2-1;i++)
for(j=y1+1;j<=y2-1;j++)
if((x==i)&&(y==j))
{
flag=2;
break;
}
if(flag==1)
printf("\n pixel is on the polygon");
else
{
if(flag==2)
printf("\n pixel is inside the polygon");
else
printf("\n pixel is outside the polygon");
}
putpixel(x,y,RED);
}
getch();
}

You might also like