CG Lab File
CG Lab File
PRACTICAL FILE
SUBJECT NAME : COMPUTER GRAPHICS LAB
SUBJECT CODE : PBC 601
COURSE : BCA
SEMESTER : VI
SESSION : 2024-25
SUBMITTED TO SUBMITTED BY
Mr. Malay Tripathi Varsha Pandey
DEPARTMENT OF COMPUTER SCIENCE
& APPLICATION
NAME OF EXAMINER:
SIGNATURE OF EXAMINER:
DEPARTMENT
STUDENT LAB REPORT SHEET
Computer Graphics Lab (PBC-601)
Local Address……………………………………………………..……Email…………………………………………………..…………..
Grade A B C
Marks 5 3 1
S.No. Name of the Experiment D.O.P. Date of Grade Grade Total Marks Student’s Teacher’s
Submission (Viva) (Report (out of 10) Signature Signature
File)
10
11
12
13
14
15
I would like to express my gratitude to Mr. Malay Tripathi for their guidance and
support throughout the completion of this practical file. Their valuable insights and
encouragement have been instrumental in enhancing my understanding of the
subject. Would also like to acknowledge my friends for their collaboration and help.
Thank you all for your contributions to the successful completion of this practical
work.
Varsha Pandey
[email protected]
PROGRAM - 1
NAME :- Varsha Pandey
COURSE :- BCA
SEMESTER :- VI
ROLL NO. :- 2292247
DATE :- 08/04/2025
PROGRAM:
#include<graphics.h>
#include<conio.h>
#include<stdio.h>
void main()
{
intgd = DETECT ,gm, i;
float x, y,dx,dy,steps;
int x0, x1, y0, y1;
initgraph(&gd, &gm, "C:\\TC\\BGI");
setbkcolor(WHITE);
x0 = 100 , y0 = 200, x1 = 500, y1 = 300;
dx = (float)(x1 - x0);
dy = (float)(y1 - y0);
if(dx>=dy)
{
steps = dx;
}
else
{
steps = dy;
}
dx = dx/steps;
dy = dy/steps;
x = x0;
y = y0;
i = 1;
while(i<= steps)
{
putpixel(x, y, RED);
x += dx;
y += dy;
i=i+1;
}
getch();
closegraph();
}
OUTPUT:
PROGRAM - 2
NAME :- Varsha Pandey
COURSE :- BCA
SEMESTER :- VI
ROLL NO. :- 2292247
DATE :- 08/04/2025
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;
}
OUTPUT:
PROGRAM - 3
NAME :- Varsha Pandey
COURSE :- BCA
SEMESTER :- VI
ROLL NO. :- 2292247
DATE :- 15/04/2025
PROGRAM:
#include <graphics.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>
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, " "); /
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;
}
OUTPUT:
PROGRAM - 4
NAME :- Varsha Pandey
COURSE :- BCA
SEMESTER :- VI
ROLL NO. :- 2292247
DATE :- 15/04/2025
PROGRAM:
#include <stdio.h>
#include <conio.h>
#include <math.h>
#include <graphics.h>
void main(){
int gd=DETECT,gm;
int xcenter,ycenter,radius;
int p,x,y;
initgraph(&gd,&gm,"c:\turboc3\bgi");
x=0;
printf("n Enter radius of a circle :");
scanf("%d",&radius);
y=radius;
printf("n Enter co-ordinates of center(x and y) : ");
scanf("%d%d",&xcenter,&ycenter);
plotpoints(xcenter,ycenter,x,y);
p=1-radius;
while(x<y)
{
if(p<0)
x=x+1;
else
{
x=x+1;
y=y-1;
}
if(p<0)
p=p+2*x+1;
else p=p+2*(x-y)+1;
plotpoints(xcenter,ycenter,x,y);
}
getch();
}
int plotpoints(int xcenter,int ycenter,int x,int y)
{
putpixel(xcenter+x,ycenter+y,1);
putpixel(xcenter-x,ycenter+y,1);
putpixel(xcenter+x,ycenter-y,1);
putpixel(xcenter-x,ycenter-y,1);
putpixel(xcenter+y,ycenter+x,1);
putpixel(xcenter-y,ycenter+x,1);
putpixel(xcenter+y,ycenter-x,1);
putpixel(xcenter-y,ycenter-x,1);
}
OUTPUT:
PROGRAM - 5
NAME :- Varsha Pandey
COURSE :- BCA
SEMESTER :- VI
ROLL NO. :- 2292247
DATE :- 22/04/2025
PROGRAM:
#include <graphics.h>
#include <conio.h>
#include <stdio.h>
void EllipseMid(int xc, int yc, int rx, int ry) {
float x = 0, y = ry;
float rx2 = rx * rx, ry2 = ry * ry;
float twoRx2 = 2 * rx2, twoRy2 = 2 * ry2;
float px = 0;
float py = twoRx2 * y;
float p1 = ry2 - (rx2 * ry) + (0.25 * rx2);
while (px < py) {
putpixel(xc + x, yc + y, WHITE);
putpixel(xc - x, yc + y, WHITE);
putpixel(xc + x, yc - y, WHITE);
putpixel(xc - x, yc - y, WHITE);
x++;
px += twoRy2;
if (p1 < 0) {
p1 += ry2 + px;
} else {
y--;
py -= twoRx2;
p1 += ry2 + px - py;
}
}
float p2 = (ry2) * (x + 0.5) * (x + 0.5) +
(rx2) * (y - 1) * (y - 1) -
(rx2 * ry2);
while (y >= 0) {
putpixel(xc + x, yc + y, WHITE);
putpixel(xc - x, yc + y, WHITE);
putpixel(xc + x, yc - y, WHITE);
putpixel(xc - x, yc - y, WHITE);
y--;
py -= twoRx2;
if (p2 > 0) {
p2 += rx2 - py;
} else {
x++;
px += twoRy2;
p2 += rx2 - py + px;
}
}
}
int main() {
int gd = DETECT, gm;
int xc, yc, rx, ry;
printf("Enter center (xc yc): ");
scanf("%d%d", &xc, &yc);
printf("Enter (rx): ");
scanf("%d", &rx);
printf("Enter (ry): ");
scanf("%d", &ry);
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
EllipseMid(xc, yc, rx, ry);
getch();
closegraph();
return 0;
}
OUTPUT:
PROGRAM - 6
NAME :- Varsha Pandey
COURSE :- BCA
SEMESTER :- VI
ROLL NO. :- 2292247
DATE :- 22/04/2025
OBJECTIVE : Write a program to implement Flood Fill Algorithm and Boundary Fill
Algorithm.
PROGRAM:
#include <graphics.h>
#include <conio.h>
void floodFill8(int x, int y, int oldColor, int newColor) {
int current = getpixel(x, y);
if (current == oldColor) {
putpixel(x, y, newColor);
floodFill8(x + 1, y, oldColor, newColor);
floodFill8(x - 1, y, oldColor, newColor);
floodFill8(x, y + 1, oldColor, newColor);
floodFill8(x, y - 1, oldColor, newColor);
floodFill8(x + 1, y + 1, oldColor, newColor);
floodFill8(x - 1, y - 1, oldColor, newColor);
floodFill8(x + 1, y - 1, oldColor, newColor);
floodFill8(x - 1, y + 1, oldColor, newColor);
}
}
void boundaryFill8(int x, int y, int fillColor, int boundaryColor) {
int current = getpixel(x, y);
if (current != boundaryColor && current != fillColor) {
putpixel(x, y, fillColor);
boundaryFill8(x + 1, y, fillColor, boundaryColor);
boundaryFill8(x - 1, y, fillColor, boundaryColor);
boundaryFill8(x, y + 1, fillColor, boundaryColor);
boundaryFill8(x, y - 1, fillColor, boundaryColor);
boundaryFill8(x + 1, y + 1, fillColor, boundaryColor);
boundaryFill8(x - 1, y - 1, fillColor, boundaryColor);
boundaryFill8(x + 1, y - 1, fillColor, boundaryColor);
boundaryFill8(x - 1, y + 1, fillColor, boundaryColor);
}
}
int main() {
int gd = DETECT, gm;
int choice;
initgraph(&gd, &gm, "");
rectangle(100, 100, 300, 200);
printf("Choose fill algorithm:\n1. Flood Fill (8-connected)\n2. Boundary Fill (8-connected)\
nChoice: ");
scanf("%d", &choice);
if (choice == 1) {
floodFill8(150, 150, BLACK, GREEN);
} else if (choice == 2) {
boundaryFill8(150, 150, RED, WHITE);
} else {
printf("Invalid choice!");
}
getch();
closegraph();
return 0;
}
OUTPUT:
PROGRAM - 7
NAME :- Varsha Pandey
COURSE :- BCA
SEMESTER :- VI
ROLL NO. :- 2292247
DATE :- 22/04/2025
PROGRAM:
#include <graphics.h>
#include <conio.h>
#include <stdio.h>
void scanFill(int x[], int y[], int n) {
int i, j, temp;
int polyYmax = 0, polyYmin = 479;
int inter[20];
for (i = 0; i < n; i++) {
if (y[i] > polyYmax)
polyYmax = y[i];
if (y[i] < polyYmin)
polyYmin = y[i];
}
for (int scanline = polyYmin; scanline <= polyYmax; scanline++) {
int k = 0;
for (i = 0; i < n; i++) {
j = (i + 1) % n;
if ((y[i] <= scanline && y[j] > scanline) || (y[j] <= scanline && y[i] > scanline)) {
int x_intersect = x[i] + (float)(scanline - y[i]) * (x[j] - x[i]) / (y[j] - y[i]);
inter[k++] = x_intersect;
}
}
for (i = 0; i < k - 1; i++) {
for (j = 0; j < k - 1 - i; j++) {
if (inter[j] > inter[j + 1]) {
temp = inter[j];
inter[j] = inter[j + 1];
inter[j + 1] = temp;
}
}
}
for (i = 0; i < k; i += 2) {
line(inter[i], scanline, inter[i + 1], scanline);
}
}
}
int main() {
int gd = DETECT, gm;
int x[10], y[10], n, i;
initgraph(&gd, &gm, "");
printf("Enter number of vertices (max 10): ");
scanf("%d", &n);
for (i = 0; i < n; i++) {
printf("Enter coordinates of vertex %d (x y): ", i + 1);
scanf("%d%d", &x[i], &y[i]);
}
for (i = 0; i < n; i++) {
line(x[i], y[i], x[(i + 1) % n], y[(i + 1) % n]);
}
scanFill(x, y, n);
getch();
closegraph();
return 0;
}
OUTPUT:
PROGRAM - 8
NAME :- Varsha Pandey
COURSE :- BCA
SEMESTER :- VI
ROLL NO. :- 2292247
DATE :- 13/05/2025
PROGRAM:
#include <graphics.h>
#include <conio.h>
#include <stdio.h>
int main() {
int gd = DETECT, gm;
int x1, y1, x2, y2;
int tx, ty;
initgraph(&gd, &gm, "");
printf("Enter top-left corner (x1 y1): ");
scanf("%d %d", &x1, &y1);
printf("Enter bottom-right corner (x2 y2): ");
scanf("%d %d", &x2, &y2);
rectangle(x1, y1, x2, y2);
outtextxy(x1, y1 - 20, "Original Rectangle");
printf("Enter translation factors tx and ty: ");
scanf("%d %d", &tx, &ty);
int newX1 = x1 + tx;
int newY1 = y1 + ty;
int newX2 = x2 + tx;
int newY2 = y2 + ty;
rectangle(newX1, newY1, newX2, newY2);
outtextxy(newX1, newY1 - 20, "Translated Rectangle");
getch();
closegraph();
return 0;
}
OUTPUT:
PROGRAM - 9
NAME :- Varsha Pandey
COURSE :- BCA
SEMESTER :- VI
ROLL NO. :- 2292247
DATE :- 13/05/2025
PROGRAM:
#include <graphics.h>
#include <conio.h>
#include <stdio.h>
int main() {
int gd = DETECT, gm;
int x1, y1, x2, y2;
float sx, sy;
initgraph(&gd, &gm, "");
printf("Enter top-left corner (x1 y1): ");
scanf("%d %d", &x1, &y1);
printf("Enter bottom-right corner (x2 y2): ");
scanf("%d %d", &x2, &y2);
rectangle(x1, y1, x2, y2);
outtextxy(x1, y1 - 20, "Original Rectangle");
printf("Enter scaling factors (sx sy): ");
scanf("%f %f", &sx, &sy);
int sx1 = x1;
int sy1 = y1;
int sx2 = x1 + (x2 - x1) * sx;
int sy2 = y1 + (y2 - y1) * sy;
rectangle(sx1, sy1, sx2, sy2);
outtextxy(sx1, sy1 - 20, "Scaled Rectangle");
getch();
closegraph();
return 0;
}
OUTPUT:
PROGRAM - 10
NAME :- Varsha Pandey
COURSE :- BCA
SEMESTER :- VI
ROLL NO. :- 2292247
DATE :- 13/05/2025
PROGRAM:
#include <graphics.h>
#include <conio.h>
#include <stdio.h>
#include <math.h>
int main() {
int gd = DETECT, gm;
int x[3], y[3];
float angle;
float rad;
int xr[3], yr[3];
initgraph(&gd, &gm, "");
printf("Enter coordinates of triangle:\n");
for (int i = 0; i < 3; i++) {
printf("Point %d (x y): ", i + 1);
scanf("%d %d", &x[i], &y[i]);
}
printf("Enter angle of rotation (in degrees): ");
scanf("%f", &angle);
rad = angle * M_PI / 180;
for (int i = 0; i < 3; i++) {
xr[i] = round(x[i] * cos(rad) - y[i] * sin(rad));
yr[i] = round(x[i] * sin(rad) + y[i] * cos(rad));
}
line(x[0], y[0], x[1], y[1]);
line(x[1], y[1], x[2], y[2]);
line(x[2], y[2], x[0], y[0]);
outtextxy(x[0] - 40, y[0] - 30, "Original Triangle");
setcolor(RED);
line(xr[0], yr[0], xr[1], yr[1]);
line(xr[1], yr[1], xr[2], yr[2]);
line(xr[2], yr[2], xr[0], yr[0]);
outtextxy(xr[0] - 40, yr[0] - 30, "Rotated Triangle");
getch();
closegraph();
return 0;
}
OUTPUT:
PROGRAM - 11
NAME :- Varsha Pandey
COURSE :- BCA
SEMESTER :- VI
ROLL NO. :- 2292247
DATE :- 13/05/2025
PROGRAM:
#include <graphics.h>
#include <conio.h>
#include <stdio.h>
void drawTriangle(int x[], int y[], int color) {
setcolor(color);
line(x[0], y[0], x[1], y[1]);
line(x[1], y[1], x[2], y[2]);
line(x[2], y[2], x[0], y[0]);
}
int main() {
int gd = DETECT, gm;
int x[3], y[3];
int rx[3], ry[3];
int choice;
initgraph(&gd, &gm, "");
printf("Enter coordinates of the triangle (x y):\n");
for (int i = 0; i < 3; i++) {
printf("Point %d: ", i + 1);
scanf("%d %d", &x[i], &y[i]);
}
drawTriangle(x, y, WHITE);
outtextxy(x[0] - 40, y[0] - 30, "Original Triangle");
printf("\nReflection options:\n");
printf("1. Reflection about X-axis\n");
printf("2. Reflection about Y-axis\n");
printf("3. Reflection about Origin\n");
printf("Enter your choice: ");
scanf("%d", &choice);
for (int i = 0; i < 3; i++) {
switch (choice) {
case 1:
rx[i] = x[i];
ry[i] = getmaxy() - y[i];
break;
case 2:
rx[i] = getmaxx() - x[i];
ry[i] = y[i];
break;
case 3:
rx[i] = getmaxx() - x[i];
ry[i] = getmaxy() - y[i];
break;
default:
printf("Invalid choice!\n");
closegraph();
return 0;
}
}
setcolor(RED);
drawTriangle(rx, ry, RED);
outtextxy(rx[0] - 60, ry[0] - 30, "Reflected Triangle");
getch();
closegraph();
return 0;
}
OUTPUT:
PROGRAM - 12
NAME :- Varsha Pandey
COURSE :- BCA
SEMESTER :- VI
ROLL NO. :- 2292247
DATE :- 13/05/2025
PROGRAM:
#include <graphics.h>
#include <conio.h>
#include <stdio.h>
void drawRectangle(int x[], int y[], int color, char label[]) {
setcolor(color);
line(x[0], y[0], x[1], y[1]);
line(x[1], y[1], x[2], y[2]);
line(x[2], y[2], x[3], y[3]);
line(x[3], y[3], x[0], y[0]);
outtextxy(x[0], y[0] - 20, label);
}
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
int x[4], y[4];
int shearedX[4], shearedY[4];
int shx = 0, shy = 0;
int choice;
x[0] = 100; y[0] = 100;
x[1] = 200; y[1] = 100;
x[2] = 200; y[2] = 150;
x[3] = 100; y[3] = 150;
drawRectangle(x, y, WHITE, "Original Rectangle");
printf("Choose shearing transformation:\n");
printf("1. Shear in X-direction\n");
printf("2. Shear in Y-direction\n");
printf("Enter choice: ");
scanf("%d", &choice);
if (choice == 1) {
printf("Enter shear factor in X-direction: ");
scanf("%d", &shx);
} else if (choice == 2) {
printf("Enter shear factor in Y-direction: ");
scanf("%d", ­);
} else {
printf("Invalid choice!\n");
closegraph();
return 0;
}
int refX = x[3];
int refY = y[3];
for (int i = 0; i < 4; i++) {
int tx = x[i] - refX;
int ty = y[i] - refY;
int sx = tx + shx * ty;
int sy = ty + shy * tx;
shearedX[i] = sx + refX;
shearedY[i] = sy + refY;
}
drawRectangle(shearedX, shearedY, RED, "Sheared Rectangle");
getch();
closegraph();
return 0;
}
OUTPUT: