0% found this document useful (0 votes)
106 views24 pages

CG Practical

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)
106 views24 pages

CG Practical

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/ 24

Practical File

COMPUTER GRAPHICS
(BCA0604)
CLASS: BCA 6TH
ROLL NO. : 21BCA016
UNIVERSITY ROLL NO: 6210250027
SUBMITTED BY: SAHIL BHARDWAJ
SUBMITTED TO: A.P. ANCHAL SHARMA
[Practical File Computer Graphics]

INDEX

Sr. No. Content Page Sign


no.
1. Write a program in c to draw lines 2

2. Write a program in C to draw a circle 3

3. Draw a hut using graphics.h and fill color in it 4

4. Draw a smiley face using graphics.h 5

5. Write a program in C to draw a rainbow 6

6. Write a program to draw a car 7-8

7. Write a program to show reflection in 2D 9-10

8. Write a program in C++ to performTranslation 11

9. Write a program for Rotation in 2D 12

10. Write a program in C++ to perform scaling 13-14

11. Write a program to show shearing in square 15

12. Program to draw a line using DDA algorithm 16-17

13. Program to draw a circle using mid point 18-19


algorithm
14. Write program to draw a circle using 20-21
Bresenham’s algorithm
15, Draw ellipse using midpoint algorithm 22-23

[1]
[Practical File Computer Graphics]

1. Write a program in c to draw lines:

#include <graphics.h>

void main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\TurboC++\Disk\TurboC3\BGI");
line(150, 150, 450, 150);
line(150, 250, 450, 250);
getch();
closegraph();
}

OUTPUT:

[2]
[Practical File Computer Graphics]

2. Write a program in C to draw a circle:

#include <graphics.h>

void main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\TurboC++\Disk\TurboC3\BGI");
circle(300, 200, 100);
getch();
closegraph();
}

OUTPUT:

[3]
[Practical File Computer Graphics]

3. Draw a hut using graphics.h and fill color in it:


#include<graphics.h>
#include<stdio.h>
#include<conio.h>
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
printf("\t\t ********* HUT ********");
line(150,100,50,200);
line(150,100,350,100);
line(150,100,300,200);
line(300,200,500,200);
line(350,100,500,200);
line(50,200,300,200);
rectangle(50,400,300,200);
rectangle(300,200,500,400);
rectangle(130,250,230,400);
setfillstyle(2, 3);
floodfill(131, 131, WHITE);
floodfill(201, 101, WHITE);
setfillstyle(11, 7);
floodfill(101, 101, WHITE);
floodfill(150, 52, WHITE);
floodfill(163, 55, WHITE);
floodfill(251, 121, WHITE);
getch();
closegraph();
}
OUTPUT:

[4]
[Practical File Computer Graphics]

4. Draw a smiley face using graphics.h:


#include <conio.h>
#include <dos.h>
#include <graphics.h>
#include <stdio.h>
void main()
{
int gr = DETECT, gm;
initgraph(&gr, &gm, "C:\\Turboc3\\BGI");
setcolor(YELLOW);
circle(300, 100, 40);
setfillstyle(SOLID_FILL, YELLOW);
floodfill(300, 100, YELLOW);
setcolor(BLACK);
setfillstyle(SOLID_FILL, BLACK);
fillellipse(310, 85, 2, 6);
fillellipse(290, 85, 2, 6);
ellipse(300, 100, 205, 335, 20, 9);
ellipse(300, 100, 205, 335, 20, 10);
ellipse(300, 100, 205, 335, 20, 11);
getch();
closegraph();
}

Output:

[5]
[Practical File Computer Graphics]

5. Write a program in C to draw a rainbow:


#include<stdio.h>
#include<graphics.h>
#include<dos.h>
void rainbow()
{
int gdriver = DETECT,gmode;
int x, y, i;
initgraph(&gdriver,&gmode," C:\TurboC++\Disk\TurboC3\BGI ");

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

for (i=30; i<200; i++)


{
delay(100);
setcolor(i/10);
arc(x, y, 0, 180, i-10);
}
}
void main()
{
rainbow();
}

