0% found this document useful (0 votes)
15 views50 pages

CG Lab File

Uploaded by

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

CG Lab File

Uploaded by

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

TRINITY INSTITUTE OF PROFESSIONAL

STUDIES

SEC-9 DWARKA, NEW DELHI- 110075


(AFFILIATED TO)
GURU GOBIND SINGH INDRAPRASTHA UNIVERSITY
SECTOR-16C, DWARKA, NEW DELHI

Practical - XI CG Lab (BCA 373)


CG LAB FILE
2024-2025
SUBMITTED BY: SUBMITTED TO:
RUCHI SHRIVASTAVA DR. SANDHYA MAITRA
05124002022 PROFESSOR
BCA-5th SEM, 2nd SHIFT
PRACTICAL – 16
WAP to Scan convert circle using Bresenham’s circle drawing Algorithm.
CODE:
#include<stdio.h>
#include"conio.h"
#include<graphics.h>
void main()
{
int gd=DETECT,gm;
int x,y,xc,yc,i,p,r;
clrscr();
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
printf("enter the radius of circle");
scanf("%d",&r);
printf("enter the circle of circle");
scanf("%d%d",&xc,&yc);
p=3-2*r;
x=0;
y=r;
while(x<=y)
{
putpixel(xc+x,yc+y,1);
putpixel(xc-y,yc+x,2);
putpixel(xc+y,yc-x,3);
putpixel(xc-y,yc+x,4);
putpixel(xc+y,yc+x,5);
putpixel(xc-x,yc-y,6);
putpixel(xc+x,yc-y,7);
putpixel(xc-x,yc+y,8);
if(p<=0)
{
x=x+1;
p=p+4*x+6;
}
else
{
x=x+1;
y=y-1;
p=p+4*x-4*y+10;
}
}
getch();
closegraph();
}

OUTPUT:
PRACTICAL – 3
WAP to Display an asterisk inside a circle.
CODE:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
intgd=DETECT,gm;
initgraph(&gd,&gm,"C:\\turboc3\\bgi");
circle(300,200,75);
line(225,200,375,200);
line(300,125,300,275);
line(250,150,350,250);
line(350,150,250,250);
getch();
}

OUTPUT:
PRACTICAL – 29
WAP to Perform Window-to-View port transformation.
CODE:
#include <graphics.h>
#include <iostream>

using namespace std;

int main() {
FILE *ip, *op;
float xwmin, xwmax, ywmax, ywmin;
float xvmin, xmax, yvmax, yvmin;
float x[10], y[10], yv, xv, sx, sy;
int gd = DETECT, gm, i;

ip = fopen("w2vinput.txt", "r");
op = fopen("w2voutput.txt", "w");
fscanf(ip, "%f %f %f %f", &xwmin, &ywmin, &xwmax, &ywmax);
fscanf(ip, "%f %f %f %f", &xvmin, &yvmin, &xvmax, &yvmax);

for (i = 0; i < 3; i++) {


fscanf(ip, "%f %f", &x[i], &y[i]);
}

sx = ((xvmax - xvmin) / (xwmax - xwmin));


sy = ((yvmax - yvmin) / (ywmax - ywmin));

initgraph(&gd, &gm, NULL);


outtextxy(40, 10, "Window port");
rectangle(xwmin, ywmin, xwmax, ywmax);

for (i = 0; i < 2; i++) {


line(x[i], y[i], x[i + 1], y[i + 1]);
}
line(x[2], y[2], x[0], y[0]);
getch();

for (i = 0; i < 3; i++) {


x[i] = xvmin + ((x[i] - xwmin) * sx);
y[i] = yvmin + ((y[i] - ywmin) * sy);
}
outtextxy(190, 145, "View port");
rectangle(xvmin, yvmin, xvmax, yvmax);
fprintf(op, "%s", "Output points:\n");

for (i = 0; i < 2; i++) {


fprintf(op, "x[%d]=%f y[%d]=%f\n", i, x[i], i, y[i]);
line(x[i], y[i], x[i + 1], y[i + 1]);
}
line(x[2], y[2], x[0], y[0]);
fprintf(op, "x[2]=%f y[2]=%f\n", x[2], y[2]);
getch();
closegraph();

return 0;
}

