Computer Graphics Practical File
Computer Graphics Practical File
Computer Graphics Practical File
GHARUAN
Computer Graphics
#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
#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
#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
#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;
#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
getch();
closegraph();
}
OUTPUT: - Values inserted on the Console Application
#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;
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);
setcolor(3);
rectangle(xa, ya, xb, yb);
getch();
closegraph();
}
OUTPUT: - Values inserted on the Console Application
#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