Output:

[6]
[Practical File Computer Graphics]

6. Write a program to draw a car:


#include<graphics.h>
void main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm," C:\TurboC++\Disk\TurboC3\BGI");
line(0,300,640,300);
setcolor(4);
circle(100,285,15);
circle(200,285,15);
circle(100,285,5);
circle(200,285,5);
line(65,285,85,285);
line(115,285,185,285);
line(215,285,235,285);
line(65,285,65,260);
line(235,285,235,260);
line(65,260,100,255);
line(235,260,200,255);
line(100,255,115,235);
line(200,255,185,235);
line(115,235,185,235);
line(106,255,118,238);
line(118,238,118,255);
line(106,255,118,255);
line(194,255,182,238);
line(182,238,182,255);
line(194,255,182,255);
line(121,238,121,255);
line(121,238,148,238);
line(121,255,148,255);
line(148,255,148,238);
line(179,238,179,255);
line(179,238,152,238);
line(179,255,152,255);
line(152,255,152,238);
setcolor(3);
getch();
closegraph();
}

[7]
[Practical File Computer Graphics]

Output:

[8]
[Practical File Computer Graphics]

7. Write a program to show reflection in 2D:


#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);

[9]
[Practical File Computer Graphics]

line(x3, getmaxy() - y3, x1,


getmaxy() - y1);
getch();
closegraph();
}
Output:

[10]
[Practical File Computer Graphics]

8. Write a program in C++ to performTranslation:


#include<iostream.h>
#include<graphics.h>
#include<math.h
#include<conio.h>
void main()
{
int gd=DETECT,gm,s;
initgraph(&gd,&gm,(char*)"");
cout<<"Translation " <<endl;
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();
closegraph();
getch();
}

Output:

[11]
[Practical File Computer Graphics]

9. Write a program for Rotation in 2D:


#include<iostream.h>
#include<graphics.h>
#include<math.h>
#include<conio.h>
int main()
{
int gd=DETECT,gm,s;
initgraph(&gd,&gm,(char*)"");
cout<<"Rotation "<<endl;
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();

closegraph();
return 0;
}
Output:

[12]
[Practical File Computer Graphics]

10. Write a program in C++ to perform scaling:


#include <conio.h>
#include <graphics.h>
#include <iostream.h>
#include <math.h>
#include <stdio.h>

int gd = DETECT, gm;


int n, x[100], y[100], i;
float sfx, sfy;
void draw();
void scale();

int main()
{
cout << "Enter no. of sides in polygon: ";
cin >> n;
cout << "Enter coordinates x, y for each vertex: ";
for (i = 0; i < n; i++) {
cin >> x[i] >> y[i];
}
cout << "Enter scale factors: sfx and sfy : ";
cin >> sfx >> sfy;
initgraph(&gd, &gm, (char*)"");
cleardevice();
setcolor(RED);
draw();
scale();
setcolor(YELLOW);
draw();
getch();
closegraph();
return 0;
}
void draw()
{
for (i = 0; i < n; i++) {
line(x[i], y[i], x[(i + 1) % n], y[(i + 1) % n]);
}
}
void scale()
{

[13]
[Practical File Computer Graphics]

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


x[i] = x[0] + (int)((float)(x[i] - x[0]) * sfx);
y[i] = y[0] + (int)((float)(y[i] - y[0]) * sfx);
}
}

Output:

[14]
[Practical File Computer Graphics]

11. Write a program to show shearing in square:


#include <iostream>
#include <graphics.h>
#include <conio.h>
int main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, NULL)
int x1 = 100, y1 = 100, x2 = 200, y2 = 100, x3 = 100, y3 = 200, x4 =
200, y4 = 200;
int shx = 2; // Shearing facto
setcolor(RED);
rectangle(x1, y1, x4, y4);
outtextxy(10, 10, "Before Shearing");
x1 += shx * y1;
x2 += shx * y2;
x3 += shx * y3;
x4 += shx * y4;
setcolor(GREEN);
rectangle(x1, y1, x4, y4);
outtextxy(10, 30, "After Shearing");
getch();
closegraph();
return 0;
}