OUTPUT:
PRACTICAL – 17
WAP to Scan convert circle using Mid-point circle drawing Algorithm.
CODE:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
Intxc,Yc,R,X,Y,I,P;
Intgd=Detect,Gm;
Initgraph(&Gd,&Gm,"C:\\Turboc3\\Bgi");
Printf("Enter Radius Of The Circle:-\T");
Scanf("%D",&R);
Printf("Enter Center Of The Circle:-\T");
Scanf("%D%D",&Xc, &Yc);
P=1-R;X=0;Y=R;
Do
{If(P<0)
{X=X+1;
P=P+2*X+1;}
Else
{
X=X+1;
Y=Y-1;
P=P+2*X-2*Y+10;}
Putpixel(Xc+X,Yc+Y,1);
Putpixel(Xc-Y,Yc-X,2);
Putpixel(Xc+Y,Yc-X,3);
Putpixel(Xc-Y,Yc+X,4);
Putpixel(Xc+Y,Yc+X,5);
Putpixel(Xc-X,Yc-Y,6);
Putpixel(Xc+X,Yc-Y,7);
Putpixel(Xc-X,Yc+Y,8);}
While(X<Y);
Getch();
Closegraph();
}
OUTPUT:
PRACTICAL – 21
WAP to get the Rotation angle from the user and rotate the triangle accordingly.

CODE:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void main()
{
int gd=DETECT,gm;
int x,y,x1,y1,x2,y2;
float t,r;
initgraph(&gd,&gm,"c:\\TURBOC3\\BGI");
printf("\t\tTRIANGLE BEFORE ROTATION\n");
printf("ENTER TRIANGLE AXIS & COORDINATES:-\t");
scanf("%d%d%d%d%d%d",&x,&y,&x1,&y1,&x2,&y2);
printf("TRIANGLE IS\t\t\t") ;
line(x,y,x1,y1);
line(x1,y1,x2,y2);
line(x2,y2,x,y);
printf("ENTER ANGLE OF ROTATION:-\t");
scanf("%f",&r);
t=r*3.14/180;
x=abs(x*cos(t)-y*sin(t));
y=abs(x*sin(t)+y*cos(t));
x1=abs(x1*cos(t)-y1*sin(t));
y1=abs(x1*sin(t)+y1*cos(t));
x2=abs(x2*cos(t)-y2*sin(t));
y2=abs(x2*sin(t)+y2*cos(t));
printf("\t\tTRIANGLE AFTER ROTATION\n");
line(x,y,x1,y1);
line(x1,y1,x2,y2);
line(x2,y2,x,y);
getch();
closegraph();
}
OUTPUT:
PRACTICAL – 23
WAP to get the Scaling factors from the user and scale the triangle
accordingly.
CODE:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void main()
{
int gd=DETECT,gm;
int x,y,x1,y1,x2,y2,sx,sy;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
printf("BEFORE THE SCALING\n");
printf("ENTER THE COORDINATES OF TRIANGLE\n");
scanf("%d%d%d%d%d%d",&x,&y,&x1,&y1,&x2,&y2);
line(x,y,x1,y1);
line(x1,y1,x2,y2);
line(x2,y2,x,y);
printf("\n\n\n\n\n\n\n\n\n\nENTER THE COORDINATES OF SCALING:\n");
scanf("%d%d",&sx,&sy);

x=sx*x;
y=sy*y;
x1=sx*x1;
y1=sy*y1;
x2=sx*x2;
y2=sy*y2;
printf("\n\n\nAFTER SCALING:-\n");
line(x,y,x1,y1);
line(x1,y1,x2,y2);
line(x2,y2,x,y);
getch();
closegraph();
}
OUTPUT:
PRACTICAL – 28
WAP to Perform the Reflection of a triangle about an arbitrary line.
CODE:
#include <conio.h>
#include <graphics.h>
#include <stdio.h>

void main()
{
int gm, gd = DETECT, ax, x1 = 100;
int x2 = 100, x3 = 200, y1 = 100;
int y2 = 200, y3 = 100;

initgraph(&gd, &gm, "");


cleardevice();

line(getmaxx() / 2, 0, getmaxx() / 2, getmaxy());


line(0, getmaxy() / 2, getmaxx(), getmaxy() / 2);

printf("Before Reflection Object in 2nd Quadrant");

setcolor(14);
line(x1, y1, x2, y2);
line(x2, y2, x3, y3);
line(x3, y3, x1, y1);
getch();

printf("\nAfter Reflection");

setcolor(4);
line(getmaxx() - x1, getmaxy() - y1, getmaxx() - x2, getmaxy() - y2);
line(getmaxx() - x2, getmaxy() - y2, getmaxx() - x3, getmaxy() - y3);
line(getmaxx() - x3, getmaxy() - y3, getmaxx() - x1, getmaxy() - y1);

setcolor(3);
line(getmaxx() - x1, y1, getmaxx() - x2, y2);
line(getmaxx() - x2, y2, getmaxx() - x3, y3);
line(getmaxx() - x3, y3, getmaxx() - x1, y1);

setcolor(2);
line(x1, getmaxy() - y1, x2, getmaxy() - y2);
line(x2, getmaxy() - y2, x3, getmaxy() - y3);
line(x3, getmaxy() - y3, x1, getmaxy() - y1);
getch();

closegraph();
}

OUTPUT:
PRACTICAL – 24
WAP to get the Shearing factors from the user and perform a shearing
transformation on a rectangle.
CODE:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void main()
{
int gd=DETECT,gm;
int x,y,x1,y1,x2,y2;
initgraph(&gd,&gm,"c:\\TURBOC3\\BGI");
printf("ENTER RECTANGLE COORDINATES:-\t");
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
rectangle(x1,y1,x2,y2);
printf("ENTER SHEARING VALUE FOR X AXIS:-\t");
scanf("%d",&x);
rectangle(x1,y1,x2*x,y2);
printf("ENTER SHEARING VALUE FOR Y AXIS:-\t");
scanf("%d",&y);
rectangle(x1,y1,x2,y2*y);
getch();
closegraph();
}

OUTPUT:
PRACTICAL – 30
WAP to Implement Cohen Sutherland line clipping algorithm.
CODE:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT, gm;
float i,xmax,ymax,xmin,ymin,x1,y1,x2,y2,m;
float start[4],end[4],code[4];
clrscr();
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
printf("\n\tEnter the bottom-left coordinate of viewport: ");
scanf("%f %f",&xmin,&ymin);
printf("\n\tEnter the top-right coordinate of viewport: ");
scanf("%f %f",&xmax,&ymax);
printf("\nEnter the coordinates for starting point of line: ");
scanf("%f %f",&x1,&y1);
printf("\nEnter the coordinates for ending point of line: ");
scanf("%f %f",&x2,&y2);
for(i=0;i <4;i++)
{
start[i]=0;
end[i]=0;
}
m=(y2-y1)/(x2-x1);
if(x1 <xmin) start[0]=1;
if(x1 >xmax) start[1]=1;
if(y1 >ymax) start[2]=1;
if(y1 <ymin) start[3]=1;
if(x2 <xmin) end[0]=1;
if(x2 >xmax) end[1]=1;
if(y2 >ymax) end[2]=1;
if(y2 <ymin) end[3]=1;
for(i=0;i <4;i++)
code[i]=start[i]&&end[i];
if((code[0]==0)&&(code[1]==0)&&(code[2]==0)&&(code[3]==0))
{
if((start[0]==0)&&(start[1]==0)&&(start[2]==0)&&(start[3]==0)&&(end[0]==0)&&(end[1]==0
)&&(end[2]==0)&&(end[3]==0))
{
cleardevice();
printf("\n\t\tThe line is totally visible\n\t\tand not a clipping candidate");
rectangle(xmin,ymin,xmax,ymax);
line(x1,y1,x2,y2);
getch();
}
else
{
cleardevice();
printf("\n\t\tLine is partially visible");
rectangle(xmin,ymin,xmax,ymax);
line(x1,y1,x2,y2);
getch();
if((start[2]==0)&&(start[3]==1))
{
x1=x1+(ymin-y1)/m;
y1=ymin;
}
if((end[2]==0)&&(end[3]==1))
{
x2=x2+(ymin-y2)/m;
y2=ymin;
}
if((start[2]==1)&&(start[3]==0))
{
x1=x1+(ymax-y1)/m;
y1=ymax;
}
if((end[2]==1)&&(end[3]==0))
{
x2=x2+(ymax-y2)/m;
y2=ymax;
}
if((start[1]==0)&&(start[0]==1))
{
y1=y1+m*(xmin-x1);
x1=xmin;
}
if((end[1]==0)&&(end[0]==1))
{
y2=y2+m*(xmin-x2);
x2=xmin;
}
if((start[1]==1)&&(start[0]==0))
{
y1=y1+m*(xmax-x1);
x1=xmax;
}
if((end[1]==1)&&(end[0]==0))
{
y2=y2+m*(xmax-x2);
x2=xmax;
}
clrscr();
cleardevice();
printf("\n\t\tAfter clippling:");
rectangle(xmin,ymin,xmax,ymax);
line(x1,y1,x2,y2);
getch();
}
}
else
{
clrscr();
cleardevice();
printf("\nLine is invisible");
rectangle(xmin,ymin,xmax,ymax);
}
getch();
closegraph();
}
OUTPUT:
PRACTICAL – 22
Program to rotate a circle outside another circle.
CODE:
#include <conio.h>
#include <graphics.h>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>

