Q1. Write a Program to draw a line.
Program :
/*C graphics program to draw a line.*/
#include <graphics.h>
#include <conio.h>
main(){
int gd = DETECT, gm;
//init graphics
initgraph(&gd, &gm, "C:/TURBOC3/BGI");
/*
if you are using turboc2 use below line to init graphics:
initgraph(&gd, &gm, "C:/TC/BGI");
*/
//draw a line
/*
line() function description
parameter left to right
x1: 100
y1: 100
x2: 200
y2: 100
*/
line(100,100,200,100); //will draw a horizontal line
line(10,10,200,10); //will draw another horizonatl line
getch();
closegraph();
return 0;
}
Q2. Write the program to draw a line using DDA Algorithm.
Program :-
#include <graphics.h>
#include <stdio.h>
#include <math.h>
#include <dos.h>
void main( )
float x,y,x1,y1,x2,y2,dx,dy,step;
int i,gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
printf("Enter the value of x1 and y1 : ");
scanf("%f%f",&x1,&y1);
printf("Enter the value of x2 and y2: ");
scanf("%f%f",&x2,&y2);
dx=abs(x2-x1);
dy=abs(y2-y1);
if(dx>=dy)
step=dx;
else
step=dy;
dx=dx/step;
dy=dy/step;
x=x1;
y=y1;
i=1;
while(i<=step)
putpixel(x,y,5);
x=x+dx;
y=y+dy;
i=i+1;
delay(100);
closegraph();
}
Q3. Write the program to draw a line using Bresenham’s Algorithm.
Program :-
#include<stdio.h>
#include<graphics.h>
void drawline(int x0, int y0, int x1, int y1) {
int dx, dy, p, x, y;
dx=x1-x0;
dy=y1-y0;
x=x0;
y=y0;
p=2*dy-dx;
while(x<x1)
if(p>=0)
putpixel(x,y,7);
y=y+1;
p=p+2*dy-2*dx;
else
putpixel(x,y,7);
p=p+2*dy;
x=x+1;
}
int main()
int gdriver=DETECT, gmode, error, x0, y0, x1, y1;
initgraph(&gdriver, &gmode, "c:\\turboc3\\bgi");
printf("Enter co-ordinates of first point: ");
scanf("%d%d", &x0, &y0);
printf("Enter co-ordinates of second point: ");
scanf("%d%d", &x1, &y1);
drawline(x0, y0, x1, y1);
return 0;
Q4. Write the program to draw a line using Midpoint Algorithm.
Program :-
//Program for Midpoint Line Drawing Algorithm in C
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void midPoint(int X1, int Y1, int X2, int Y2)
int dx = X2 - X1;
int dy = Y2 - Y1;
if(dy<=dx){
int d = dy - (dx/2);
int x = X1, y = Y1;
printf("%d%dn", x, y);
while (x < X2)
x++;
if (d < 0)
d = d + dy;
else
d += (dy - dx);
y++;
printf("%d%dn", x, y);
else if(dx<dy)
int d = dx - (dy/2);
int x = X1, y = Y1;
printf("%d%dn", x, y);
while (y < Y2)
{
y++;
if (d < 0)
d = d + dx;
else
d += (dx - dy);
x++;
printf("%d%dn", x, y);
putpixel(x,y,15);
}int main()
int gd = DETECT, gm;
initgraph(&gd, &gm, "c:\turboc3\bgi");
int X1 = 100, Y1 = 100, X2 = 110, Y2 = 112;
printf("nnnMidpoint Line Drawing Algorithmn");
midPoint(X1, Y1, X2, Y2);
getch();
return 0;
}
Q5. Write the program to draw a circle using Bresenham’s Algorithm.
Program:
//C program Code for Bresenham Circle Drawing Algorithm
#include <stdio.h>
#include <dos.h>
#include <graphics.h>
// Function for print circle
void drawCircle(int xc, int yc, int x, int y)
putpixel(xc+x, yc+y, 15);
putpixel(xc-x, yc+y, 15);
putpixel(xc+x, yc-y, 15);
putpixel(xc-x, yc-y, 15);
putpixel(xc+y, yc+x, 15);
putpixel(xc-y, yc+x, 15);
putpixel(xc+y, yc-x, 15);
putpixel(xc-y, yc-x, 15);
}
int main()
int xc = 200, yc = 200, r = 70,d,x,y;
int gd = DETECT, gm;
initgraph(&gd, &gm, "c:\turboc3\bgi"); // initialize graph
printf("\n\n Bresenham Circle Drawing Algorithm Example in C Graphics\n\n");
x = 0, y = r;
d = 3 - 2 * r;
drawCircle(xc, yc, x, y);
while (y >= x)
x++;
if (d > 0)
y--;
d = d + 4 * (x - y) + 10;
else
d = d + 4 * x + 6;
drawCircle(xc, yc, x, y);
delay(70);
getch();
return 0;
}
Q6. Write the program to draw the circle using Mid-point Algorithm.
Program:
#include<stdio.h>
#include<graphics.h>
void drawcircle(int x0, int y0, int radius)
int x = radius;
int y = 0;
int err = 0;
while (x >= y)
putpixel(x0 + x, y0 + y, 7);
putpixel(x0 + y, y0 + x, 7);
putpixel(x0 - y, y0 + x, 7);
putpixel(x0 - x, y0 + y, 7);
putpixel(x0 - x, y0 - y, 7);
putpixel(x0 - y, y0 - x, 7);
putpixel(x0 + y, y0 - x, 7);
putpixel(x0 + x, y0 - y, 7);
if (err <= 0)
y += 1;
err += 2*y + 1;
if (err > 0)
x -= 1;
err -= 2*x + 1;
int main()
int gdriver=DETECT, gmode, error, x, y, r;
initgraph(&gdriver, &gmode, "c:\\turboc3\\bgi");
printf("Enter radius of circle: ");
scanf("%d", &r);
printf("Enter co-ordinates of center(x and y): ");
scanf("%d%d", &x, &y);
drawcircle(x, y, r);
return 0;
Q7. Write the program to draw a Ellipse using Midpoint algorithm.
Program :
#include<stdio.h>
#include<graphics.h>
void main(){
long x,y,x_center,y_center;
long a_sqr,b_sqr, fx,fy, d,a,b,tmp1,tmp2;
int g_driver=DETECT,g_mode;
clrscr();
initgraph(&g_driver,&g_mode,"C:\\TURBOC3\\BGI");
printf("********* MID POINT ELLIPSE ALGORITHM *********");
printf("\n\n Enter coordinate x and y = ");
scanf("%ld%ld",&x_center,&y_center);
printf("\n Now enter constants a and b = ");
scanf("%ld%ld",&a,&b);
x=0;
y=b;
a_sqr=a*a;
b_sqr=b*b;
fx=2*b_sqr*x;
fy=2*a_sqr*y;
d=b_sqr-(a_sqr*b)+(a_sqr*0.25);
do
putpixel(x_center+x,y_center+y,1);
putpixel(x_center-x,y_center-y,1);
putpixel(x_center+x,y_center-y,1);
putpixel(x_center-x,y_center+y,1);
if(d<0)
d=d+fx+b_sqr;
else
y=y-1;
d=d+fx+-fy+b_sqr;
fy=fy-(2*a_sqr);
x=x+1;
fx=fx+(2*b_sqr);
delay(10);
while(fx<fy);
tmp1=(x+0.5)*(x+0.5);
tmp2=(y-1)*(y-1);
d=b_sqr*tmp1+a_sqr*tmp2-(a_sqr*b_sqr);
do
putpixel(x_center+x,y_center+y,1);
putpixel(x_center-x,y_center-y,1);
putpixel(x_center+x,y_center-y,1);
putpixel(x_center-x,y_center+y,1);
if(d>=0)
d=d-fy+a_sqr;
else
x=x+1;
d=d+fx-fy+a_sqr;
fx=fx+(2*b_sqr);
}
y=y-1;
fy=fy-(2*a_sqr);
while(y>0);
getch();
closegraph();
Q8. Write a program for 2D translation of an object.
Program:
#include<iostream>
#include<graphics.h>
#include<math.h>
using namespace std;
int main()
{
int gd=DETECT,gm,s;
initgraph(&gd,&gm,(char*)"");
cout<<"1.Translation\n2.Rotation\n3.Scaling\n4.Reflection\n5.Shearing "<<endl;
cout<<"Selection:";
cin>>s;
switch(s)
{
case 1:
{ int x1=200,y1=150,x2=300,y2=250;
int tx=50,ty=50;
cout<<"Rectangle before translation"<<endl;
setcolor(3);
rectangle(x1,y1,x2,y2);
setcolor(4);
cout<<"Rectangle after translation"<<endl;
rectangle(x1+tx,y1+ty,x2+tx,y2+ty);
getch();
break;
}
case 2:
{ long x1=200,y1=200,x2=300,y2=300;
double a;
cout<<"Rectangle with rotation"<<endl;
setcolor(3);
rectangle(x1,y1,x2,y2);
cout<<"Angle of rotation:";
cin>>a;
a=(a*3.14)/180;
long xr=x1+((x2-x1)*cos(a)-(y2-y1)*sin(a));
long yr=y1+((x2-x1)*sin(a)+(y2-y1)*cos(a));
setcolor(2);
rectangle(x1,y1,xr,yr);
getch();
break;}
case 3:
{
int x1=30,y1=30,x2=70,y2=70,y=2,x=2;
cout<<"Before scaling"<<endl;
setcolor(3);
rectangle(x1,y1,x2,y2);
cout<<"After scaling"<<endl;
setcolor(10);
rectangle(x1*x,y1*y,x2*x,y2*y);
getch();
break;}
case 4:
{
int x1=200,y1=300,x2=500,y2=300,x3=350,y3=400;
cout<<"triangle before reflection"<<endl;
setcolor(3);
line(x1,y1,x2,y2);
line(x1,y1,x3,y3);
line(x2,y2,x3,y3);cout<<"triangle after reflection"<<endl;
setcolor(5);
line(x1,-y1+500,x2,-y2+500);
line(x1,-y1+500,x3,-y3+500);
line(x2,-y2+500,x3,-y3+500);
getch();
break;}
case 5:
{
int x1=400,y1=100,x2=600,y2=100,x3=400,y3=200,x4=600,y4=200,shx=2;
cout<<"Before shearing of rectangle"<<endl;
setcolor(3);
line(x1,y1,x2,y2);
line(x1,y1,x3,y3);
line(x3,y3,x4,y4);
line(x2,y2,x4,y4);
cout<<"After shearing of rectangle"<<endl;
x1=x1+shx*y1;
x2=x2+shx*y2;
x3=x3+shx*y3;
x4=x4+shx*y4;
setcolor(13);
line(x1,y1,x2,y2);
line(x1,y1,x3,y3);
line(x3,y3,x4,y4);
line(x2,y2,x4,y4);getch();}default:
{
cout<<"Invalid Selection"<<endl;
break;
}
}closegraph();
return 0;
}
9. Write a program for 2D Scaling of an object.
Program :-
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main(){
int x,y,x1,y1,x2,y2;
int scl_fctr_x,scl_fctr_y;
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
printf("\t\t\t********** Scaling ***********\n");
printf("\n\t\t\t Please enter first coordinate of Triangle = ");
scanf("%d %d",&x,&y);
printf("\n\t\t\t Please enter second coordinate of Triangle = ");
scanf("%d %d",&x1,&y1);
printf("\n\t\t\t Please enter third coordinate of Triangle = ");
scanf("%d %d",&x2,&y2);
line(x,y,x1,y1);
line(x1,y1,x2,y2);
line(x2,y2,x,y);
printf("\n\t\t\t Now Enter Scaling factor x and y = ");
scanf("%d %d",&scl_fctr_x,&scl_fctr_y);
x = x* scl_fctr_x;
x1 = x1* scl_fctr_x;
x2 = x2* scl_fctr_x;
y = y* scl_fctr_y;
y1 = y1* scl_fctr_y;
y2= y2 * scl_fctr_y ;
line(x,y,x1,y1);
line(x1,y1,x2,y2);
line(x2,y2,x,y);
getch();
closegraph();
Output:
Q10: Write a program to window to viewport mapping.
Program:-
#include<graphics.h>
#include<conio.h>
#include<stdio.h>
void main ()
int W_xmax, W_ymax, W_xmin, W_ymin;
int V_xmax, V_ymax, V_xmin, V_ymin;
float sx, sy;
int x, x1, x2, y, y1, y2;
int gr = DETECT, gm;
initgraph (&gr, &gm, "C:\\TURBOC3\\BGI");
printf ("\n****** Window to Viewport ***********\n");
printf ("Enter the coordinates for triangle \n x and y = ");
scanf ("%d %d", &x, &y);
printf ("\n x1 and y1 = ");
scanf ("%d %d", &x1, &y1);
printf ("\n x2 and y2 = ");
scanf ("%d %d", &x2, &y2);
printf ("Please enter Window coordinates \n First enter XMax, YMax =");
scanf ("%d %d", &W_xmax, &W_ymax);
printf ("\n Now, enter XMin, YMin =");
scanf ("%d %d", &W_xmin, &W_ymin);
cleardevice ();
delay (50);
//Window
rectangle (W_xmin, W_ymin, W_xmax, W_ymax);
outtextxy (W_xmin, W_ymin - 10, "Window");
//drawing a triangle
line (x, y, x1, y1);
line (x1, y1, x2, y2);
line (x2, y2, x, y);
// viewport
V_xmin = 300;
V_ymin = 30;
V_xmax = 550;
V_ymax = 350;
rectangle (V_xmin, V_ymin, V_xmax, V_ymax);
outtextxy (V_xmin, V_ymin - 10, "Viewport");
// calculatng Sx and Sy
sx = (float) (V_xmax - V_xmin) / (W_xmax - W_xmin);
sy = (float) (V_ymax - V_ymin) / (W_ymax - W_ymin);
x = V_xmin + (float) ((x - W_xmin) * sx);
x1 = V_xmin + (float) ((x1 - W_xmin) * sx);
x2 = V_xmin + (float) ((x2 - W_xmin) * sx);
y = V_ymin + (float) ((y - W_ymin) * sy);
y1 = V_ymin + (float) ((y1 - W_ymin) * sy);
y2 = V_ymin + (float) ((y2 - W_ymin) * sy);
// drawing triangle
line (x, y, x1, y1);
line (x1, y1, x2, y2);
line (x2, y2, x, y);
getch ();
closegraph ();}
Output:-
Q11. Write a Program to clipping a line.
Program:-
#include<graphics.h>
#include<conio.h>
#include<stdio.h>
#include<math.h>
void main()
int rcode_begin[4]={0,0,0,0},rcode_end[4]={0,0,0,0},region_code[4];
int W_xmax,W_ymax,W_xmin,W_ymin,flag=0;
float slope;
int x,y,x1,y1,i, xc,yc;
int gr=DETECT,gm;
initgraph(&gr,&gm,"C:\\TURBOC3\\BGI");
printf("\n****** Cohen Sutherlsnd Line Clipping algorithm ***********");
printf("\n Now, enter XMin, YMin =");
scanf("%d %d",&W_xmin,&W_ymin);
printf("\n First enter XMax, YMax =");
scanf("%d %d",&W_xmax,&W_ymax);
printf("\n Please enter intial point x and y= ");
scanf("%d %d",&x,&y);
printf("\n Now, enter final point x1 and y1= ");
scanf("%d %d",&x1,&y1);
cleardevice();
rectangle(W_xmin,W_ymin,W_xmax,W_ymax);
line(x,y,x1,y1);
line(0,0,600,0);
line(0,0,0,600);
if(y>W_ymax) {
rcode_begin[0]=1; // Top
flag=1 ;
if(y<W_ymin) {
rcode_begin[1]=1; // Bottom
flag=1;
if(x>W_xmax) {
rcode_begin[2]=1; // Right
flag=1;
if(x<W_xmin) {
rcode_begin[3]=1; //Left
flag=1;
//end point of Line
if(y1>W_ymax){
rcode_end[0]=1; // Top
flag=1;
if(y1<W_ymin) {
rcode_end[1]=1; // Bottom
flag=1;
if(x1>W_xmax){
rcode_end[2]=1; // Right
flag=1;
if(x1<W_xmin){
rcode_end[3]=1; //Left
flag=1;
if(flag==0)
printf("No need of clipping as it is already in window");
}
flag=1;
for(i=0;i<4;i++){
region_code[i]= rcode_begin[i] && rcode_end[i] ;
if(region_code[i]==1)
flag=0;
if(flag==0)
printf("\n Line is completely outside the window");
else{
slope=(float)(y1-y)/(x1-x);
if(rcode_begin[2]==0 && rcode_begin[3]==1) //left
y=y+(float) (W_xmin-x)*slope ;
x=W_xmin;
if(rcode_begin[2]==1 && rcode_begin[3]==0) // right
y=y+(float) (W_xmax-x)*slope ;
x=W_xmax;
if(rcode_begin[0]==1 && rcode_begin[1]==0) // top
x=x+(float) (W_ymax-y)/slope ;
y=W_ymax;
if(rcode_begin[0]==0 && rcode_begin[1]==1) // bottom
x=x+(float) (W_ymin-y)/slope ;
y=W_ymin;
// end points
if(rcode_end[2]==0 && rcode_end[3]==1) //left
y1=y1+(float) (W_xmin-x1)*slope ;
x1=W_xmin;
if(rcode_end[2]==1 && rcode_end[3]==0) // right
y1=y1+(float) (W_xmax-x1)*slope ;
x1=W_xmax;
if(rcode_end[0]==1 && rcode_end[1]==0) // top
x1=x1+(float) (W_ymax-y1)/slope ;
y1=W_ymax;
}
if(rcode_end[0]==0 && rcode_end[1]==1) // bottom
x1=x1+(float) (W_ymin-y1)/slope ;
y1=W_ymin;
delay(1000);
clearviewport();
rectangle(W_xmin,W_ymin,W_xmax,W_ymax);
line(0,0,600,0);
line(0,0,0,600);
setcolor(RED);
line(x,y,x1,y1);
getch();
closegraph();
Output:-
Q12. Write a program for creating various types of texts and fonts.
Program :-
#include <graphics.h>
#include <conio.h>
int main()
{
//initilizing graphic driver and
//graphic mode variable
int graphicdriver=DETECT,graphicmode;
//calling initgraph with parameters
initgraph(&graphicdriver,&graphicmode,"c:\\turboc3\\bgi");
//Printing message for user
outtextxy(20, 20 + 20, "Program to print different fonts in C graphics");
//initilizing variables
int x = 75, y = 75, f = 0;
//for loop to print different fonts
for (f = 0; f <= 5; f++)
settextstyle(f, HORIZ_DIR, 1);
outtextxy(x, y, "Testing different fonts..");
y = y + 20;
getch();
return 0;
}
Q13. Write a program for creating 2D objects.
Program:
#include <graphics.h>
#include <conio.h>
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
rectangle(100, 100, 200, 200);
circle(300, 150, 50);
line(400, 100, 450, 200);
line(450, 200, 350, 200);
line(350, 200, 400, 100); // triangle
getch();
closegraph();
return 0;
}
Q14. Write a program for creating three dimensional object.
Program:-
#include <graphics.h>
#include <conio.h>
void draw3DCube(int x, int y, int size) {
int offset = size / 2;
rectangle(x, y, x + size, y + size); // front face
rectangle(x + offset, y - offset, x + size + offset, y + size - offset); // back face
// Connect corners
line(x, y, x + offset, y - offset);
line(x + size, y, x + size + offset, y - offset);
line(x, y + size, x + offset, y + size - offset);
line(x + size, y + size, x + size + offset, y + size - offset);
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
draw3DCube(100, 200, 100);
getch();
closegraph();
return 0;
}
Output:
Q15. Write a program for creating curve objects.
Program :
/*
Bezier Curve
*/
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
int x,y,z;
int C(int n,int j);
float blend(int j,int n,float u);
void bezier(float u,int n, int p[4][3]);
void main()
{
float u;
int gd,gm,ymax,i,n,c[4][3];
for(i=0;i<4;i++)
{
c[i][0]=0;
c[i][1]=0;
}
printf("\n\n Enter four points : \n\n");
for(i=0; i<4; i++)
{
printf("\t X%d Y%d : ",i,i);
scanf("%d %d",&c[i][0],&c[i][1]);
}
c[4][0]=c[0][0];
c[4][1]=c[0][1];
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"C:\\Turboc3\\bgi");
ymax = 480;
setcolor(13);
for(i=0;i<3;i++)
{
line(c[i][0],ymax-c[i][1],c[i+1][0],ymax-c[i+1][1]);
}
setcolor(3);
n=3;
for(i=0;i<=40;i++)
{
u=(float)i/40.0;
bezier(u,n,c);
if(i==0)
{
moveto(x,ymax-y);
}
else
{
lineto(x,ymax-y);
}
getch();
}
getch();
}
void bezier(float u,int n, int p[4][3])
{
int j;
float v,b;
float blend(int,int,float);
x=0;y=0;z=0;
for(j=0;j<=n;j++)
{
b=blend(j,n,u);
x=x+(p[j][0]*b);
y=y+(p[j][1]*b);
z=z+(p[j][2]*b);
}
}
float blend(int j,int n,float u)
{
int k;
float v,blend;
v=C(n,j);
for(k=0;k<j;k++)
{ v*=u; }
for(k=1;k<=(n-j);k++)
{ v *= (1-u); }
blend=v;
return(blend);
}
int C(int n,int j)
{
int k,a,c;
a=1;
for(k=j+1;k<=n;k++) { a*=k; }
for(k=1;k<=(n-j);k++) { a=a/k; }
c=a;
return(c);
}
Q16. Write a program for simple animation using transformation.
Program :
#include <graphics.h>
#include <conio.h>
#include <dos.h> // for delay()
int main() {
int gd = DETECT, gm;
int x = 0;
// Initialize graphics mode
initgraph(&gd, &gm, "C:\\Turboc3\\BGI");
while (!kbhit()) {
cleardevice(); // Clear previous frame
setcolor(WHITE);
rectangle(x, 100, x + 100, 150); // Moving rectangle
x += 5; // Translate right
if (x > getmaxx())
x = 0; // Reset to left
delay(50); // Slow down the loop
closegraph();
return 0;
}
Q 17. Write a program to visualize the projection of 3D images.
Program :
#include <stdio.h>
#include <stdlib.h>
#include<graphics.h>
#include<conio.h>
void draw3d(int fs,int x[20],int y[20],int tx,int ty,int d);
void draw3d(int fs,int x[20],int y[20],int tx,int ty,int d)
{
int i,j,k=0;
for(j=0;j<2;j++)
{
for(i=0;i<fs;i++)
{
if(i!=fs-1)
line(x[i]+tx+k,y[i]+ty-k,x[i+1]+tx+k,y[i+1]+ty-k);
else
line(x[i]+tx+k,y[i]+ty-k,x[0]+tx+k,y[0]+ty-k);
}
k=d;
}
for(i=0;i<fs;i++)
{
line(x[i]+tx,y[i]+ty,x[i]+tx+d,y[i]+ty-d);
}
}
void main()
{
int gd=DETECT,gm;
int x[20],y[20],tx=0,ty=0,i,fs,d;
initgraph(&gd,&gm,"");
printf("No of sides (front view only) : ");
scanf("%d",&fs);
printf("Co-ordinates : ");
for(i=0;i<fs;i++)
{
printf("(x%d,y%d)",i,i);
scanf("%d%d",&x[i],&y[i]);
}
printf("Depth :");
scanf("%d",&d);
draw3d(fs,x,y,tx,ty,d);
getch();//front view
setcolor(14);
for(i=0;i<fs;i++)
{
if(i!=fs-1)
line(x[i]+200,y[i],x[i+1]+200,y[i+1]);
else
line(x[i]+200,y[i],x[0]+200,y[0]);
}
getch();//top view
for(i=0;i<fs-1;i++)
{
line(x[i],300,x[i+1],300);
line(x[i],300+d*2,x[i+1],300+d*2);
line(x[i],300,x[i],300+d*2);
line(x[i+1],300,x[i+1],300+d*2);
}
getch();//side view
for(i=0;i<fs-1;i++)
{
line(10,y[i],10,y[i+1]);
line(10+d*2,y[i],10+d*2,y[i+1]);
line(10,y[i],10+d*2,y[i]);
line(10,y[i+1],10+d*2,y[i+1]);
}
getch();
closegraph();
}
Output:
Q18. Write a program to produce animations effect of triangle transform int square and then
into circle.
Program :
#include <graphics.h>
#include <conio.h>
#include <dos.h> // for delay()
void drawTriangle(int x, int y, int size) {
line(x, y, x + size, y); // Base
line(x, y, x + size / 2, y - size); // Left side
line(x + size / 2, y - size, x + size, y); // Right side
}
void drawSquare(int x, int y, int size) {
rectangle(x, y - size, x + size, y);
void drawCircle(int x, int y, int radius) {
circle(x + radius, y - radius, radius);
int main() {
int gd = DETECT, gm;
int x = 200, y = 300, size = 100;
int step, i;
initgraph(&gd, &gm, "C:\\Turboc3\\BGI");
// Step 1: Triangle appearing
for (i = 0; i <= 20; i++) {
cleardevice();
drawTriangle(x, y, size * i / 20);
delay(100);
delay(500);
// Step 2: Triangle fades, square appears
for (i = 0; i <= 20; i++) {
cleardevice();
// Shrink triangle
drawTriangle(x, y, size * (20 - i) / 20);
// Grow square
drawSquare(x, y, size * i / 20);
delay(100);
delay(500);
// Step 3: Square fades, circle appears
for (i = 0; i <= 20; i++) {
cleardevice();
// Shrink square
drawSquare(x, y, size * (20 - i) / 20);
// Grow circle
drawCircle(x, y, (size / 2) * i / 20);
delay(100);
getch();
closegraph();
return 0;