0% found this document useful (0 votes)
48 views44 pages

Dipesh File PDF

Uploaded by

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

Dipesh File PDF

Uploaded by

dipeshkaushik720
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

A

PRACTICAL FILE
ON
COMPUTER
GRAPHICS
(Session 2024-25)

Submitted To: Submitted By:


Ms. Bhawana Name : Dipesh

(Assistant Professor Class : BCA final year

In University roll no. : 221602014039


Computer Science ) College roll no.:1221733011040

Department of Management and


Computer Science ,
Vaish College , Bhiwani
Index
S no. Particulars Page no. Signature

1. Program to Draw line

2. Program to Draw circle

3. Program to Draw rectangle

4. Program to Implementation
Flood Fill
5. Program to draw a Ellipse

6. Program to Implement 2-D


Mirror Reflection
7. Program to Draw a smile

8. Program to Implement 2-D


shearing
9. Program to Draw circle using
Bresenham’s Algorithm
10. Program to Implement 2-D
translation

1.Program to draw a line


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

int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
line(100, 100, 300, 300);

getch();
closegraph();

return 0;
}
Output: A straight line drawn diagonally from the point
(100, 100) to (300, 300).
2.Program to draw a circle
#include <graphics.h>
#include <conio.h>

int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "");

circle(250, 250, 100);


getch();
closegraph();

return 0;
}
Output: A white circle appears centered at (250, 250) with
a radius of 100.
3.Program to draw a rectangle
#include <graphics.h>
#include <conio.h>

int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "");

rectangle(150, 100, 400, 300);

getch();
closegraph();

return 0;
}
Output: A white rectangle drawn from coordinates (150,
100) to (400, 300).
4.Program to Implementation flood fill
#include <graphics.h>
#include <conio.h>

void floodFill(int x, int y, int fill_color, int boundary_color) {


int current = getpixel(x, y);
if (current != boundary_color && current != fill_color) {
putpixel(x, y, fill_color);
floodFill(x + 1, y, fill_color, boundary_color);
floodFill(x - 1, y, fill_color, boundary_color);
floodFill(x, y + 1, fill_color, boundary_color);
floodFill(x, y - 1, fill_color, boundary_color);
}
}

int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
circle(250, 250, 100);
floodFill(250, 250, RED, WHITE);

getch();
closegraph();
return 0;
}
Output:
5.Program to draw a Ellipse
#include <graphics.h>
#include <conio.h>

int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "");

ellipse(300, 250, 0, 360, 100, 50);

getch();
closegraph();

return 0;
}
 Output: This draws a full ellipse (0 to 360 degrees)
centered at (300, 250) with:
o horizontal radius = 100
o vertical radius = 50
6.Program to Implement 2-D mirror reflection
#include <graphics.h>
#include <conio.h>

int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
int x1 = 200, y1 = 200;
int x2 = 250, y2 = 100;
int x3 = 300, y3 = 200;
line(0, getmaxy()/2, getmaxx(), getmaxy()/2);
line(getmaxx()/2, 0, getmaxx()/2, getmaxy());

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

int ry1 = getmaxy() - y1;


int ry2 = getmaxy() - y2;
int ry3 = getmaxy() - y3;
setcolor(RED);
line(x1, ry1, x2, ry2);
line(x2, ry2, x3, ry3);
line(x3, ry3, x1, ry1);

int rx1 = getmaxx() - x1;


int rx2 = getmaxx() - x2;
int rx3 = getmaxx() - x3;
setcolor(GREEN);
line(rx1, y1, rx2, y2);
line(rx2, y2, rx3, y3);
line(rx3, y3, rx1, y1);

getch();
closegraph();
return 0;
}
.
Output: Original triangle in yellow

. Mirror image over X-axis (horizontal flip) in red


. Mirror image over Y-axis (vertical flip) in green
. X and Y axes for reference
7.Program to draw a smile
#include <graphics.h>
#include <conio.h>

int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "");

setcolor(YELLOW);
setfillstyle(SOLID_FILL, YELLOW);
circle(250, 250, 100);
floodfill(250, 250, YELLOW);
setcolor(BLACK);
setfillstyle(SOLID_FILL, BLACK);
circle(220, 220, 10);
floodfill(220, 220, BLACK);
circle(280, 220, 10);
floodfill(280, 220, BLACK);