using namespace std;

void drawEllipse(int xc, int yc, int a, int b, float alpha, int color)
{
float t = 3.14 / 180;
alpha = 360 - alpha;
setcolor(color);
int theta;

for (int i = 0; i < 360; i += 1) {


theta = i;
int x = a * cos(t * theta) * cos(t * alpha)
+ b * sin(t * theta) * sin(t * alpha);

int y = b * sin(t * theta) * cos(t * alpha)


- a * cos(t * theta) * sin(t * alpha);

putpixel(xc + x, yc - y, color);
}
}

void slidePattern(int xc, int yc, int r, int a, int b, int alpha, float p, int color)
{
setcolor(color);
float t = 3.14 / 180;
float t1, t2, d;
float angle = (p * alpha);

t1 = cos(t * fmod(angle, 360));


t2 = sin(t * fmod(angle, 360));
t1 *= t1;
t2 *= t2;
t1 = t1 / (a * a);
t2 = t2 / (b * b);
d = sqrt(t1 + t2);
d = 1 / d;

int draw_x = xc + (r + d) * cos(t * alpha);


int draw_y = yc - (r + d) * sin(t * alpha);
int draw_ang = angle + alpha;

drawEllipse(draw_x, draw_y, a, b, draw_ang, color);


}

void ellipseovercircle(int xc, int yc, int r, int a, int b)


{
float theta = 0;
double h, p1;

h = (a * a) + (b * b);
h /= 2;
p1 = sqrt(h);
p1 /= r;
p1 = 1 / (p1);

for (;; theta -= 1) {

slidePattern(xc, yc, r, a, b, theta, p1, WHITE);

circle(xc, yc, r);


delay(25);

slidePattern(xc, yc, r, a, b, theta, p1, BLACK);


}
}

int main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, " C:\\TURBOC3\\BGI ");

int maxx = getmaxx();


int maxy = getmaxy();
ellipseovercircle(maxx / 2, maxy / 2, 100, 40, 28);

closegraph();
return 0;
}

