CG Practicals
CG Practicals
Practical 1:
Write a program to implement Bresenhams line drawing algorithm.
#include <iostream>
#include <iomanip>
#include <math.h>
#include <graphics.h>
// Initial value of d
double d = 2*dy - dx;
// Output to terminal
cout << "\ni\tPixel\td\tPlotted Values" << endl;
cout << "-------------------------------------" << endl;
cout << "0\t \t \t" << "(" << round(x1) << "," << round(y1) << ")" << endl;
double x = x1;
double y = y1;
for(int i = 1; ; i++) {
double d_temp = d;
// Choose NE pixel
if (d>0){
d = d + a + b;
x = x + 1;
y = y + 1;
pixel = "NE";
}
// Choose E pixel
else {
d = d + a;
x = x + 1;
pixel = "E";
}
// Exit condition
if (x > x2 || y > y2) break;
// draw pixel
putpixel(x + x_origin, -y + y_origin, 15);
// Output to terminal
cout << i << "\t" << pixel << "\t" << d_temp << "\t" << "(" << round(x) << "," <<
round(y) << ")" << endl;
}
// Clean up
getch();
closegraph();
int main()
{
int x1 = -200;
int y1 = -100;
int x2 = 200;
int y2 = 180;
BresenhamLine(x1, y1, x2, y2);
return 0;
}
OUTPUT:
Practical 2:
Write a program to implement mid-point circle drawing algorithm .
#include <cmath>
#include <cstdlib>
#include <graphics.h>
#include <iostream>
// Setup
int win = initwindow(400, 300, "Bresenham Circle Drawing Algorithm Example");
setcurrentwindow(win);
// Initial value of d
int d = round(5/4 - r);
// Output to terminal
cout << "\nIst OCTANT\n-------------" << endl;
cout << "\ni\tPixel\td\tPlotted Values" << endl;
cout << "-------------------------------------" << endl;
cout << "0\t \t \t" << "(" << x1 << "," << y1+r << ")" << endl;
int i = 0;
string pixel = "";
int x = 0;
int y = r;
while (y >= x)
{
i = i + 1;
int d_temp = d;
// Choose E pixel
if (d < 0)
{
d += 2 * x + 3;
x += 1;
pixel = 'E';
}
// Choose SE pixel
else
{
d += 2 * (x - y) + 5;
x += 1;
y -= 1;
pixel = "SE";
}
drawCirclePixels(x_c, y_c, x, -y, 15);
// Output to terminal
cout << i << "\t" << pixel << "\t" << d_temp << "\t" << "(" << x << "," << y << ")" << endl;
}
cout << endl;
// Clean up
getch();
closegraph();
}
int main(void)
{
int x, y, r;
cout << "Enter Centre (x y): ";
cin >> x >> y;
cout << "Enter Radius (r): ";
cin >> r;
BresenhamCircle(x, y, r);
return 0;
}
OUTPUT:
PRACTICAL 3
Write a program to clip a line using Cohen and Sutherland line clipping algorithm.
// Global Variables
int xmin, xmax, ymin, ymax;
// Finding Bits
bits[0] = sign(xmin - mylines.x1);
bite[0] = sign(xmin - mylines.x2);
bits[1] = sign(mylines.x1 - xmax);
bite[1] = sign(mylines.x2 - xmax);
bits[2] = sign(ymin - mylines.y1);
bite[2] = sign(ymin - mylines.y2);
bits[3] = sign(mylines.y1 - ymax);
bite[3] = sign(mylines.y2 - ymax);
// initial will used for initial coordinates and end for final
string initial = "", end = "", temp = "";
// convert bits to string
for (i = 0; i < 4; i++) {
if (bits[i] == 0)
initial += '0';
else
initial += '1';
}
for (i = 0; i < 4; i++) {
if (bite[i] == 0)
end += '0';
else
end += '1';
}
// if both points are inside the Accept the line and draw
if (initial == end && end == "0000") {
// inbuild function to draw the line from(x1, y1) to (x2, y2)
line(mylines.x1, mylines.y1, mylines.x2, mylines.y2);
return;
}
// this will contain cases where line maybe totally outside for partially inside
else {
// taking bitwise end of every value
for (i = 0; i < 4; i++) {
// Driver Function
int main()
{
int gd = DETECT, gm;
// Setup
int win = initwindow(400, 300, "Line Clipping Example");
setcurrentwindow(win);
// Drawing Window using Lines
line(xmin, ymin, xmax, ymin);
line(xmax, ymin, xmax, ymax);
line(xmax, ymax, xmin, ymax);
line(xmin, ymax, xmin, ymin);
mylines[1].x1 = 120;
mylines[1].y1 = 40;
mylines[1].x2 = 200;
mylines[1].y2 = 180;
mylines[2].x1 = 120;
mylines[2].y1 = 200;
mylines[2].x2 = 160;
mylines[2].y2 = 140;
mylines[3].x1 = 170;
mylines[3].y1 = 100;
mylines[3].x2 = 240;
mylines[3].y2 = 150;
OUTPUT:
PRACTICAL 4:
Write a program to clip a polygon using Sutherland Hodgeman algorithm.
#include<iostream>
#include <graphics.h>
using namespace std;
new_points[new_poly_size][0] = kx;
new_points[new_poly_size][1] = ky;
new_poly_size++;
}
getch();
}
//Driver code
int main()
{
int gd = DETECT, gm, errorcode;
initgraph(&gd, &gm, NULL);
setcolor(YELLOW);
int poly[50];
for (int q = 0; q < poly_size; q++)
{
for (int t = 0; t < 2; t++)
{
poly[q * 2 + t] = poly_points[q][t];
}
}
poly[2 * poly_size] = poly[0];
poly[2 * poly_size + 1] = poly[1];
drawpoly(poly_size + 1, poly);
return 0;
}
OUTPUT:
PRACTICAL 5:
Write a program to fill a polygon using Scan line fill algorithm.
#include <iostream>
#include <vector>
#include <algorithm>
#include <math.h>
#include "dda.cpp"
#include <conio.h>
#include <math.h>
#include <cmath>
#include <graphics.h>
struct Node {
};
void addEdge(vector<vector<Node> >& adjList, int u, int v, pair<int, int> point_u, pair<int, int>
point_v) {
adjList[u].push_back(Node(v, point_v));
adjList[v].push_back(Node(u, point_u));
class EdgeNode
public:
int vertex2;
double x;
double y;
double m_inv;
EdgeNode *ptr;
EdgeNode(){
this->x = 0.0;
this->y = 0.0;
this->m_inv = 0.0;
ptr = NULL;
};
class vertex_ptr{
public:
EdgeNode *ptr;
vertex_ptr(){
ptr = NULL;
};
class array_linked_list
public:
vertex_ptr *arr;
int s;
array_linked_list(int n){
};
for(int i=1;i<=n;i++){
cout<<"----------------------------------------------------------------------"<<endl;
cout<<endl;
temp = temp->ptr;
cout<<"---------------------------------------------------------------------\n\n"<<endl;
//cout<<"---------------edge list----------------------------"<<endl;
for(int i=0;i<edges.size();i++){
// cout<<edges[i].first<<" , "<<edges[i].second<<endl;
//cout<<"-----------------------------------------------------"<<endl;
// find all vertex which has y =4 , and than find adjacent edges
// and store some flags that will be used to know that those edges, has already been used
vector <int> vertices;
if(node.point.second == y){
vertices.push_back(int(i));
int flag = 0;
// ged->arr[1] is a pointer
int i = y;
for(int j=0;j<vertices.size();j++){ // this access all vertex with y = 4, or i=4 , or some other
vector<int> temp;
for(int k= 0; k < adjList[vertices[j]].size(); k++){ // this in 1 loop gives all adjacent to 1 vertex
}
// now for each edge we will make a node
int x1 = adjList[vertices[j]][0].point.first;
int y1 = adjList[vertices[j]][0].point.second;
// v1 is vertices[j]
// v2 inside ,, if ( v1,v2) is found in any pair in edge vectore than we will skip it
for(int p=0;p<temp.size();p++){
// this is v2
int x2 = adjList[temp[p]][0].point.first;
int y2 = adjList[temp[p]][0].point.second;
flag = -1;
break;
flag = -1;
break;
}
if(flag == -1){
flag =0;
continue;
int minX = (y1 < y2) ? x1 : y2; // for x which ever has minmum y
ged->arr[i].ptr->x = minX;
ged->arr[i].ptr->y = maxY;
ged->arr[i].ptr->vertex1 = vertices[j];
ged->arr[i].ptr->vertex2 = temp[p];
edges.push_back(make_pair(vertices[j], temp[p]));
}else{
tempp = tempp->ptr;
tempp->ptr->y = maxY;
tempp->ptr->vertex1 = vertices[j];
tempp->ptr->vertex2 = temp[p];
//cout<<"============================================================\n";
// cout<<temp->vertex1<<" , "<<temp->vertex2<<endl;
active_edges.push_back(temp);
temp = temp->ptr;
//cout<<"==============================================================\n";
// remove it
// cout<<"remove it : "<<endl;
int indexToRemove = i;
active_edges.erase(active_edges.begin() + indexToRemove);
} else {
for(int i=0;i<active_edges.size();i++){
cout<<"------------------------------------------------------------------------------------------------------\n"<<endl;
int x1 = 0;
int y1 = y;
int x2 = 0;
int y2 = y;
for(int i =0 ;i<active_edges.size(); i +=2){
x1 = active_edges[i]->x;
x2 = active_edges[i+1]->x;
dda(x1,y1,x2,y2,GREEN);
if(!isinf(active_edges[i]->m_inv)){
int y = y_min;
int get = 5;
int aet = 0;
while(y <= y_max){ // repeat until aet and get is empty
move_edges(y,adjList, active_edges,ged);
remove_from_active_edge(active_edges,y);
// print_global_edge_table(ged,y_max);
// print_active_edge_table(active_edges);
make_pair_and_print(active_edges,y);
y +=1;
if(y< y_max){
increment_x(active_edges);
}else{
break;
}
}
int main() {
int numVertices = 6;
points.push_back(make_pair(200,100));
points.push_back(make_pair(50,300));
points.push_back(make_pair(80,400));
points.push_back(make_pair(200,350));
points.push_back(make_pair(300,400));
points.push_back(make_pair(350,310));
scan_line(adjList,numVertices,i,ged,edges);
//print_global_edge_table(ged,y_max);
scan_algo(ged,y_min,y_max,adjList);
cout<<"done"<<endl;
getch();
closegraph();
return 0;
PRACTICAL 6:
#define _USE_MATH_DEFINES
#include <cmath>
#include <cstdlib>
#include <graphics.h>
#include <iostream>
void clrscr()
#ifdef _WIN32
system("cls");
#elif __unix__
system("clear");
#endif
double **inputFigure(int n)
cout << "Enter the matrix for the 2-D shape (homogeneous):\n";
return figure;
setcolor(WHITE);
line(COORD_SHIFT + points[i][0],
COORD_SHIFT + points[i][1],
COORD_SHIFT + points[(i + 1) % n][0],
delay(5e3);
cleardevice();
return _figure;
{0, 0, 1}};
return _figure;
return _figure;
switch (c)
case 1:
T[1][1] = -1;
break;
case 2:
T[0][0] = -1;
break;
case 3:
T[0][0] = 0;
T[0][1] = 1;
T[1][0] = 1;
T[1][1] = 0;
break;
case 4:
T[0][0] = -1;
T[1][1] = -1;
break;
default:
return NULL;
break;
return _figure;
return _figure;
int ch = 0;
double **_figure;
do
clrscr();
switch (ch)
case 1:
int m, n;
cin >> m;
cin >> n;
drawFigure(figure, dim);
drawFigure(_figure, dim);
break;
case 2:
double theta;
drawFigure(figure, dim);
break;
case 3:
cin >> m;
cin >> n;
drawFigure(figure, dim);
drawFigure(_figure, dim);
break;
case 4:
cin >> m;
drawFigure(figure, dim);
drawFigure(_figure, dim);
break;
case 5:
cin >> n;
drawFigure(figure, dim);
drawFigure(_figure, dim);
break;
case 6:
drawFigure(figure, dim);
break;
case 7:
default:
break;
delete _figure;
<< "Finished..."
<< endl;
if (ch != 7)
cin.ignore();
cin.get();
};
int main(void)
int n;
double **fig;
cin >> n;
fig = inputFigure(n);
menu(fig, n);
delete fig;
closegraph();
return 0;
OUTPUT:
PRACTICAL 7:
#include <iostream>
#include <direct.h>
#include <stdio.h>
#include <math.h>
#include <conio.h>
#include <graphics.h>
#include <process.h>
double y1;
int i;
clearviewport();
getch();
closegraph();
double a, b, c;
int i;
clearviewport();
edge[i][0] = edge[i][0] * a;
edge[i][1] = edge[i][1] * b;
edge[i][2] = edge[i][2] * c;
draw_cube(edge);
closegraph();
int a, b, c;
int i;
clearviewport();
for (i = 0; i < 20; i++)
edge[i][0] += a;
edge[i][0] += b;
edge[i][0] += c;
draw_cube(edge);
closegraph();
int ch;
int i;
switch (ch)
case 1:
edge[i][0] = edge[i][0];
temp = edge[i][1];
temp1 = edge[i][2];
draw_cube(edge);
break;
case 2:
edge[i][1] = edge[i][1];
temp = edge[i][0];
temp1 = edge[i][2];
draw_cube(edge);
break;
case 3:
edge[i][2] = edge[i][2];
temp = edge[i][0];
temp1 = edge[i][1];
edge[i][0] = temp * cos(theta) - temp1 * sin(theta);
draw_cube(edge);
break;
int ch;
int i;
switch (ch)
case 1:
edge[i][0] = edge[i][0];
edge[i][1] = -edge[i][1];
edge[i][2] = -edge[i][2];
draw_cube(edge);
break;
case 2:
for (i = 0; i < 20; i++)
edge[i][1] = edge[i][1];
edge[i][0] = -edge[i][0];
edge[i][2] = -edge[i][2];
draw_cube(edge);
break;
case 3:
edge[i][2] = edge[i][2];
edge[i][0] = -edge[i][0];
edge[i][1] = -edge[i][1];
draw_cube(edge);
break;
int ch;
int i;
double p, q, r;
switch (ch)
case 1:
cin >> p;
draw_cube(edge);
break;
case 2:
cin >> q;
draw_cube(edge);
break;
case 3:
cin >> r;
draw_cube(edge);
break;
closegraph();
int main()
int choice;
double edge[20][3] = {
100, 0, 0,
100, 100, 0,
0, 100, 0,
0, 100, 100,
0, 0, 100,
0, 0, 0,
100, 0, 0,
100, 0, 100,
100, 100, 0,
0, 100, 100,
0, 100, 0,
0, 0, 0,
0, 0, 100,
100, 0, 100};
while (1)
switch (choice)
case 1:
draw_cube(edge);
break;
case 2:
scale(edge);
break;
case 3:
rotate(edge);
break;
case 4:
reflect(edge);
break;
case 5:
translate(edge);
break;
case 6:
perspect(edge);
break;
case 7:
exit(0);
default:
getch();
break;
closegraph();
return 0;
OUTPUT:
PRACTICAL 7:
Write a program to draw Hermite/Bezier curve.
BEZIER CURVE:
#include<graphics.h>
#include<math.h>
#include<conio.h>
#include<stdio.h>
int main()
{
int x[4],y[4],i;
double put_x,put_y,t;
int gr=DETECT,gm;
initgraph(&gr,&gm,NULL);
printf("\n****** Bezier Curve ***********");
printf("\n Please enter x and y coordinates ");
for(i=0;i<4;i++)
{
scanf("%d%d",&x[i],&y[i]);
putpixel(x[i],y[i],3); // Control Points
}
return 0;
}
OUTPUT:
HERMITE CURVE:
#include <iostream>
#include <graphics.h>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
struct point
int x, y;
};
float x, y, t;
putpixel(x, y, YELLOW);
int main()
errorcode = graphresult();
/* an error occurred */
if (errorcode != grOk)
getch();
exit(1);
getch();
closegraph();
return 0;
OUTPUT: