You are on page 1of 13

Program for creating House shape

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
intgd=DETECT,gm,x=25,y=25,font=10;
initgraph(&gd,&gm,"C:\\turboC3\\BGI");
for(font=0;font<=4;font++)
{
settextstyle(font,HORIZ_DIR,font+1);// sets font type, font direction, size
setcolor(font+1); // sets color for text.
outtextxy(x,y,"text with different fonts"); // prints message on screen at (x,y)
y=y+25;
}
for(font=0;font<=2;font++)
{
settextstyle(font,VERT_DIR,font+2);
setcolor(font+1);
x=250;
y=100;
outtextxy(x,y,"text in vertical direction");
y=y+25;
}
getch();
closegraph();
}

Program for creating simple car shape:


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
void main()
{
intgd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TurboC3\\BGI");
cleardevice();
line( 150, 100, 242, 100);
ellipse(242, 105, 0, 90, 10, 5);
line(150, 100, 120, 150);
line(252, 105, 280, 150);
line(100, 150, 320, 150);
line(100, 150, 100, 200);
line(320, 150, 320, 200);
line(100, 200, 110, 200);
line( 320, 200, 310, 200);
arc(130, 200, 0, 180, 20);
arc( 290, 200, 0, 180, 20);
line( 270, 200, 150, 200);

circle(130, 200, 17);


circle(290, 200, 17);
getch();
}
Program for creating fish:
#include<stdlib.h>
#include<conio.h>
#include<dos.h>
#include<graphics.h>
#include<ctype.h>
void main()
{
intgd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
cleardevice();
ellipse(520,200,30,330,90,30);
circle(450,193,3);
line(430,200,450,200);
line(597,185,630,170);
line(597,215,630,227);
line(630,170,630,227);
line(597,200,630,200);
line(597,192,630,187);
line(597,207,630,213);
line(500,190,540,150);
line(530,190,540,150);
getch();
}
Program for creating man object:
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
void main()
{
intgd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
setcolor(9);
circle(150,150,35);
line(150,185,150,300);
line(150,200,120,230);
line(150,200,180,230);
line(150,300,120,330);
line(150,300,180,330);
outtextxy(230,350,"HI, This is Computer Graphics");
getch();
}

Attributes
#include <graphics.h>
#include <conio.h>
main()
{
intgd=DETECT,gm,points[]={320,150,420,300,250,300,320,150};
initgraph(&gd, &gm, "C:\\TurboC3\\BGI");
drawpoly(4, points);
getch();
closegraph();
return 0;
}
Coloring the Pictures
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
void flood(int,int,int,int);
void main()
{
intgd,gm=DETECT;
clrscr();
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
rectangle(50,50,100,100);
flood(55,55,9,0);
getch();
}
void flood(intx,int y, intfill_col, intold_col)
{
if(getpixel(x,y)==old_col)
{
delay(10);
putpixel(x,y,fill_col);
flood(x+1,y,fill_col,old_col);
flood(x-1,y,fill_col,old_col);
flood(x,y+1,fill_col,old_col);
flood(x,y-1,fill_col,old_col);
}
}
concept of Boundary fill algorithm
Program for 4-connected Boundary fill Algorithm:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>

