Computer Graphic Lab File
Computer Graphic Lab File
Code
#include <graphics.h>
#include <conio.h>
void main() {
initgraph(&gd, &gm, "C:\\Turboc3\\BGI"); // Ensure the path is correct for your setup
setbkcolor(GREEN);
printf("\t\t\t\n\nLINE");
printf("\t\t\n\n\n\nRECTANGLE");
printf("\t\t\t\n\n\n\n\n\n\nARC");
printf("\t\n\n\n\nCIRCLE");
printf("\t\n\n\n\nECLIPSE");
getch();
Cdoe
#include <graphics.h>
#include <conio.h>
void main() {
// initgraph(&gd, &gm, "C:\\Turboc3\\BGI"); // Ensure the path is correct for your setup
initgraph(&gd, &gm, "C:\\Turboc3\\BGI"); // Ensure the path is correct for your setup
}
Question 3
Code
// #include <stdio.h>
#include <conio.h>
#include <graphics.h>
#include <stdlib.h>
void main() {
// initgraph(&gd, &gm, "C:\\TC\\BGI"); // Ensure the path is correct for your setup
initgraph(&gd, &gm, "C:\\Turboc3\\BGI"); // Ensure the path is correct for your setup
i++;
circle(xx, yy, r); // Draw a circle at random position with random radius
}
getch(); // Wait for a key press
}
Question 4
Code
#include <graphics.h>
#include <conio.h>
int main() {
// initgraph(&gd, &gm, "C:\\TC\\BGI"); // Ensure the path is correct for your setup
initgraph(&gd, &gm, "C:\\Turboc3\\BGI"); // Ensure the path is correct for your setup
setfillstyle(1, BLUE);
setfillstyle(1, LIGHTBLUE);
return 0;
}
Question 5
Code
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
#include <dos.h>
void main() {
}
Question 6
Ccdoe
#include <graphics.h>
#include <stdio.h>
void main() {
char ch;
while ((ch = getch()) != 13) { // Continue until the Enter key (ASCII 13) is pressed
switch (ch) {
break;
break;
break;
break;
}
Question 7
Code
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
struct time t;
void display(int,int,int);
void main()
int i=0,gd=DETECT,gm,hr,min,sec;
clrscr();
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
setcolor(GREEN);
settextstyle(4,0,7);
while(!kbhit())
gettime(&t);
hr=t.ti_hour;
min=t.ti_min;
sec=t.ti_sec;
i++;
display(100,100,hr);
display(200,100,min);
display(300,100,sec);
sound(400);
delay(30);
nosound();
delay(930);
cleardevice();
getch();
char str[3];
itoa(num,str,10);
settextstyle(4,0,7);
outtextxy(180,100,":");
outtextxy(280,100,":");
outtextxy(x,y,str);
rectangle(90,90,380,200);
rectangle(70,70,400,220);
outtextxy(90,250,"Digital Clock");
}
Question 8
Code
#include <stdio.h>
#include <graphics.h>
void main() {
int x, y = 0, t = MAXHEIGHT, c = 1;
// Draw Ground
setcolor(WHITE);
// Draw Ball
setcolor(RED);
setfillstyle(SOLID_FILL, RED);
t -= 40;
c = 1;
if (t >= 40) {
y += (c ? 15 : -15);
if (kbhit()) {
break;
}
Question 9
Code
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
#include <dos.h>
int main() {
int x, y, flag = 0;
// Toggle the flag when the ball reaches the top or bottom
flag = !flag;
setcolor(RED);
setfillstyle(SOLID_FILL, RED);
} else {
return 0;
}
Question 10
Code
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
#include <math.h>
void draw();
void rotate();
int main(void) {
return 0;
}
void draw() {
void rotate() {
float angle;
scanf("%f", &angle);
printf("ROTATION");
printf("\nChanged coordinates:\n");
printf("%d %d\n%d %d\n%d %d", a1, b1, a2, b2, a3, b3);
}
Question 11
Code
#include<stdio.h>
#include<graphics.h>
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;
}
void main()
scanf("%d", &r);
drawcircle(x, y, r);
getch();
}
Question 12
Code
#include <stdio.h>
#include <graphics.h>
#include <math.h>
#include <stdlib.h>
#define pi 3.14
// Function prototypes
void initializeGraphics();
int main() {
int choice;
float amplitude;
initializeGraphics();
// Input amplitude
scanf("%f", &litude);
// Menu
printf("\nMENU:\n");
printf("4. EXIT\n");
scanf("%d", &choice);
switch (choice) {
case 1:
drawSine(amplitude);
break;
case 2:
drawCosine(amplitude);
break;
case 3:
drawTangent(amplitude);
break;
case 4:
exit(0);
default:
printf("Invalid choice!\n");
break;
return 0;
float x, y;
cleardevice();
xm = getmaxx() / 2;
ym = getmaxy() / 2;
// Draw axes
float x, y;
cleardevice();
xm = getmaxx() / 2;
ym = getmaxy() / 2;
// Draw axes
float x, y;
cleardevice();
xm = getmaxx() / 2;
ym = getmaxy() / 2;
// Draw axes
void initializeGraphics() {
if (errorcode != grOk) {
getch();
exit(1);
}
Question 13
Cdoe
#include <graphics.h>
#include <conio.h>
int main() {
// Draw a rectangle
// Draw a circle
circle(x, y, radius);
// Draw an ellipse
// Display text
return 0;
}
Question 14
Code
#include <graphics.h>
#include <conio.h>
int main()
setcolor(YELLOW);
rectangle(0,30,639,450);
settextstyle(SANS_SERIF_FONT,HORIZ_DIR,2);
setcolor(WHITE);
outtextxy(275,0,"Bar Chart");
setlinestyle(SOLID_LINE,0,2);
line(100,420,100,60);
line(100,420,600,420);
line(90,70,100,60);
line(110,70,100,60);
line(590,410,600,420);
line(590,430,600,420);
outtextxy(95,35,"Y");
outtextxy(610,405,"X");
outtextxy(85,415,"O");
setfillstyle(LINE_FILL,BLUE);
bar(150,100,200,419);
setfillstyle(XHATCH_FILL,RED);
bar(225,150,275,419);
setfillstyle(WIDE_DOT_FILL,GREEN);
bar(300,200,350,419);
setfillstyle(INTERLEAVE_FILL,MAGENTA);
bar(375,125,425,419);
setfillstyle(HATCH_FILL,BROWN);
bar(450,175,500,419);
getch();
return 0;
}
Question 15
Code
#include <graphics.h>
#include <math.h>
#include <stdio.h>
int abs(int n) {
// Custom round function (since round() may not be available in Turbo C++)
// Calculate dx & dy
int dx = X1 - X0;
int dy = Y1 - Y0;
float Y = Y0;
// Driver program
int main() {
int X0, Y0, X1, Y1; // Declare all variables at the beginning of the block
X0 = 100;
Y0 = 100;
X1 = 400;
Y1 = 300;
return 0;
}
Question 16
Code
#include<stdio.h>
#include<graphics.h>
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()
return 0;
}
Question 17
Code
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void main()
int xc=340,yc=280,y,r,x;
float theta;
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\turboc3\\BGI");
//scanf("%d%d",&xc,&yc);
scanf("%d",&r);
for(theta=0;theta<=2*3.14;theta+=0.01)
x=(int)(xc+(r*cos(theta)));
y=(int)(yc+(r*sin(theta)));
putpixel(x,y,WHITE);
getch();
closegraph();
}
Question 18
Code
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void main()
int gd=DETECT,gm;
int r,x,y,p,xc=320,yc=240;
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
cleardevice();
scanf("%d",&r);
x=0;
y=r;
putpixel(xc+x,yc-y,1);
p=3-(2*r);
for(x=0;x<=y;x++)
if(p<0)
y=y;
p=(p+(4*x)+6);
else
y=y-1;
p=p+((4*(x-y)+10));
putpixel(xc+x,yc-y,1);
putpixel(xc-x,yc-y,2);
putpixel(xc+x,yc+y,3);
putpixel(xc-x,yc+y,4);
putpixel(xc+y,yc-x,5);
putpixel(xc-y,yc-x,6);
putpixel(xc+y,yc+x,7);
putpixel(xc-y,yc+x,8);
getch();
closegraph();
}
Question 19
Code
#include<graphics.h>
#include<conio.h>
#include<stdio.h>
float xmin,ymin,xmax,ymax;
int code=0;
if(y>ymax)
code |=t;
else if(y<ymin)
code|=b;
else if(x>xmax)
code|=r;
else if(x<xmin)
code|=l;
return(code);
int accept=0,done=0;
code1=calcode(x1,y1);
code2=calcode(x2,y2);
do
if(!(code1|code2))
{
accept=1;
done=1;
done=1;
else
float x,y;
codeout=code1?code1:code2;
if(codeout& t)
x=x1+(x2-x1)*(ymax-y1)/(y2-y1);
y=ymax;
else if(codeout& b)
x=x1+(x2-x1)*(ymin-y1)/(y2-y1);
y=ymin;
else if(codeout& r)
y=y1+(y2-y1)*(xmax-x1)/(x2-x1);
x=xmax;
else
y=y1+(y2-y1)*(xmin-x1)/(x2-x1);
x=xmin;
if(codeout==code1)
{
x1=x;
y1=y;
code1=calcode(x1,y1);
else
x2=x;
y2=y;
code2=calcode(x2,y2);
while(done==0);
if(accept)
line(x1,y1,x2,y2);
rectangle(xmin,ymin,xmax,ymax);
main()
float x1,y1,x2,y2;
int gd=DETECT,gm;
clrscr();
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
scanf("%f",&x1);
printf("\n\t y1:");
scanf("%f",&y1);
printf("\n\t x2:");
scanf("%f",&x2);
printf("\n\t y2:");
scanf("%f",&y2);
printf("\n\txmin");
scanf("%f",&xmin);
printf("\n\tymin");
scanf("%f",&ymin);
printf("\n\txmax");
scanf("%f",&xmax);
printf("\n\tymax");
scanf("%f",&ymax);
clrscr();
line(x1,y1,x2,y2);
rectangle(xmin,ymin,xmax,ymax);
getch();
clrscr();
lineclip(x1,y1,x2,y2);
getch();
closegraph();
return 0;
}
Question 20
Code
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<math.h>
int main()
int gd = DETECT,gm,a,b,i,r,x,y;
initgraph(&gd, &gm,"C:\\Turboc3\\BGI");
setfillstyle(SOLID_FILL,1);
rectangle(10,320,200,340);
floodfill(11,321,15);
rectangle(30,300,175,320);
floodfill(31,301,15);
rectangle(50,280,155,300);
floodfill(51,281,15);
setfillstyle(SOLID_FILL,3);
rectangle(100,38,110,280);
floodfill(101,39,15);
setfillstyle(SOLID_FILL,RED);
rectangle(110,40,220,58);
floodfill(111,43,15);
setfillstyle(SOLID_FILL,15);
rectangle(110,58,220,78);
floodfill(111,59,15);
setfillstyle(SOLID_FILL,GREEN);
rectangle(110,78,220,98);
floodfill(111,79,15);
//Ashok chakra
a=160;
b=68;
r=13;
setcolor(BLUE);
circle(a,b,r);
for(i=0;i<=360;i=i+25)
x=r*cos(i*3.14/180);
y=r*sin(i*3.14/180);
line(a,b,a+x,b-y);
getch();
return 0;
// closegraph();