CG Lab File
CG Lab File
1
INDEX
2
EXPERIMENT-1
AIM:
WAP to draw a line using DDA Line Algorithm.
THEORY:
In any 2D plane if we connect two points (x0, y0) and (x1, y1), we get a line segment. But in the case of
computer graphics we cannot directly join any two coordinate points, for that we should calculate intermediate
point’s coordinate and put a pixel for each intermediate point. For using graphics functions, our output screen is
treated as a coordinate system where the coordinate of the top-left corner is (0, 0) and as we move down our y-
ordinate increases and as we move right our x-ordinate increases for any point (x, y).
Now, for generating any line segment we need intermediate points and for calculating them we have can use a
basic algorithm called DDA(Digital differential analyzer) line generating algorithm.
ALGORITHM
1. Calculate difference in x i.e. dx = (x2 – x1), difference in y i.e. dy = (y2 – y1)
2. Now calculate the slope of the line with given points and using dx and dy, i.e. m = (dy/dx)
3. Finding steps i.e. Steps = max(abs(dx), abs(dy))
4. Finding incremental value in ‘x’ and ‘y’. incX = (dx/Steps), incY = (dy/Steps)
5. Now for each step, increment X and Y by their increment value, set the pixel using putpixel
CODE:
#include <graphics.h>
#include <conio.h>
int main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TC\\BGI");
3
OUTPUT:
4
EXPERIMENT-2
AIM:
WAP to draw a line using Bresenham’s Line Algorithm.
THEORY:
Bresenham’s line drawing algorithm deals with the problems that occurred in DDA line drawing algorithm. It
does not takes the approx. values of coordinates, rather solves the problem with new concept in form of
variables known as Decision variable generally represented by ‘P’. With decision variable we solve the case
where it is to be decided that whether we should choose upper or lower coordinate i.e. y+1 or y. Decision
parameter finds the mid-point of two pixels and determines the shortest distance of line from two pixels.
Shortest distant pixel is chosen
ALGORITHM:
1. Input two coordinates – x1, y1 and x2, y2
2. Find the slope of the line i.e. m = (y2-y1)/(x2-x1) and P = 2.dy – dx and steps = max(abs(dy), abs(dx))
3. If slope is less than one i.e. m < 1 and steps != 0
a. If P < 0
i. x = x + 1 and y = y
ii. P = P + 2.dy
b. Else
i. x = x + 1 and y = y + 1
ii. P = P + 2.dy – 2.dx
4. steps = steps – 1
5. Repeat and check again from step 4
6. If slope is greater than 1 i.e. m > 1 and steps != 0
a. If P < 0
i. y = y + 1 and x = x
ii. P = P + 2.dx
b. Else
i. x = x + 1 and y = y + 1
ii. P = P + 2.dx – 2.dy
7. steps = steps - 1
8. Repeat and check from Step 8
9. If slope is equal to 1 i.e. m = 1 and steps != 0
a. x = x + 1 and y = y + 1
10. Repeat and check from step 11
CODE:
#include <graphics.h>
#include <conio.h>
#include <iostream.h>
#include <math.h>
5
dy = abs(Y1 - Y0);
dx = abs(X1 - X0);
if (abs(m) < 1)
{
pk = 2 * dy - dx;
for (int i = 0; i < dx; i++)
{
x++;
if (pk< 0)
pk = pk + 2 * dy;
else
{
y++;
pk = pk + 2 * dy - 2 * dx;
}
putpixel(x, y, WHITE);
}
}
else
{
pk = 2 * dx - dy;
for (int i = 0; i < dy; i++)
{
x++;
if (pk< 0)
pk = pk + 2 * dx;
else
{
y++;
pk = pk + 2 * dx - 2 * dy;
}
putpixel(x, y, WHITE);
}
}
}
int main()
{
clrscr();
intgd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TC\\BGI");
getch();
return 0;
}
6
OUTPUT:
7
EXPERIMENT-3
AIM:
WAP to draw a circle using Bresenham’s Circle Drawing Algorithm.
THEORY:
The Bresenham Circle Drawing Algorithm efficiently plots a circle on raster displays using only integer
arithmetic. It leverages the circle's symmetry, calculating points for one octant (45 degrees) and reflecting them
to plot the full circle. Starting at the topmost point (0,r)(0, r)(0,r), the algorithm uses a decision parameter,
pkp_kpk, to determine the next pixel's position. If pk<0p_k < 0pk<0, the next point is (x+1,y)(x+1, y)(x+1,y);
otherwise, it is (x+1,y−1)(x+1, y-1)(x+1,y−1). The decision parameter is updated iteratively to avoid floating-
point computations. Each calculated point is mirrored across the circle’s symmetric axes, reducing computation.
The algorithm stops when x≥yx \geq yx≥y, as further points would overlap. This method is computationally
efficient due to its reliance on simple operations like addition and subtraction. Widely used in computer
graphics, it is ideal for drawing smooth circles in applications like game development, CAD systems, and other
visual rendering tasks.
ALGORITHM
1. Input the radius “r” of the circle and centre of the circle (x c, yc).
2. Find the decision variable i.e. d = 3–2* r;
3. The first coordinate is x = 0, y = r;
4. While x < y, repeat this step
5. Get temporary coordinatesby shifting by centre –
6. x = x + xc
7. y = y + yc
8. Plot (x,y), (-x,y), (x, -y), (-x, -y), (y, x), (-y, x), (y, -x) and (-y, -x).
9. If (d < 0)
a. d= d + 4*.x + 6
b. Increment x
10. Else (d >= 0)
a. d = d + 4(x-y) + 10
b. Increment x and decrement y
CODE:
#include <graphics.h>
#include <conio.h>
#include <iostream>
using namespace std;
while (x <= y) {
// Plot the 8 symmetrical points
putpixel(X0 + x, Y0 + y, WHITE);
8
putpixel(X0 - x, Y0 + y, WHITE);
putpixel(X0 + x, Y0 - y, WHITE);
putpixel(X0 - x, Y0 - y, WHITE);
putpixel(X0 + y, Y0 + x, WHITE);
putpixel(X0 - y, Y0 + x, WHITE);
putpixel(X0 + y, Y0 - x, WHITE);
putpixel(X0 - y, Y0 - x, WHITE);
if (d < 0) {
d = d + 4 * x + 6;
} else {
d = d + 4 * (x - y) + 10;
y--;
}
x++;
}
}
int main() {
int gd = DETECT, gm;
Output:
9
EXPERIMENT-4
AIM:
WAP for Boundary Fill Algorithm
THEORY:
The boundary fill algorithm works as its name. This algorithm picks a point inside an object and starts to fill
until it hits the boundary of the object. The color of the boundary and the color that we fill should be different
for this algorithm to work.
In this algorithm, we assume that color of the boundary is same for the entire object. The boundary fill
algorithm can be implemented by 4-connected pixels or 8-connected pixels.
ALGORITHM:
1. Create a function named as boundaryfill with 4 parameters (x,y,f_color,b_color).
CODE:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
void fill_right(int x,int y);
void fill_left(int x,int y);
void main()
10
{
int gd=DETECT,gm,x,y,n,i;
clrscr();
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
printf("*** Boundary Fill algorithm ***");
/*- draw object -*/
line (50,50,200,50);
line (200,50,200,300);
line (200,300,50,300);
line (50,300,50,50);
/*- set seed point -*/
x=100; y=100;
fill_right(x,y);
fill_left(x-1,y);
getch();
}
void fill_right(int x,int y)
{
if((getpixel(x,y) != WHITE)&&(getpixel(x,y) != RED))
{
putpixel(x,y,RED);
fill_right(++x,y); x=x-1;
fill_right(x,y-1);
fill_right(x,y+1);
}
delay(1);
}
void fill_left(int x,int y)
{
if((getpixel(x,y) != WHITE)&&(getpixel(x,y) != RED))
{
putpixel(x,y,RED);
fill_left(--x,y); x=x+1;
fill_left(x,y-1);
fill_left(x,y+1);
}
delay(1);
}
OUTPUT:
11
EXPERIMENT-5
AIM:
WAP for Flood Fill Algorithm
THEORY:
Sometimes we come across an object where we want to fill the area and its boundary with different colors. We
can paint such objects with a specified interior color instead of searching for particular boundary color as in
boundary filling algorithm. Instead of relying on the boundary of the object, it relies on the fill color. In other
words, it replaces the interior color of the object with the fill color. When no more pixels of the original interior
color exist, the algorithm is completed.
In Flood Fill algorithm we start with some seed and examine the neighboring pixels, however pixels are
checked for a specified interior color instead of boundary color and is replaced by a new color. It can be done
using 4 connected or 8 connected region method.
ALGORITHM:
Step 1 − Initialize the value of seed point (seedx, seedy), fcolor and dcol.
Step 3 − Check if the current seed point is of default color, then repeat the steps 4 and 5 till the boundary
pixels reached.
Step 4 − Change the default color with the fill color at the seed point.
Step 6 − Exit
There is a problem with this technique. Consider the case as shown below where we tried to fill the entire
region. Here, the image is filled only partially. In such cases, 4-connected pixels technique cannot be used.
CODE:
#include<stdio.h>
12
#include<graphics.h>
#include<dos.h>
voidfloodFill(intx,inty,intoldcolor,intnewcolor)
{
if(getpixel(x,y) == oldcolor)
{
putpixel(x,y,newcolor);
floodFill(x+1,y,oldcolor,newcolor);
floodFill(x,y+1,oldcolor,newcolor);
floodFill(x-1,y,oldcolor,newcolor);
floodFill(x,y-1,oldcolor,newcolor);
}
}
//getpixel(x,y) gives the color of specified pixel
int main()
{
intgm,gd=DETECT,radius;
intx,y;
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
circle(x,y,radius);
floodFill(x,y,0,15);
delay(5000);
closegraph();
return 0;
}
OUTPUT:
13
14
EXPERIMENT-6
AIM:
WAP to draw circle using Midpoint Circle Drawing Algorithm.
THEORY:
Mid-point circle drawing algorithm uses the concept of 8-way symmetry property of the circle. In this algorithm
we find the coordinates of the first octant using the given radius and coordinates and place the other
corresponding symmetric points in other 7 octants. To solve the approximation problems same as Bresenham’s
algorithm, we use the concept of Decision parameter which is based upon the concept that whether the mid-
point of two possible pixels is in circle, on circle or outside the circle.
ALGORITHM
11. Input the radius “r” of the circle and centre of the circle (x c, yc).
12. Find the decision variable i.e. P = 1 – r;
13. The first coordinate is x = 0, y = r;
14. While x < y, repeat this step
15. Get temporary coordinatesby shifting by centre –
16. x = x + xc
17. y = y + yc
18. Plot (x,y), (-x,y), (x, -y), (-x, -y), (y, x), (-y, x), (y, -x) and (-y, -x).
19. If (P < 0)
a. P = P + 2.x + 3
b. Increment x
20. Else (P >= 0)
a. P = P + 2(x-y) + 5
b. Increment x and decrement y
CODE:
#include <graphics.h>
#include <conio.h>
#include <iostream.h>
#include <math.h>
while (x <= y)
{
putpixel(x + X0, y + Y0, WHITE);
putpixel(-x + X0, -y + Y0, WHITE);
putpixel(x + X0, -y + Y0, WHITE);
putpixel(-x + X0, y + Y0, WHITE);
putpixel(y + X0, x + Y0, WHITE);
putpixel(-y + X0, -x + Y0, WHITE);
putpixel(y + X0, -x + Y0, WHITE);
putpixel(-y + X0, x + Y0, WHITE);
15
if (p < 0)
{
x++;
p = p + 2 * x + 1;
}
else
{
x++;
y--;
p = p + 2 * x - 2 * y + 1;
}
}
}
int main()
{
clrscr();
intgd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TC\\BGI");
getch();
return 0;
}
OUTPUT:
16
EXPERIMENT-7
AIM:
THEORY:
A translation process moves every point a constant distance in a specified direction. It can be described as a
rigid motion. A translation can also be interpreted as the addition of a constant vector to every point, or as
shifting the origin of the coordinate system.
Suppose, If point P(X, Y) is to be translated by amount Dx and Dy to a new location P’(X’, Y’) then new
coordinates can be obtained by adding Dx to X and Dy to Y as:
X’ = Dx + X
Y’ = Dy + Y
Or
P’ = T + P
where P’ = [X’,Y’] ; T = [Dx,Dy]; P = [X,Y];
1. Translation of a Triangle
Concept:
A triangle is defined by three vertices, each represented by (x,y)(x, y)(x,y) coordinates. Translating a triangle
involves moving all three vertices by adding a translation vector T=(Tx,Ty)T = (T_x, T_y)T=(Tx,Ty) to their
coordinates.
Translation Formula:
2. Translation of a Circle
Concept:
A circle is defined by its center point (xc,yc)(x_c, y_c)(xc,yc) and radius rrr. Translating a circle involves
moving its center by adding a translation vector T=(Tx,Ty)T = (T_x, T_y)T=(Tx,Ty). The radius of the circle
remains unchanged.
17
Translation Formula:
xc′=xc+Tx,yc′=yc+Tyx_c' = x_c + T_x, \quad y_c' = y_c + T_yxc′=xc+Tx,yc′=yc+Ty
Code:
#include <graphics.h>
#include <conio.h>
#include <stdio.h>
// Translated Triangle
setcolor(3); // Cyan for translated
line(triangle[0][0], triangle[0][1], triangle[1][0], triangle[1][1]);
line(triangle[1][0], triangle[1][1], triangle[2][0], triangle[2][1]);
line(triangle[2][0], triangle[2][1], triangle[0][0], triangle[0][1]);
}
// Translated Circle
setcolor(3); // Cyan for translated
circle(center[0], center[1], radius);
18
}
int main() {
// Initialize graphics mode
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TC\\BGI");
return 0;
}
Output :
19
EXPERIMENT-8
AIM:
Description:
A scaling can be represented by a scaling matrix. To scale an object by a vector v = (vx, vy, vz), each point p =
(px, py, pz) would need to be multiplied with this scaling matrix:
Such a scaling changes the diameter of an object by a factor between the scale factors, the area by a factor
between the smallest and the largest product of two scale factors, and the volume by the product of all three.
The scaling is uniform if and only if the scaling factors are equal (vx = vy = vz). If all except one of the scale
factors are equal to 1, we have directional scaling.
In the case where vx = vy = vz = k, the scaling is also called an enlargement or dilation by a factor k, increasing
the area by a factor of k2 and the volume by a factor of k3.
A scaling in the most general sense is any affine transformation with a diagonalizable matrix. It includes the
case that the three directions of scaling are not perpendicular. It includes also the case that one or more scale
factors are equal to zero (projection), and the case of one or more negative scale factors. The latter corresponds
to a combination of scaling proper and a kind of reflection. Along lines in a particular direction we take the
reflection in the point of intersection with a plane that need not be perpendicular; therefore it is more general
than ordinary reflection in the plane.
If we provide values less than 1 to the scaling factor S, then we can reduce the size of the object. If we provide
values greater than 1, then we can increase the size of the object.
1. Scaling of a Triangle
Concept:
A triangle is defined by its three vertices, each represented by coordinates (x,y)(x, y)(x,y). Scaling a triangle
involves changing the size of the triangle by multiplying the coordinates of its vertices by scaling factors
SxS_xSx (for the x-axis) and SyS_ySy (for the y-axis).
2. Scaling of a Circle
Concept:
A circle is defined by its center (xc,yc)(x_c, y_c)(xc,yc) and radius rrr. Scaling a circle involves enlarging or
reducing its size by multiplying the radius by a scaling factor SrS_rSr. The position of the center remains
unchanged.
20
Code:
#include <graphics.h>
#include <conio.h>
#include <stdio.h>
// Scaled Triangle
setcolor(3); // Cyan for scaled
line(triangle[0][0], triangle[0][1], triangle[1][0], triangle[1][1]);
line(triangle[1][0], triangle[1][1], triangle[2][0], triangle[2][1]);
line(triangle[2][0], triangle[2][1], triangle[0][0], triangle[0][1]);
}
// Scaled Circle
setcolor(3); // Cyan for scaled
circle(center[0], center[1], radius);
}
int main() {
// Initialize graphics mode
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TC\\BGI");
21
{200, 100} // Vertex C
};
// Circle radius
int radius = 50;
// Scaling factors
float Sx = 1.5, Sy = 1.5; // Scaling for triangle
float Sc = 1.8; // Scaling for circle
return 0;
}
Output:
22
EXPERIMENT-9
AIM:
WAP to implement Cohen-Sutherland Line Clipping Algorithm
Description:
This algorithm uses the clipping window as shown in the following figure. The minimum coordinate for the
clipping region is (XWmin, YWmin) and the maximum coordinate for the clipping region is (XWmax,
YWmax).
We will use 4-bits to divide the entire region. These 4 bits represent the Top, Bottom, Right, and Left of the
region as shown in the following figure. Here, the TOP and LEFT bit is set to 1 because it is the TOP-
LEFT corner.
There are 3 possibilities for the line −
● Line can be completely inside the window (This line should be accepted).
● Line can be completely outside of the window (This line will be completely removed from the region).
● Line can be partially inside the window (We will find intersection point and draw only that portion of
line that is inside region).
Algorithm:
Code:
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<graphics.h>
#include<dos.h>
23
typedef struct coordinate
{
int x,y;
char code[4];
}PT;
void drawwindow();
void drawline(PT p1,PT p2);
PT setcode(PT p);
int visibility(PT p1,PT p2);
PT resetendpt(PT p1,PT p2);
void main()
{
int gd=DETECT,v,gm;
PT p1,p2,p3,p4,ptemp;
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
drawwindow();
delay(500);
drawline(p1,p2);
delay(500);
cleardevice();
delay(500);
p1=setcode(p1);
p2=setcode(p2);
v=visibility(p1,p2);
delay(500);
switch(v)
{
case 0: drawwindow();
delay(500);
drawline(p1,p2);
break;
case 1: drawwindow();
delay(500);
break;
case 2: p3=resetendpt(p1,p2);
p4=resetendpt(p2,p1);
drawwindow();
delay(500);
drawline(p3,p4);
break;
24
}
delay(5000);
closegraph();
}
void drawwindow()
{
line(150,100,450,100);
line(450,100,450,350);
line(450,350,150,350);
line(150,350,150,100);
}
if(p.y<100)
ptemp.code[0]='1'; //Top
else
ptemp.code[0]='0';
if(p.y>350)
ptemp.code[1]='1'; //Bottom
else
ptemp.code[1]='0';
if(p.x>450)
ptemp.code[2]='1'; //Right
else
ptemp.code[2]='0';
if(p.x<150)
ptemp.code[3]='1'; //Left
else
ptemp.code[3]='0';
ptemp.x=p.x;
ptemp.y=p.y;
return(ptemp);
}
25
for(i=0;i<4;i++)
{
if((p1.code[i]!='0') || (p2.code[i]!='0'))
flag=1;
}
if(flag==0)
return(0);
for(i=0;i<4;i++)
{
if((p1.code[i]==p2.code[i]) && (p1.code[i]=='1'))
flag='0';
}
if(flag==0)
return(1);
return(2);
}
if(p1.code[3]=='1')
x=150;
if(p1.code[2]=='1')
x=450;
if((p1.code[3]=='1') || (p1.code[2]=='1'))
{
m=(float)(p2.y-p1.y)/(p2.x-p1.x);
k=(p1.y+(m*(x-p1.x)));
temp.y=k;
temp.x=x;
for(i=0;i<4;i++)
temp.code[i]=p1.code[i];
if(p1.code[0]=='1')
y=100;
if(p1.code[1]=='1')
26
y=350;
if((p1.code[0]=='1') || (p1.code[1]=='1'))
{
m=(float)(p2.y-p1.y)/(p2.x-p1.x);
k=(float)p1.x+(float)(y-p1.y)/m;
temp.x=k;
temp.y=y;
for(i=0;i<4;i++)
temp.code[i]=p1.code[i];
return(temp);
}
else
return(p1);
}
Result:
Before clipping:
After clipping:
27
EXPERIMENT-10
AIM:
WAP to implement the Mid-Point Ellipse Drawing Algorithm.
THEORY:
Midpoint ellipse algorithm plots(finds) points of an ellipse on the first quadrant by dividing the quadrant into
two regions. Each point(x, y) is then projected into other three quadrants (-x, y), (x, -y), (-x, -y) i.e. it uses 4-
way symmetry.
ALGORITHM:
1. Take input radius along x axis and y axis and obtain center of ellipse.
Initially, we assume ellipse to be centered at origin and the first point as : (x, y0)= (0, ry).
2. Obtain the initial decision parameter for region 1 as: p10=ry2+1/4rx2-rx 2ry
3. For every xk position in region 1 :
a. If p1k<0
i. then the next point along the is (xk+1 , yk)
ii. AND p1k+1=p1k+2ry2xk+1+ry2
b. Else
i. the next point is (xk+1, yk-1)
ii. AND p1k+1=p1k+2ry2xk+1– 2rx2yk+1+ry2
4. Obtain the initial value in region 2 using the last point (x0, y0) of region 1 as:
p20=ry2(x0+1/2)2+rx2(y0-1)2-rx2ry2
5. At each yk in region 2 starting at k =0 perform the following task.
a. If p2k<0
i. the next point is (xk, yk+1)
ii. and p2k+1=p2k-2rx2yk+1+rx2
b. Else
i. the next point is (xk+1, yk -1)
ii. and p2k+1=p2k+2ry2xk+1-2rx2yk+1+rx2
6. Now obtain the symmetric points in the three quadrants and plot the coordinate value as: x=x+xc,
y=y+yc
7. Repeat the steps for region 1 until 2ry2xgt=2rx2y
CODE:
#include <graphics.h>
#include <conio.h>
#include <iostream.h>
#include <math.h>
using namespace std;
28
dy = 2 * rx * rx * y;
while(y >= 0)
{
cout << x + xc <<" , "<< y + yc << endl;
cout << -x + xc <<" , "<< y + yc << endl;
cout << x + xc <<" , "<< -y + yc << endl;
cout << -x + xc <<" , "<< -y + yc << endl;
if(d2 > 0)
{
y--;
dy = dy - (2 * rx * rx);
d2 = d2 + (rx * rx) - dy;
}
else
{
y--;
x++;
dx = dx + (2 * ry * ry);
dy = dy - (2 * rx * rx);
d2 = d2 + dx - dy + (rx * rx);
}
}
}
int main()
{
midEl(10, 15, 50, 50);
return0;
}
29
OUTPUT:
30