#include<dos.h>
voidboundary_fill(int x, int y, intfcolor, intbcolor)
{
if ((getpixel(x, y) != bcolor) && (getpixel(x, y) != fcolor))
{ delay(10);
putpixel(x, y, fcolor);
boundary_fill(x + 1, y, fcolor, bcolor);
boundary_fill(x - 1, y, fcolor, bcolor);
boundary_fill(x, y + 1, fcolor, bcolor);
boundary_fill(x, y - 1, fcolor, bcolor);
}
}
void main()
{
int x, y, fcolor, bcolor;
intgd=DETECT,gm;
initgraph(&gd, &gm, "C:\\TurboC3\\BGI");
printf("Enter the seed point (x,y) : ");
scanf("%d%d", &x, &y);
printf("Enter boundary color : ");
scanf("%d", &bcolor);
printf("Enter new color : ");
scanf("%d", &fcolor);
circle(100,200,45);
boundary_fill(x,y,fcolor,bcolor);
getch();
}
Floodfill: floodfill function is used to fill an enclosed area of an object. Current fill
pattern and fill color is
used to fill the area.(x, y) is any point on the screen if (x,y) lies inside the area then
inside will be filled
otherwise outside will be filled,border specifies the color of boundary of area. To
change fill pattern and
fill color use setfillstyle.
Syntax: floodfill(x,y,boundarycolor);
Program illustrating the use of Floodfill function:
#include<graphics.h>
#include<conio.h>
main()
{
intgd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TurboC3\\BGI");
setcolor(RED);
circle(100,100,50);
floodfill(100,100,RED);
getch();
closegraph();
return 0;

}
Setfillstyle: setfillstyle function sets the current fill pattern and fill color.
Syntax: setfillstyle(pattern,color);
1) Here, pattern refers to type of filling. The pattern value may be integer value or
its corresponding
pattern name in CAPS. The patterns available are listed below:
EMPTY_FILL----0,
SOLID_FILL----1,
LINE_FILL-----2,
LTSLASH_FILL--3,
SLASH_FILL----4,
BKSLASH_FILL--5,
LTBKSLASH_FILL-6,
HATCH_FILL-----7,
XHATCH_FILL----8,
INTERLEAVE_FILL-9,
WIDE_DOT_FILL--10,
CLOSE_DOT_FILL-11,
USER_FILL------12.[ integer value from 0 to 12 refers to its corresponding pattern
name]
2) Color refers to fill color .The below table shows color names and their equivalent
integer values.
Program illustrating the use of setfillstyle:
#include<graphics.h>
#include<conio.h>
main()
{
intgd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TurboC3\\BGI");
setfillstyle(XHATCH_FILL, RED);
circle(100, 100, 50);
floodfill(100, 100, WHITE);
getch();
closegraph();
}

Write C-programs for designing simple animations using transformations?


Aim: To develop programs for making simple animations using transformations like
rotation, scaling and
translation. The simple animations are given below:
i. Circle moving from left to right and vice versa

ii. Man object moving


iii. Wind mill rotation
iv. Man walking
v. Simple animation of football goal
Description:
For moving any object, we incrementally calculate the object coordinates and
redraw the
picture to give a feel of animation by using for loop.
Suppose if we want to move a circle from left to right means, we have to shift the
position of
circle along x-direction continuously in regular intervals.
The below programs illustrate the movement of objects by using for loop and also
using
transformations like rotation, translation etc.
For windmill rotation, we use 2D rotation concept and formulas.
i. Program for moving circle in different directions
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
#include<dos.h>
void main()
{
intgd=DETECT,gm,i;
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
//for moving circle from left to right,the following loop works
for(i=50;i<=getmaxx();i++)
{
setcolor(3);
setfillstyle(SOLID_FILL,9);
circle(50+i,50,50);
floodfill(52+i,52,3);
delay(20);
cleardevice();
}
//for moving circle from right to left, the following loop works
for(i=getmaxy();i>=0;i--)
Experiment-11

Simple animations using transformations


{
setcolor(3);
setfillstyle(SOLID_FILL,9);
circle(i,50,50);
floodfill(i+2,52,3);
delay(20);
cleardevice();
}
//for moving circle from top to bottom,the following loop works
for(i=50;i<=getmaxy();i++)
{

setcolor(3);
setfillstyle(SOLID_FILL,9);
circle(50,i,50);
floodfill(52,i+2,3);
delay(20);
cleardevice();
}
//for moving circle from bottom to top,the following loop works
for(i=getmaxy();i>=0;i--)
{
setcolor(3);
setfillstyle(SOLID_FILL,9);
circle(50,i,50);
floodfill(52,i+2,3);
delay(20);
cleardevice();
}
//for moving circle in diagonal direction,the following loop works
for(i=50;i<=getmaxx();i++)
{
setcolor(3);
setfillstyle(SOLID_FILL,9);
circle(i,i,50);
floodfill(i+2,i+2,3);
delay(20);
cleardevice();
}
//for moving circle in reverse diagonal direction,the following loop works
for(i=getmaxx();i>=0;i--)
{
setcolor(3);
setfillstyle(SOLID_FILL,9);
circle(i,i,50);
floodfill(i+2,i+2,3);
delay(20);
cleardevice();
}
getch();
}
Output for moving circle:
ii. program for man object moving:
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
#include<dos.h>
void main()
{
intgd=DETECT,gm,i;

initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
//creation of man object by using circle,line.
setcolor(7);
setfillstyle(SOLID_FILL,10);
circle(50,50,30); // drawing head
floodfill(52,52,7);
setcolor(13);
line(50,80,50,200); //drawing body
line(50,110,20,140); //left hand
line(50,110,80,140); //right hand
line(50,200,20,230); //left leg
line(50,200,80,230); //right leg
// for loop for moving man
for(i=50;i<=getmaxx();i++)
{
setcolor(7);
setfillstyle(SOLID_FILL,10);
circle(i,50,30); // drawing head
floodfill(i+2,52,7);
setcolor(13);
line(i,80,i,200); //drawing body
line(i,110,i-30,140); //left hand
line(i,110,i+30,140); //right hand
line(i,200,i-30,230); //left leg
line(i,200,i+30,230); //right leg
cleardevice();
delay(10);
}
//doing simple animation using translation
for(i=50;i<=getmaxx()/2;i++)
{
setcolor(7);
setfillstyle(SOLID_FILL,10);
circle(i,50,30); // drawing head
floodfill(i+2,52,7);
setcolor(13);
line(i,80,i,200); //drawing body
line(i,110,i-30,140); //left hand
line(i,110,i+30,140); //right hand
line(i,200,i-30,230); //left leg
line(i,200,i+30,230); //right leg
cleardevice(); delay(10);
}
getch();
}
output for man moving:
iii.program for windmill rotation:
#include<stdio.h>

