Professional Documents
Culture Documents
#include <graphics.h>
#include <stdio.h>
void draw_moving_car(void)
{
int i, j = 0, gd = DETECT, gm;
step4:function to initialize graphics mode
initgraph(&gd, &gm, "");
for (i = 0; i <= 420; i = i + 10)
{
Step5:Set color of car as red
setcolor(RED);
step6:Thease lines for bonnet and body of car
line(0 + i, 300, 210 + i, 300);
line(50 + i, 300, 75 + i, 270);
line(75 + i, 270, 150 + i, 270);
line(150 + i, 270, 165 + i, 300);
line(0 + i, 300, 0 + i, 330);
line(210 + i, 300, 210 + i, 330);
step7:For left wheel of car
setcolor(BLACK);
step13:Lines for bonnet and body of car
line(0 + i, 300, 210 + i, 300);
line(50 + i, 300, 75 + i, 270);
line(75 + i, 270, 150 + i, 270);
line(150 + i, 270, 165 + i, 300);
line(0 + i, 300, 0 + i, 330);
line(210 + i, 300, 210 + i, 330);
step14: For left wheel of car
circle(65 + i, 330, 15);
circle(65 + i, 330, 2);
step15: For right wheel of car
circle(145 + i, 330, 15);
circle(145 + i, 330, 2);
step16: Line left of left wheel
line(0 + i, 330, 50 + i, 330);
step17:Line middle of both wheel
line(80 + i, 330, 130 + i, 330);
step18:Line right of right wheel
line(210 + i, 330, 160 + i, 330);
}
getch();
closegraph();
}
Step19:Driver code
int main()
{
draw_moving_car();
return 0;
}
Step20:stop algorithm
Program:
#include<graphics.h>
#include<conio.h>
int main()
{
intgd=DETECT,gm, i, maxx, cy;
initgraph(&gd, &gm, "C:\\TC\\BGI");
setbkcolor(WHITE);
setcolor(RED);
maxx = getmaxx();
cy = getmaxy()/2;
for(i=0;i<maxx-140;i++)
{
cleardevice();
line(0+i,cy-20, 0+i, cy+15);
line(0+i, cy-20, 25+i, cy-20);
line(25+i, cy-20, 40+i, cy-70);
line(40+i, cy-70, 100+i, cy-70);
line(100+i, cy-70, 115+i, cy-20);
line(115+i, cy-20, 140+i, cy-20);
line(0+i, cy+15, 18+i, cy+15);
circle(28+i, cy+15, 10);
line(38+i, cy+15, 102+i, cy+15);
circle(112+i, cy+15,10);
line(122+i, cy+15 ,140+i,cy+15);
line(140+i, cy+15, 140+i, cy-20);
rectangle(50+i, cy-62, 90+i, cy-30);
setfillstyle(1,BLUE);
floodfill(5+i, cy-15, RED);
setfillstyle(1, LIGHTBLUE);
floodfill(52+i, cy-60, RED);
delay(10);
}
getch();
closegraph();
return 0;
}
Output:
Write a program to draw a pentagon using bresenham's line
drawing algorithm.
Algorithm:
1. Declare all variables including graphics variables and polygon
array.
2. Initialize all variables.
3. Initialize the graph while the path is set to the graphics driver.
4. Assign values to polygon array in pairs.
5. Use drawpoly( ) function to draw the polygon shape.
6. Close the graph.
Program:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
int main()
{
int gd=DETECT, gm;
drawpoly(6,pentagon);
outtextxy(330, 160, "Pentagon");
getch();
closegraph();
return 0;
}
Output:
write a program to draw a hexagon using generalized bresenham's
line drawing algorithm.
Algorithm:
1) move out: ε1=ε0+k*s
1a) decide where to move: ε1<=0.5, so we will move to the R hex
2) color hex (1,0), and increase ε once more: ε2=ε1+k*s. Now hex (1,0) is the current one
3) move out: ε3=ε2+k*s
3a) decide where to move: ε3>0.5, so we will move to the UR hex
4) color hex (1,1), and adjust ε accordingly: ε4=ε3-1.5. Now hex (1,1) is current, and ε is
negative (thus shown in red)
5) move out: ε5=ε4+k*s, ε is still negative (and will remain such till the final hex)
5a) decide where to move: ε5<=0.5, hence R hex is next
6) color hex (2,1), and increase ε once more: ε6=ε5+k*s. Now hex (2,1) is current
7) move out: ε7=ε6+k*s
7a) decide where to move: ε7<=0.5, hence R hex is next
8) color hex (3,1), and we are done
Program:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
int main()
fillpoly(7,hexagon);
getch();
closegraph();
return 0;
}
Output:
Writte a program to perform the following sequencial transformation
on a square.
1.rotate the square by an angle of 90 degree w.r.t origin.
Algorithm:
Initial co-ordinates of the object O = (Xold, Yold)
Initial angle of the object O with respect to origin = Φ
Rotation angle = θ
New co-ordinates of the object O after rotation = (Xnew, Ynew)
Program:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
int x1,y1,x2,y2,x3,y3,a,b;
void draw();
void rotate();
int main(void)
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TC\\BGI");
printf("Enter first co-ordinate value for triangle:");
scanf("%d%d",&x1,&y1);
printf("Enter second co-ordinatevalues for triangle:");
scanf("%d%d",&x2,&y2);
printf("Enter third co-ordinate valuesfor triangle:");
scanf("%d%d",&x3,&y3);
draw();
getch();
rotate();
getch();
return 0;
}
void draw()
{
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
}
void rotate()
{
int a1,a2,a3,b1,b2,b3;
float angle;
printf("Enter the rotation angle co-ordinates:");
scanf("%f",&angle);
cleardevice();
angle=(angle*3.14)/180;
a1=a+(x1-a)*cos(angle)-(y1-b)*sin(angle);
b1=b+(x1-a)*sin(angle)+(y2-b)*cos(angle);
a2=a+(x2-a)*cos(angle)-(y1-b)*sin(angle);
b2=b+(x2-a)*sin(angle)+(y2-b)*cos(angle);
a3=a+(x3-a)*cos(angle)-(y1-b)*sin(angle);
b3=b+(x3-a)*sin(angle)+(y2-b)*cos(angle);
printf("ROTATION");
printf("\n Changed coordinates\n");
printf("%d %d\n%d %d\n%d %d",a1,b1,a2,b2,a3,b3);
line(a1,b1,a2,b2);
line(a2,b2,a3,b3);
line(a3,b3,a1,b1);
}
Output:
To write a program to draw the various attributes of line, circle and ellipse.
Algorithm:
Step 1: Start the program.
Step 2: Initialize the variables.
Step 3: Call the initgraph() function
Step 4: Set color for the output primitives.
Step 5: Using Outtextxy() display the chosen particular primitives.
Step 6: Include the various attributes of line, circle and ellipse.
Step 7: close the graph and run the program.
Step 8: stop the program
Program:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<string.h>
void main()
{
char ch='y';
int gd=DETECT,gm,x1,y1,x2,y2,rad,sa,ea,xrad,yrad,i;
initgraph(&gd,&gm,"");
while(ch=='y')
{
cleardevice();
setbkcolor(9);
outtextxy(100,130,"Choose From The Following ");
outtextxy(100,150,"1. Line");
outtextxy(100,170,"2.Circle");
outtextxy(100,190,"3.Box");
outtextxy(100,210,"4.Arc");
outtextxy(100,230,"5.Ellipse");
outtextxy(100,250,"6.Rectangle");
outtextxy(100,270,"7.Exit");
ch=getch();
cleardevice();
switch(ch)
{
case '1':
line(100,200,300,400);
break;
case '2':
circle(200,200,100);
break;
case '3':
setfillstyle(5,4);
bar(100,300,200,100);
break;
case '4':
setfillstyle(5,4);
arc(200,200,100,300,100);
break;
case '5':
setfillstyle(5,4);
fillellipse(100,100,50,100);
break;
case '6':
settextstyle(DEFAULT_FONT,0,2);
outtextxy(120,140,"AMS COLLEGE ");
line(100,100,100,300);
line(300,300,100,300);
line(100,100,300,100);
line(300,100,300,300);
break;
case '7':
closegraph();
return;
}
ch='y';
getch();
}
}
OUTPUT:
Choose from the following
1.Line
2. Circle
3.Box
4.Arc
5.Ellipse
6.Rectangle
7.Exit
LINE
CIRCLE
ARC