0% found this document useful (0 votes)
288 views

Computer Graphics Practical File

The document is a computer graphics program submitted by Ashish Osten to his instructor Mrs. Harmandeep Kaur. It contains 8 programs written in C++ to perform basic graphics operations using Borland Graphics Interface (BGI). The programs draw lines, rectangles, translate and scale objects, and place individual pixels on the screen.

Uploaded by

Fotavo Jugilan
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)
288 views

Computer Graphics Practical File

The document is a computer graphics program submitted by Ashish Osten to his instructor Mrs. Harmandeep Kaur. It contains 8 programs written in C++ to perform basic graphics operations using Borland Graphics Interface (BGI). The programs draw lines, rectangles, translate and scale objects, and place individual pixels on the screen.

Uploaded by

Fotavo Jugilan
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
You are on page 1/ 30

ADESH INSTITUTE OF TECHNOLOGY

GHARUAN

Computer Graphics

Bachelor of Computer Applications

Submitted to: Submitted by:


Mrs. Harmandeep Kaur Ashish Osten

Roll No. – 1732193


6th semester
this page is intentionally left blank
INDEX: -

S.No. Program Page No.

1. Write a C++ program to draw a line. 1-2

Write a C++ program to draw a line using DDA (Digital


2. 3-6
Differential Analyzer) Algorithm.

Write a C++ program to draw a line using Bresenham’s


3. 7-10
Line Algorithm.

4. Write a C++ program to draw a Rectangle. 11-12

5. Write a C++ program to translate (move) a line. 13-16

6. Write a C++ program to translate a Rectangle. 17-20

7. Write a C++ program to Scale an Object. 21-24

8. Write a C++ program to put a Pixel on Screen. 25-26


OUTPUT: - Values inserted on the Console Application

OUTPUT: - A line drawn on BGI (Borland Graphics Interface)


Program 1 - Write a C++ program to draw a line.

#include <iostream>
#include <graphics.h>
using namespace std;

int main()
{
int gd = DETECT, gm; //graphics driver and graphics mode
int x1, y1, x2, y2; // co-ordinates
initgraph(&gd, &gm, "");
cout << "Program to draw a line" << endl
<< endl;
cout << "Enter the starting co-ordinates" << endl;
cout << "x1 = ";
cin >> x1;
cout << "y1 = ";
cin >> y1;
cout << "Enter the ending co-ordinates" << endl;
cout << "x2 = ";
cin >> x2;
cout << "y2 = ";
cin >> y2;
line(x1, y1, x2, y2);
getch();
closegraph();
}
OUTPUT: - Values inserted on the Console Application

OUTPUT: - A line drawn using Digital Differential Analyzer Algorithm


Program 2 - Write a C++ program to draw a line using DDA (Digital Differential Analyzer)
Algorithm.

#include <iostream>
#include <graphics.h>
#include <math.h>
using namespace std;

int main()
{
int gd = DETECT, gm;
int x1, y1, x2, y2; // co-ordinates
int dx, dy, length, i, x, y, x_min, y_min; //for using maths functions
initgraph(&gd, &gm, "");
cout << "Program to draw a line using DDA Algorithm" << endl
<< endl;
cout << "Enter the starting co-ordinates" << endl;
cout << "x1 = ";
cin >> x1;
cout << "y1 = ";
cin >> y1;
cout << "Enter the ending co-ordinates" << endl;
cout << "x2 = ";
cin >> x2;
cout << "y2 = ";
cin >> y2;
dx = x2 - x1;
dy = y2 - y1;
this page is intentionally left blank
if (abs(dx) > abs(dy))
length = abs(dx);
else
length = abs(dy);

x_min = dx / length;
y_min = dy / length;
x = x1;
y = y1;
putpixel(x, y, WHITE);
for (i = 1; i <= length; i++)
{
putpixel(x, y, WHITE);
x = x + x_min;
y = y + y_min;
}
getch();
closegraph();
}
OUTPUT: - Values inserted on the Console Application

OUTPUT: - A line drawn using Bresenham’s Line Algorithm


Program 3 - Write a C++ program to draw a line using Bresenham’s Line Algorithm.

#include <iostream>
#include <graphics.h>
using namespace std;

int main()
{
int gd = DETECT, gm;
int x1, y1, x2, y2;
int dx, dy, i, x, y;
initgraph(&gd, &gm, "");
cout << "Program to draw a line using Bresenham’s Line Algorithm" << endl
<< endl;
cout << "Enter the starting co-ordinates" << endl;
cout << "x1 = ";
cin >> x1;
cout << "y1 = ";
cin >> y1;
cout << "Enter the ending co-ordinates" << endl;
cout << "x2 = ";
cin >> x2;
cout << "y2 = ";
cin >> y2;

dx = x2 - x1;
dy = y2 - y1;
this page is intentionally left blank
i = 2 * dy - dx;

x = x1, y = y1;
putpixel(x, y, 15);
while (x <= x2)
{
if (i < 0)
{
x = x + 1;
i = i + 2 * dy;
}
else
{
x = x + 1;
y = y + 1;
i = i + 2 * (dy - dx);
}
putpixel(x, y, 15);
}
getch();
closegraph();
}
OUTPUT: - Values inserted on the Console Application