OUTPUT:
PRACTICAL – 9
Program to draw Flying Balloons.
CODE:
#include<stdio.h>
#include"conio.h"
#include<dos.h>
#include<graphics.h>
void main(){
int gd=DETECT,gm,x,y,w,z,i;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
for(i=100;i<=210;i++)
{
delay(5);
cleardevice();
setfillstyle(SOLID_FILL,BLUE);
circle(150,250-i,40);
line(150,290-i,150,500-i);
floodfill(150,250-i,WHITE);
setfillstyle(SOLID_FILL,YELLOW);
circle(250,250-i,40);
line(250,290-i,250,500-i);
floodfill(250,250-i,WHITE);
setfillstyle(SOLID_FILL,RED);
circle(350,250-i,40);
line(350,290-i,350,500-i);
floodfill(350,250-i,WHITE);
}
getch();
closegraph();
}
OUTPUT:
PRACTICAL – 10
Show Bouncing Ball Animation.
CODE:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<stdlib.h>

void main()
{
int gd = DETECT, gm;
int i, x, y, flag=0;
initgraph(&gd, &gm, "C:\\TC\\BGI");

x = getmaxx()/2;
y = 30;

while(!kbhit())
{
if(y >= getmaxy()-30 || y <= 30)
flag = !flag;

setcolor(RED);
setfillstyle(SOLID_FILL, RED);
circle(x, y, 30);
floodfill(x, y, RED);

delay(25);
cleardevice();
if(flag)
{
y = y + 2;
}
else
{
y = y - 2;
}}
getch();
closegraph();
}
OUTPUT:
PRACTICAL – 11
Draw pie chart of family income and Expenditure.
CODE:
#include<graphics.h>

int main() {
int gd = DETECT, gm, x, y;
initgraph(&gd, &gm, "C:\\TC\\BGI");

settextstyle(BOLD_FONT,HORIZ_DIR,2);
outtextxy(220,10,"PIE CHART");
/* Setting cordinate of center of circle */
x = getmaxx()/2;
y = getmaxy()/2;

settextstyle(SANS_SERIF_FONT,HORIZ_DIR,1);
setfillstyle(SOLID_FILL, RED);
pieslice(x, y, 0, 60, 120);
outtextxy(x + 140, y - 70, "FOOD");

setfillstyle(SOLID_FILL, YELLOW);
pieslice(x, y, 60, 160, 120);
outtextxy(x - 30, y - 170, "RENT");

setfillstyle(SOLID_FILL, GREEN);
pieslice(x, y, 160, 220, 120);
outtextxy(x - 250, y, "ELECTRICITY");

setfillstyle(SOLID_FILL, BROWN);
pieslice(x, y, 220, 360, 120);
outtextxy(x, y + 150, "SAVINGS");

closegraph();
return 0;
}
OUTPUT:
PRACTICAL – 8
Show changing radius of circle.
CODE:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void main()
{
int gd=DETECT,gm;
int x,y,r,angle,xc,yc;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
x=0;
y=r;
angle=0;
printf("ENTER XC,YC,RADIUS OF CIRCLE:-\t");
scanf("%d%d%d",&xc,&yc,&r);
while(angle<=360)
{
putpixel(xc+x,yc+y,6);
x=r*cos(angle);
y=r*sin(angle);
angle=angle+1;
}
getch();
}

OUTPUT:
PRACTICAL – 25
WAP to Perform fixed point Scaling.

CODE:
#include <stdio.h>
#include <conio.h>

void main() {
int fixedPointValue, scaleFactor;
int scaledValue;

clrscr();
printf("Enter fixed point value: ");
scanf("%d", &fixedPointValue);
printf("Enter scale factor: ");
scanf("%d", &scaleFactor);

scaledValue = fixedPointValue * scaleFactor;

printf("Scaled Value: %d\n", scaledValue);


getch();
}
OUTPUT:
PRACTICAL – 13
Making an Analog Clock.
CODE:
#include <graphics.h>
#include <conio.h>
#include <dos.h>
#include <math.h>
#include <time.h>

void drawClockFace(int xc, int yc, int radius) {


setcolor(WHITE);
circle(xc, yc, radius);
for (int i = 0; i < 12; i++) {
float angle = (i * 30) * M_PI / 180;
int x1 = xc + (radius - 10) * cos(angle);
int y1 = yc - (radius - 10) * sin(angle);
int x2 = xc + radius * cos(angle);
int y2 = yc - radius * sin(angle);
line(x1, y1, x2, y2);
}
}