Output:

[15]
[Practical File Computer Graphics]

12. Program to draw a line using DDA algorithm:


#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);
}

[16]
[Practical File Computer Graphics]

closegraph();
}

Output:

[17]
[Practical File Computer Graphics]

13. Program to draw a circle using mid point algorithm:

#include<stdlib.h>
#include<graphics.h>
#include<dos.h>
#include<conio.h>

void CMP(int xcenter, int ycenter, int radius){


int x = 0;
int y = radius;
int p = 1 - radius;
void plotpoints(int, int , int, int);
plotpoints(xcenter, ycenter, x, y);
while(x<y){
x++;
if(p<0){
p = p + 2*x + 1;
}
else{
y--;
p = p + 2*(x-y) + 1;
}
plotpoints(xcenter, ycenter, x, y);
}
}

void plotpoints(int xcenter, int ycenter, int x, int y){


putpixel(xcenter+x, ycenter+y, WHITE);
putpixel(xcenter-x, ycenter+y, WHITE);
putpixel(xcenter+x, ycenter-y, WHITE);
putpixel(xcenter-x, ycenter-y, WHITE);
putpixel(xcenter+y, ycenter+x, WHITE);
putpixel(xcenter-y, ycenter+x, WHITE);
putpixel(xcenter+y, ycenter-x, WHITE);
putpixel(xcenter-y, ycenter-x, WHITE);
}

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

[18]
[Practical File Computer Graphics]

getch();
return 0;
}

Output:

[19]
[Practical File Computer Graphics]

14. Write program to draw a circle using Bresenham’s algorithm:


#include <stdio.h>
#include <dos.h>
#include <graphics.h>
void drawCircle(int xc, int yc, int x, int y)
{
putpixel(xc+x, yc+y, RED);
putpixel(xc-x, yc+y, RED);
putpixel(xc+x, yc-y, RED);
putpixel(xc-x, yc-y, RED);
putpixel(xc+y, yc+x, RED);
putpixel(xc-y, yc+x, RED);
putpixel(xc+y, yc-x, RED);
putpixel(xc-y, yc-x, RED);
}
void circleBres(int xc, int yc, int r)
{
int x = 0, y = r;
int 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(500);
}
}
int main()
{
int xc = 300, yc =250, r = 100;
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
circleBres(xc, yc, r);
return 0;
}

[20]
[Practical File Computer Graphics]

Output:

[21]
[Practical File Computer Graphics]

15. Draw ellipse using mid point algorithm:


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm;
float p,x,y,xc,yc,a,b;
initgraph(&gd,&gm,"C:\\tc\\bgi");
cleardevice();
printf("Enter xc, yc:\n");
scanf("%f%f",&xc,&yc);
printf("Enter a, b:\n");
scanf("%f%f",&a,&b);
x=0;
y=b;
//Region 1
p=(b*b)-(a*a*b)+(0.25*a*a);
do
{
putpixel(xc+x,yc+y,WHITE);
putpixel(xc+x,yc-y,WHITE);
putpixel(xc-x,yc+y,WHITE);
putpixel(xc-x,yc-y,WHITE);
if(p<0)
{
x=x+1;
p=p+2*b*b*x+b*b;
}
else
{
x=x+1;
y=y-1;
p=p+2*b*b*x-2*a*a*y+b*b;
}
}while(2*b*b*x<2*a*a*y);
//Region 2
p=(b*b*(x+0.5)*(x+0.5))+((y-1)*(y-1)*a*a-a*a*b*b);
do
{
putpixel(xc+x,yc+y,WHITE);
putpixel(xc+x,yc-y,WHITE);

[22]
[Practical File Computer Graphics]

putpixel(xc-x,yc+y,WHITE);
putpixel(xc-x,yc-y,WHITE);
if(p>0)
{
y=y-1;
p=p-2*a*a*y+a*a;
}
else
{
x=x+1;
y=y-1;
p=p-2*a*a*y+2*b*b*x+a*a;
}
}while(y!=0);
getch();
closegraph();
}

Output:

[23]

You might also like