#include<graphics.h>
#include<math.h>
void wind(float x[7],float y[7]);
void main()
{
intgd=DETECT,gm;
float x[7],y[7],maxx,maxy,xw1,yw1,xw2,yw2;
float theta=30;
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
maxx=getmaxx();
maxy=getmaxy();
x[0]=maxx/2;
y[0]=maxy/2;
x[1]=y[4]=x[2]=-90;
y[6]=y[5]=y[1]=60;
y[2]=35;
y[3]=-100;
x[4]=20;
x[3]=0;
x[5]=90;
x[6]=65;
theta=theta*22/7/180;
while(kbhit()==0)
{
wind(x,y);
xw1=cos(theta)*x[1]+sin(theta)*y[1];
yw1=-sin(theta)*x[1]+cos(theta)*y[1];
xw2=cos(theta)*x[2]+sin(theta)*y[2];
yw2=-sin(theta)*x[2]+cos(theta)*y[2];
x[1]=xw1;
y[1]=yw1;
x[2]=xw2;
y[2]=yw2;
xw1=cos(theta)*x[3]+sin(theta)*y[3];
yw1=-sin(theta)*x[3]+cos(theta)*y[3];
xw2=cos(theta)*x[4]+sin(theta)*y[4];
yw2=-sin(theta)*x[4]+cos(theta)*y[4];
x[3]=xw1;
y[3]=yw1;
x[4]=xw2;
y[4]=yw2;
xw1=cos(theta)*x[5]+sin(theta)*y[5];
yw1=-sin(theta)*x[5]+cos(theta)*y[5];
xw2=cos(theta)*x[6]+sin(theta)*y[6];
yw2=-sin(theta)*x[6]+cos(theta)*y[6];
x[5]=xw1;
y[5]=yw1;
x[6]=xw2;
y[6]=yw2;
67

delay(50);
cleardevice();
}
closegraph();
}
void wind(float x[7],float y[7])
{
cleardevice();
line(x[0],y[0],x[0]-50,y[0]+200);
line(x[0],y[0],x[0]+50,y[0]+200);
line(x[0]-60,y[0]+200,x[0]+60,y[0]+200);
line(x[0],y[0],x[0]+x[1],y[0]-y[1]);
line(x[0],y[0],x[0]+x[2],y[0]-y[2]);
line(x[0]+x[1],y[0]-y[1],x[0]+x[2],y[0]-y[2]);
line(x[0],y[0],x[0]+x[3],y[0]-y[3]);
line(x[0],y[0],x[0]+x[4],y[0]-y[4]);
line(x[0]+x[3],y[0]-y[3],x[0]+x[4],y[0]-y[4]);
line(x[0],y[0],x[0]+x[5],y[0]-y[5]);
line(x[0],y[0],x[0]+x[6],y[0]-y[6]);
line(x[0]+x[5],y[0]-y[5],x[0]+x[6],y[0]-y[6]);
}
iv. program for man walking:
#include<stdio.h>
#include<dos.h>
#include<conio.h>
#include<graphics.h>
#include<stdlib.h>
void main()
{
intgd = DETECT, gm = DETECT, c = -200, i = 0, x = 40, l = 15, h = 15, ht = 0;
initgraph(&gd, &gm, "C:\\TurboC3\\BGI");
cleardevice();
setcolor(BROWN);
line(0, 201, 600, 201);
cont:
while (!kbhit()) {
setcolor(4);
ellipse(x, 100, 0, 180, 50, 30);
line(x - 50, 100, x + 50, 100);
line(x, 100, x, 150);
circle(x - 20, 115, 15);
line(x - 20, 130, x - 20, 175);
line(x - 20, 175, x - 20 - l, 200);
line(x - 20, 175, x - 20 + l, 200);
line(x - 20, 140, x, 150);
line(x - 20, 140, x - 20 - h, 160);
setcolor(0);
delay(50);

ellipse(x, 100, 0, 180, 50, 30);


line(x - 50, 100, x + 50, 100);
line(x, 100, x, 150);
circle(x - 20, 115, 15);
line(x - 20, 130, x - 20, 175);
line(x - 20, 175, x - 20 - l, 200);
line(x - 20, 175, x - 20 + l, 200);
line(x - 20, 140, x, 150);
line(x - 20, 140, x - 20 - h, 160);
line(x + 50, 100, x + 50, 200);
x++;
l--;
if (l == -15)
l = 15;
if (ht == 1)
h++;
else
h--;
if (h == 15)
ht = 0;
else if (h == -15)
ht = 1;
69
}
if (getch() == ' ') {
while (!kbhit());
getch();
gotocont;
}
}
vi. program for simple animation of football goal:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
void main()
{
int x=0,gd=DETECT,gm,points[]={0,220,1600,220,1600,900,0,900,0,220};
float y=0;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
setcolor(MAGENTA);
settextstyle(6,HORIZ_DIR,6);
outtextxy(200,250,"Hi");
delay(1000);
cleardevice();
settextstyle(7,VERT_DIR,1);
outtextxy(200,50,"GET READY FOR ANIMATION");
delay(1000);

cleardevice();
setcolor(GREEN);
setfillstyle(SOLID_FILL,GREEN);
fillpoly(5,points);
setcolor(WHITE);
circle(100,100,25);
delay(1000);
line(100,125,100,185);
delay(1000);
line(100,135,125,170);
delay(1000);
line(100,135,75,170);
delay(1000);
line(100,185,125,220);
delay(1000);
line(100,185,75,220);
delay(1000);
setcolor(RED);
setfillstyle(SOLID_FILL,RED);
fillellipse(135+x,210-y,10,10);
for(x=0;x<50;x++)
{
setcolor(WHITE);
line(100,185,75+x,220-y);
delay(100);
setcolor(BLACK);
line(100,185,75+x,220-y);
y=y+0.25;
}
setcolor(WHITE);
line(100,185,125,220);
line(100,185,75,220);
for(x=0,y=0;y<100;x++)
{
setcolor(RED);
setfillstyle(SOLID_FILL,RED);
fillellipse(135+x,210-y,10,10);
delay(10);
setcolor(GREEN);
setfillstyle(SOLID_FILL,GREEN);
fillpoly(5,points);
setcolor(BLACK);
setfillstyle(SOLID_FILL,BLACK);
fillellipse(135+x,210-y,10,10);
y=y+0.5;
}
for(;x<490;x++)
{
setcolor(RED);
setfillstyle(SOLID_FILL,RED);

fillellipse(135+x,y,10,10);
delay(10);
setcolor(BLACK);
setfillstyle(SOLID_FILL,BLACK);
fillellipse(135+x,y,10,10);
y=y+0.25;
}
setcolor(RED);
setfillstyle(SOLID_FILL,RED);
fillellipse(135+x,y,10,10);
delay(2000);
cleardevice();
setbkcolor(CYAN);
settextstyle(3,HORIZ_DIR,10);
outtextxy(200,80,"GOAL");
getch();
closegraph();
}

You might also like