void drawHand(int xc, int yc, int length, float angle) {


int x = xc + length * cos(angle * M_PI / 180);
int y = yc - length * sin(angle * M_PI / 180);
line(xc, yc, x, y);
}

void drawClock(int xc, int yc) {


time_t now = time(0);
struct tm *t = localtime(&now);

int hour = t->tm_hour % 12;


int minute = t->tm_min;
int second = t->tm_sec;

float hourAngle = (hour * 30) + (minute / 2.0);


float minuteAngle = minute * 6;
float secondAngle = second * 6;

setcolor(WHITE);
drawClockFace(xc, yc, 100);
setcolor(YELLOW);
drawHand(xc, yc, 50, hourAngle);
setcolor(GREEN);
drawHand(xc, yc, 70, minuteAngle);
setcolor(GREEN);
drawHand(xc, yc, 90, secondAngle);
}

int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");

while (!kbhit()) {
cleardevice();
drawClock(getmaxx() / 2, getmaxy() / 2);
delay(1000);
}

getch();
closegraph();
return 0;
}

OUTPUT:
PRACTICAL – 14
Draw a moving cycle.
CODE:
#include <conio.h>
#include <dos.h>
#include <graphics.h>
#include <iostream.h>

int main()
{
int gd = DETECT, gm, i, a;
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");

for (i = 0; i < 600; i++) {


line(50 + i, 405, 100 + i, 405);
line(75 + i, 375, 125 + i, 375);
line(50 + i, 405, 75 + i, 375);
line(100 + i, 405, 100 + i, 345);
line(150 + i, 405, 100 + i, 345);
line(75 + i, 345, 75 + i, 370);
line(70 + i, 370, 80 + i, 370);
line(80 + i, 345, 100 + i, 345);

circle(150 + i, 405, 30);


circle(50 + i, 405, 30);

line(0, 436, getmaxx(), 436);

rectangle(getmaxx() - i, 436, 650 - i, 431);

delay(10);
cleardevice();
}

getch();
closegraph();
}
OUTPUT:
PRACTICAL – 12
Design a screensaver.
CODE:
#include <graphics.h>
#include <conio.h>
#include <stdlib.h>
#include <dos.h>
#include <time.h>

void drawRandomCircle(int x, int y, int radius) {


setcolor(rand() % 16);
int pattern = rand() % 4;

if (pattern == 0) {
setfillstyle(SOLID_FILL, getcolor());
} else if (pattern == 1) {
setfillstyle(HATCH_FILL, getcolor());
} else if (pattern == 2) {
setfillstyle(XHATCH_FILL, getcolor());
} else {
setfillstyle(INTERLEAVE_FILL, getcolor());
}

floodfill(x, y, getcolor());
circle(x, y, radius);
}

int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
srand(time(NULL));

while (!kbhit()) {
cleardevice();
for (int i = 0; i < 50; i++) {
int x = rand() % getmaxx();
int y = rand() % getmaxy();
int radius = rand() % 30 + 10;
drawRandomCircle(x, y, radius);
}
delay(100);
}

getch();
closegraph();
return 0;
}

OUTPUT:
PRACTICAL – 15
Show Moving Car Animation.
CODE:
#include<stdio.h>
#include"conio.h"
#include<dos.h>
#include<graphics.h>
void main(){
int gd=DETECT,gm,x,y,w,z,i;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
for(i=100;i<=400;i++)
{
delay(10);
cleardevice();
rectangle(400-i,200,600-i,250);
rectangle(480-i,210,510-i,230);//window of car
line(420-i,200,460-i,160);
line(460-i,160,540-i,160);
line(540-i,160,580-i,200);
circle(450-i,250,20);//wheel of car
circle(550-i,250,20); //wheel of car
circle(450-i,250,10);
circle(550-i,250,10);
}
getch();
closegraph();
}

OUTPUT:
PRACTICAL – 2
Display a string “Hello User” in the centre of the screen using Outtextxy()
function.
CODE:
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>