OUTPUT: - A rectangle drawn on BGI (Borland Graphics Interface)


Program 4 - Write a C++ program to draw a Rectangle.

#include <iostream>
#include <graphics.h>
using namespace std;

int main()
{
int gd = DETECT, gm;
int r_left, r_top, r_bottom, r_right;
initgraph(&gd, &gm, "");
cout << "Program to draw a Rectangle" << endl
<< endl;
cout << "Enter the co-ordinates" << endl;
cout << "left = ";
cin >> r_left;
cout << "top = ";
cin >> r_top;
cout << "and" << endl;
cout << "bottom = ";
cin >> r_bottom;
cout << "right = ";
cin >> r_right;

rectangle(r_left, r_top, r_bottom, r_right); //dimensions of rectangle


getch();
closegraph();
}
OUTPUT: - Values inserted on the Console Application

OUTPUT: - A line translated(transformed) on BGI (Borland Graphics Interface)


Program 5 - Write a C++ program to translate (move) a line.

#include <iostream>
#include <graphics.h>
using namespace std;

int main()
{
int gd = DETECT, gm;
int x1, y1, x2, y2; //original co-ordinates
int dx1, dx2; // new co-ordinates
initgraph(&gd, &gm, "");
cout << "Program to translate a line" << endl
<< endl;
cout << "Enter the starting co-ordinates" << endl;
cout << "x1 = ";
cin >> x1;
cout << "y1 = ";
cin >> y1;
cout << "Enter the ending co-ordinates" << endl;
cout << "x2 = ";
cin >> x2;
cout << "y2 = ";
cin >> y2;
line(x1, y1, x2, y2); // original line

cout << "Enter the new starting co-ordinates" << endl;


cout << "dx1 = ";
this page is intentionally left blank
cin >> dx1;
cout << "dx2 = ";
cin >> dx2;
setcolor(3);
line(x1 + dx1, y1, x2 + dx2, y2); //new line

getch();
closegraph();
}
OUTPUT: - Values inserted on the Console Application

OUTPUT: - A rectangle translated(transformed) on BGI (Borland Graphics Interface)


Program 6 - Write a C++ program to translate a Rectangle.

#include <iostream>
#include <graphics.h>
using namespace std;

int main()
{
int gd = DETECT, gm;
int r_left, r_top, r_bottom, r_right;
int x, y; // translating co-ordinates
initgraph(&gd, &gm, "");
cout << "Program to translate a Rectangle" << endl
<< endl;
cout << "Enter the co-ordinates" << endl;
cout << "left = ";
cin >> r_left;
cout << "top = ";
cin >> r_top;
cout << "and" << endl;
cout << "bottom = ";
cin >> r_bottom;
cout << "right = ";
cin >> r_right;

rectangle(r_left, r_top, r_bottom, r_right); //dimensions of rectangle

cout << "Enter the new co-ordinates" << endl;


this page is intentionally left blank
cout << "x = ";
cin >> x;
cout << "y = ";
cin >> y;
setcolor(3);
rectangle(r_left + x, r_top + y, r_bottom + x, r_right + y);
getch();
closegraph();
}
OUTPUT: - Values inserted on the Console Application

OUTPUT: - A rectangle scaled(transformed) on BGI (Borland Graphics Interface)


Program 7 - Write a C++ program to Scale an Object.
#include <iostream>
#include <graphics.h>
using namespace std;

int main()
{
int gd = DETECT, gm;
int x1 = 100, y1 = 100, x2 = 200, y2 = 200; //original co-ordinates
int fx, fy; // fixed points
int sx, sy; // scaling factors
int xa, ya, xb, yb; // new co-ordinates
initgraph(&gd, &gm, "");
cout << "Program to Scale an Object" << endl
<< endl;
rectangle(x1, y1, x2, y2);

cout << "Enter the new fixed points" << endl;


cout << "fixed point for x = ";
cin >> fx;
cout << "fixed point for y = ";
cin >> fy;

cout << "Enter the scaling factors" << endl;


cout << "scaling factor for x = ";
cin >> sx;
cout << "scaling factor for y = ";
cin >> sy;
this page is intentionally left blank
xa = (x1 * sx) + (fx * (1 - sx));
ya = (y1 * sy) + (fy * (1 - sy));
xb = (x2 * sx) + (fx * (1 - sx));
yb = (y2 * sy) + (fy * (1 - sy));

setcolor(3);
rectangle(xa, ya, xb, yb);
getch();
closegraph();
}
OUTPUT: - Values inserted on the Console Application

OUTPUT: - A pixel illustrated on BGI (Borland Graphics Interface)


Program 8 - Write a C++ program to put a Pixel on Screen.

#include <iostream>
#include <graphics.h>
using namespace std;

int main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
cout << "Program to put a Pixel on Screen" << endl
<< endl;
putpixel(50, 50, WHITE);
getch();
closegraph();
}
A Practical File by Ashish Osten

You might also like