arc(250, 250, 200, 340, 50);

getch();
closegraph();
return 0;
}
output:
8.Program to Implement 2-D shearing
#include <graphics.h>
#include <iostream>
using namespace std;

void drawShape(int x[], int y[], int n, int color) {


setcolor(color);
for (int i = 0; i < n; i++) {
line(x[i], y[i], x[(i + 1) % n], y[(i + 1) % n]);
}
}

int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "");

A rectangle
int x[4] = {100, 200, 200, 100};
int y[4] = {100, 100, 150, 150};
drawShape(x, y, 4, WHITE);

int choice;
float shx = 0, shy = 0;
cout << "2D Shearing Transformation\n";
cout << "1. Shear in X-direction\n";
cout << "2. Shear in Y-direction\n";
cout << "3. Shear in both X and Y\n";
cout << "Enter your choice: ";
cin >> choice;

switch (choice) {
case 1:
cout << "Enter shearing factor in X-direction (shx): ";
cin >> shx;
for (int i = 0; i < 4; i++) {
x[i] = x[i] + shx * y[i];
}
break;

case 2:
cout << "Enter shearing factor in Y-direction (shy): ";
cin >> shy;
for (int i = 0; i < 4; i++) {
y[i] = y[i] + shy * x[i];
}
break;

case 3:
cout << "Enter shearing factor in X-direction (shx): ";
cin >> shx;
cout << "Enter shearing factor in Y-direction (shy): ";
cin >> shy;
for (int i = 0; i < 4; i++) {
int tempX = x[i];
x[i] = x[i] + shx * y[i];
y[i] = y[i] + shy * tempX;
}
break;

default:
cout << "Invalid choice!";
getch();
closegraph();
return 0;
}

drawShape(x, y, 4, YELLOW);

getch();
closegraph();
return 0;
}
Output:
9.Program to Draw circle using Bresenham’s Algorithm
#include <graphics.h>
#include <conio.h>
#include <iostream>

using namespace std;

void plotCirclePoints(int xc, int yc, int x, int y) {


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

void bresenhamCircle(int xc, int yc, int r) {


int x = 0;
int y = r;
int d = 3 - 2 * r;

while (x <= y) {
plotCirclePoints(xc, yc, x, y);
if (d < 0) {
d = d + 4 * x + 6;
} else {
d = d + 4 * (x - y) + 10;
y--;
}
x++;
}
}

int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, (char*)"");
int xc, yc, r;
cout << "Enter center of circle (xc, yc): ";
cin >> xc >> yc;
cout << "Enter radius of circle: ";
cin >> r;

bresenhamCircle(xc, yc, r);

getch();
closegraph();
return 0;
}
Output:
Bresenham's Circle Algorithm :
It uses integer arithmetic to efficiently plot a circle by
calculating pixels in one octant and reflecting them across
all eight octants.
10.Program to implement 2-D Translation
#include <graphics.h>
#include <conio.h>
#include <iostream>

using namespace std;

void drawTriangle(int x1, int y1, int x2, int y2, int x3, int y3,
int color) {
setcolor(color);
line(x1, y1, x2, y2);
line(x2, y2, x3, y3);
line(x3, y3, x1, y1);
}

int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, (char*)"");
int x1 = 100, y1 = 100;
int x2 = 150, y2 = 50;
int x3 = 200, y3 = 100;

drawTriangle(x1, y1, x2, y2, x3, y3, WHITE);


outtextxy(x1, y1 - 20, (char*)"Original");

int tx, ty;


cout << "Enter translation in X (tx): ";
cin >> tx;
cout << "Enter translation in Y (ty): ";
cin >> ty;

int tx1 = x1 + tx, ty1 = y1 + ty;


int tx2 = x2 + tx, ty2 = y2 + ty;
int tx3 = x3 + tx, ty3 = y3 + ty;

drawTriangle(tx1, ty1, tx2, ty2, tx3, ty3, GREEN);


outtextxy(tx1, ty1 - 20, (char*)"Translated");

getch();
closegraph();
}
Output:

You might also like