int main()
{
int gdriver = DETECT, gmode;
initgraph(&gdriver, &gmode, "C:\\TC\\BGI");

int centerX = getmaxx() / 2;


int centerY = getmaxy() / 2;

outtextxy(centerX - textwidth("HELLO USER") / 2, centerY, "HELLO USER");

getch();
closegraph();
return 0;
}

OUTPUT:
PRACTICAL – 26
WAP to Perform fixed point Rotation.
CODE:
#include <stdio.h>
#include <conio.h>

void main() {
int x, y, angle;
float radian, cosine, sine, newX, newY;

clrscr();
printf("Enter Fixed Point (x,y) for Rotation:");
scanf("%d", &x);

radian = angle * (3.14159 / 180);


cosine = cos(radian);
sine = sin(radian);

newX = x * cosine - y * sine;


newY = x * sine + y * cosine;

getch();
}

OUTPUT:
PRACTICAL – 4
Display nested circles.
CODE:
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
void main(){
int gd = DETECT,gm;
int x ,y;
initgraph(&gd, &gm, "C:\\TC\\BGI");
x = getmaxx()/2;
y = getmaxy()/2;
outtextxy(240, 50, "Concentric Circles");
setcolor(RED);
circle(x, y, 30);
setcolor(GREEN);
circle(x, y, 50);
setcolor(YELLOW);
circle(x, y, 70);
setcolor(BLUE);
circle(x, y, 90);
getch();
closegraph();
}
OUTPUT:
PRACTICAL – 5
Display nested rectangles.
CODE:
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main()
{
int gdriver = DETECT, gmode;
int left, top, right, bottom;
initgraph(&gdriver, &gmode, "C://TC//bgi");
left = 100;
top = 100;
right = 400;
bottom = 300;
for (int i = 0; i < 5; i++) {
rectangle(left, top, right, bottom);
left += 20;
top += 20;
right -= 20;
bottom -= 20;
}
getch();
closegraph();
return 0;
}

OUTPUT:
PRACTICAL – 7
Write a program to display HUT.
CODE:
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>

int main(void)
{
int gdriver = DETECT, gmode;
initgraph(&gdriver, &gmode, "");

printf("\n\n\n\t\t\t\t THE HUT IS");

line(40 + 80, 400, 140 + 80, 420);


line(140 + 80, 420, 450 + 80, 420);
line(40 + 80, 400, 40 + 80, 170);
line(140 + 80, 420, 140 + 80, 190);
line(40 + 80, 170, 140 + 80, 190);
line(140 + 80, 190, 450 + 80, 190);
line(450 + 80, 190, 450 + 80, 420);
line(40 + 80, 170, 90 + 80, 110);
line(140 + 80, 190, 90 + 80, 110);

line(90 + 80, 110, 370 + 80, 110);


line(370 + 80, 110, 450 + 80, 190);
rectangle(200 + 80, 240, 400 + 80, 360);
line(300 + 80, 240, 300 + 80, 360);
line(200 + 80, 300, 400 + 80, 300);
line(70 + 80, 405, 70 + 80, 290);
line(70 + 80, 290, 110 + 80, 310);
line(110 + 80, 310, 110 + 80, 415);

getch();
closegraph();
return 0;
}
OUTPUT:
PRACTICAL – 20
WAP to get the Translation vector from the user and translate the triangle
accordingly.
CODE:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm;
int x,y,x1,y1,x2,y2,tx,ty;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
printf("BEFORE THE TRANSLATION\n");
printf("ENTER THE COORDINATES OF TRIANGLE\n");
scanf("%d%d%d%d%d%d",&x,&y,&x1,&y1,&x2,&y2);
line(x,y,x1,y1);
line(x1,y1,x2,y2);
line(x2,y2,x,y);
printf("\n\n\n\n\n\n\n\n\n\nENTER TRANSLATION COORDINATES:\n\n");
scanf("%d%d",&tx,&ty);
x=x+tx;
y=y+ty;
x1=x1+tx;
y1=y1+ty;
x2=x2+tx;
y2=y2+ty;
printf("\n\n\nAFTER TRANSLATION:-\n");
line(x,y,x1,y1);
line(x1,y1,x2,y2);
line(x2,y2,x,y);
getch();
closegraph();
}
OUTPUT:
PRACTICAL – 19
WAP to Scan convert circle using Polynomial method.
CODE:
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
void main()
{
intgd=DETECT,gm;
intx,y,r,xc,yc;
initgraph(&gd,&gm,"C:\\turboc3\\bgi");
x=0;
printf("ENTER XC,YC,RADIUS OF CIRCLE:-\t");
scanf("%d%d%d",&xc,&yc,&r);
y=r;
while(x<=y)
{putpixel(xc+x,yc+y,4);
putpixel(xc+y,yc+x,4);
putpixel(xc-x,yc+y,4);
putpixel(xc-y,yc+x,4);
putpixel(xc-y,yc-x,4);
putpixel(xc-x,yc-y,4);
putpixel(xc+x,yc-y,4);
putpixel(xc+y,yc-x,4);
x=x+1;y=sqrt(r*r-x*x);}getch();
}

OUTPUT:
PRACTICAL – 18
WAP to Scan convert circle using Trigonometric method.
CODE:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void main()
{
int gd=DETECT,gm;
int x,y,r,angle,xc,yc;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
x=0;
y=r;
angle=0;
printf("ENTER XC,YC,RADIUS OF CIRCLE:-\t");
scanf("%d%d%d",&xc,&yc,&r);
while(angle<=360)
{
putpixel(xc+x,yc+y,6);
x=r*cos(angle);
y=r*sin(angle);
angle=angle+1;
}
getch();
}

OUTPUT:
PRACTICAL – 27
WAP to Perform two transformations in succession on same object.
CODE:
#include <stdio.h>

int main() {
int x, y, newX, newY, scaleFactor, translationX, translationY;

printf("Enter x-coordinate: ");


scanf("%d", &x);
printf("Enter y-coordinate: ");
scanf("%d", &y);
printf("Enter scale factor: ");
scanf("%d", &scaleFactor);
printf("Enter translation x: ");
scanf("%d", &translationX);
printf("Enter translation y: ");
scanf("%d", &translationY);

newX = x * scaleFactor;
newY = y * scaleFactor;

newX += translationX;
newY += translationY;

printf("Transformed coordinates: (%d, %d)\n", newX, newY);

return 0;
}

OUTPUT:
PRACTICAL – 1
WAP to Display Coordinate Axes.
CODE:
#include<graphics.h>
#include<conio.h>
#include<stdio.h>
void main(){
int a,b,i;
int gd=DETECT,gm;
initgraph(&gd,&gm,"//turboc 3\\bgi");
a=getmaxx();
b=getmaxy();
line(a/2,0,a/2,b);
line(0,b/2,a,b/2);
outtextxy(a-50,b/2,"X AXIS");
outtextxy(a/2,b-50,"Y AXIS");
for(i=0;i<a;i+=35){
line(a/2+i,b/2-10,a/2+i,b/2+10);
line(a/2-i,b/2+10,a/2-i,b/2-10);
line(a/2+10,b/2-i,a/2-10,b/2-i);
line(a/2-10,b/2+i,a/2+10,b/2+i);}
getch();
}

OUTPUT:
PRACTICAL – 6
WAP to Display different Colorful Shapes.
CODE:
#include <graphics.h>
#include <conio.h>

void main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\Turboc3\\BGI");

setbkcolor(BLACK);
cleardevice();

setcolor(RED);
setfillstyle(SOLID_FILL, RED);
floodfill(300, 100, RED);
circle(300, 100, 50);

setcolor(YELLOW);
setfillstyle(SOLID_FILL, YELLOW);
int x[] = {250, 350, 300};
int y[] = {200, 200, 100};
fillpoly(3, x, y);

setcolor(BLUE);
setfillstyle(SOLID_FILL, BLUE);
rectangle(400, 150, 600, 250);
floodfill(500, 200, BLUE);

setcolor(MAGENTA);
setfillstyle(SOLID_FILL, MAGENTA);
rectangle(400, 300, 500, 400);
floodfill(450, 350, MAGENTA);

getch();
closegraph();
}
OUTPUT:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy