Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
LAB FILE
Aim:
Theory:
Line:
• DDA
• Bresenham’s
• Mid-
Point
Where,
We refer the one point of line as X0, Y0 and the second point of
line as X1, Y1. The lines appear continuous but they are actually
made of pixels. The lines are drawn by a process known as
rasterization.
dy/dx=m=(y2-y2)/(x2-x1)
Xi+1=Xi+x
Yi+1=Yi+delta_y*(y2-y1)/(x2-x1)
Step 1 – Get the input of two end points (X0, Y0) and
(X1, Y1).
dx=X1-
X0
If dx>dy:
If dx>dy:
Steps=absolute(dx)
Else:
Steps=absolute(dy)
Y_inc=dy/(float)steps
#include
<graphics.h>
#include
<iostream>
#include
<math.h>
#include <dos.h>
using namespace
std; int main( )
float
x,y,x1,y1,x2,y2,dx,dy,steps;
int m;
initwindow(800,800);
dx=abs(x2-x1);
dy=abs(y2-y1);
steps=
max(dx,dy);
dx=dx/steps;
dy=dy/steps;
x=x1; y=y1;
i=m;
Vaibhav verma 2k17/se/123
while(i<=steps)
{
Output:
Conclusion :
Aim:
Theory:
Line:
• DDA
• Bresenham’s
• Mid-
Point
Where,
We refer the one point of line as X0, Y0 and the second point of
line as X1, Y1. The lines appear continuous but they are actually
made of pixels. The lines are drawn by a process known as
rasterization.
Bresenham Algorithm:
y= (y1-y0)*(x-x0)/(x1-x0) + y0
The slope △Y/△X depends on the endpoint coordinates only & can
be precomputed , & the ideal Y for successive integer values of X
can be computed starting from & repeatedly adding the slope.
If error becomes greater than 0.5 we know that the line has moved
upwards one pixel, and that we must increment our y coordinate
and readjust the error to represent the distance from the top of the
new pixel - which is done by subtracting one from error.
Pseudocode :
plotline(x0,y0,x1,y1)
dx=x1-x0
dy=y1-y0
D=2*dy-dx
y=y0;
For x from x0 to x1
plot(x,y)
if(D>0)
y=y+1 D=D-
2*dx
FLOWCHART :
int main()
initwindow(800,800
); int dx, dy, p, x,
y; dx=x1-x0;
dy=y1-y0;
x=x0;
y=y0;
p=2*dy-
dx;
while(x<x1)
{
if(p>=0)
{
}
else
{
putpixel(x,y,7);
p=p+2*dy;
getch
(); return
0;
}
OUTPUT :
Conclusion
Theory:
45-degree axis. For example, if we calculate one point seven more points
could be found by reflection.
P1 = (x,y)
P2 = (y.x)
P3 = (-y,x)
P4 = (-x, y)
P5 = (-x, -y)
P6 = (-y,x)
P7 = (y,-x)
P8 = (x,-y)
We need to select, which of these pixels is closer to the true circle and is
this algorithm we find the midpoint between the two pixels. This mid-point,
M is given as follows -
M(xi+1 , (yi+yi-1)/2)
Midpoint is Plot
Where U and L are the Upper and Lower pixel respectively To apply the
midpoint method, we define a circle function –
Any point (xy) on the boundary of the circle with radius r satisfies the
equation F(x )=0
If d = 0, point (x y) lies on the circle If d > 0, point (x,y) lies outside circle
If d < 0, point (x,y) lies inside circle
Algorithm :
Step4: Calculate d = 3 - 2r
Step9: Go to step 6
Step10: Stop
Flowchart:
#include
<graphics.h>
#include
<iostream>
#include
<math.h> using
namespace std;
int main()
cout<<”Enter radius of
circle :”; cin>>r;
initwindow(600,
600); float x=0,
y=r;
float
p=1-y;
while(x<
=y)
{
Vaibhav verma 2k17/se/123
putpixel(xc+x, yc+y, 5);
putpixel(xc-y,
yc+x, 5);
delay(10);
if(p>=0)
p += 2*x – 2*y + 5;
y--;
e
l
s
p += 2*x+3;
e
}
x
+
+
;
}
Output:
Aim:
Theory
Let us consider one quarter of an ellipse. The curve is divided into two
regions. In region I, the slope on the curve is greater than –1 while in
region II less than –1.
b2 x2 + a2 y2 – a2 b2 = 0
Set f(x,y) = b2 x2 + a2
y2 – a2 b2 In region I
(dy/dx > –1),
or yk+1 = yk – 1 if SE is selected.
Pk = f(xk+1, yk–½)
= b2 (xk+1)2 + a2 (yk–½)2 – a2 b2
If Pk > 0, select SE :
= b2 (xk+2)2 + a2 (yk–3/2)2 – a2 b2
∆Pk SE = Pk+1 SE – Pk
= b2 (2xk + 3) – 2a2
(yk – 1) Calculate the
changes of ∆ Pk:
If E is selected,
∆Pk+1 E = b2 (2xk + 5)
b^2 )
x0 = 0, y0 = b, P0 = b2 + ¼a2 (1 – 4b)
In region II (dy/dx < –1), all calculations are similar to that in region I
except that y is decremented in each step.
If Pk < 0, select SE :
If S is selected,
= 2a^2 ∆Pk+1 SE
–1, x < a^2 / √(a^2 + b^2 ) and y > b^2 / √(a^2 + b^2 ), therefore
∆Pk SE < b^2 (2a^2 /√(a^2 + b^2 ) – 2a^2 (b^2 /√(a^2 + b^2 ) – 1) = 2a^2
+ 3b^2
x0 = a^2 / √(a^2 +
b^2 ) y0 = b^2 /
√(a^2 + b^2 )
Algorithm:
1. Take input radius along x axis and y axis and obtain centre of ellipse.
3. Obtain the initial decision parameter for region 1 as: p10=ry2+1/4rx2-rx 2ry
5. 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
8. Now obtain the symmetric points in the three quadrants and plot the
coordinate value as: x=x+xc, y=y+yc
Flowchart:
#include<iostream.h>
#include<graphics.h>
dx=x1-x0;
dy=y1-y0;
x=x0;
y=y0;
p=2*dy-dx;
while(x<x1)
if(p>=0)
putpixel(x,y,7);
y=y+1;
p=p+2*dy-2*dx;
Vaibhav verma 2k17/se/123
}
else
putpixel(x,y,7);
p=p+2*dy;
x=x+1;
int main()
cin>>x0>>y0;
cin>>x1>>y1;
return 0;
Conclusion :
Theory :
Polygon Filling:-
Filling the polygon means highlighting all the pixels which lie inside the
polygon with any colour other than background colour. Polygons are
easier to fill since they have linear boundaries.
In this method, edges of the polygons are drawn. Then starting with
some seed, any point inside the Polygon we examine the
neighbouring pixels to check whether the boundary pixel is reached. If
boundary pixels are not reached, pixels are highlighted and the
process is continued until boundary pixels are reached.
Algorithm :
1. Take the position of the starting point and the boundary color.
2. Decide wether you want to go in 4 directions (N, S, W, E) or 8
directions (N, S, W, E, NW, NE, SW, SE).
3. Choose a fill color.
4. Travel in those directions.
5. If the pixel you land on is not the fill color or the boundary color ,
replace it with the fill color.
6. Repeat 4 and 5 until you’ve been everywhere within the
boundaries.
Flowchart :
#include <graphics.h>
putpixel(x, y, fill_color);
int main()
circle(x, y, rad);
boundaryFillAlgoFor4(x, y, 4, 15);
delay(10000);
getch();
closegraph();
Vaibhav verma 2k17/se/123
return 0;
Output :
Code :
#include <graphics.h>
getpixel(x, y) != fill_color)
putpixel(x, y, fill_color);
//driver code
int main()
delay(10000);
Vaibhav verma 2k17/se/123
getch();
closegraph();
return 0;
Output :
Theory :
Flood fill algorithm helps in visiting each and every point in a given area. It
determines the area connected to a given cell in a multi-dimensional array.
Following are some famous implementations of flood fill algorithm:
Clicking in an area with this tool selected fills that area with the selected
colour.
Solving a Maze:
Given a matrix with some starting point, and some destination with some
obstacles in between, this algorithm helps to find out the path from source
to destination
Minesweeper:
It clearly shows how the cell in the middle is connected to cells around it.
For instance, there are 8-connections like there are in Minesweeper (clicking
on any cell that turns out to be blank reveals 8 cells around it which
contains a number or are blank). The cell (1,1) is connected
to (0,0), (0,1), (0,2), (1,0), (1,2), (2,0), (2,1), (2,2).
Now that the given area has been modelled as a graph, a DFS or BFS can
be applied to traverse that graph.
Algorithm :
#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<iostream.h>
int current;
current=getpixel(x,y);
if(current==old)
putpixel(x,y,fill);
delay(5);
floodFill(x+1,y,old,fill);
floodFill(x-1,y,old,fill);
floodFill(x, y+1,old,fill);
floodFill(x,y-1,old,fill);
void main()
int x,y,o=0;
int gDriver=DETECT,gmode;
initgraph(&gDriver,&gmode, "C:\\TC\\BGI");
rectangle(100,100,150,200);
x=(100+150)/2;
y=(100+200)/2;
floodFill(x,y,2,4);
getch();
closegraph();
Output :
1) 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.
EXPERIMENT – 6(a)
Aim :
Theory :
In Computer graphics,
Let-
Algorithm :
1) Suppose, If point (X, Y) is to be translated by amount Dx and Dy to a new location (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
T = (Dx, Dy ),
P = (X, Y)
2) Here, P(X, Y) is the original point. T(Dx, Dy) is the translation factor, i.e. the amount by which
the point will be translated. P'(X’, Y’) is the coordinates of point P after translation.
Code :
#include<bits/stdc++.h>
setcolor (2);
setcolor(3);
closegraph();
int main()
{
Vaibhav verma 2k17/se/123
int P[2][2] = {5, 8, 12, 18};
2DTranslation(P, T);
return 0;
Output :
Conclusion :
X’ = X + tx
Y’ = Y + ty
Vaibhav verma 2k17/se/123
5) The pair (tx, ty) is called the translation vector or shift vector. The
above equations can also be represented using the column
vectors.
We can write it as − P’ = P + T
EXPERIMENT-6(b)
Aim :
Implementation of 2D Rotation
Theory :
In Computer graphics,
Let-
Algorithm :
We know that,
x = rcosB, y = rsinB
Step 3: Calculate :
#include<stdio.h>
#include<graphics.h>
#include<math.h>
int main()
int gd=0,gm,x1,y1,x2,y2;
double s,c, angle;
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
cleardevice();
setbkcolor(WHITE);
line(x1,y1,x2,y2);
getch();
scanf("%lf", &angle);
setcolor(6);
x1 = floor(x1 * c + y1 * s);
y1 = floor(-x1 * s + y1 * c);
x2 = floor(x2 * c + y2 * s);
y2 = floor(-x2 * s + y2 * c);
cleardevice();
closegraph();
return 0;
}
Conclusion :
Aim :
Implementation of 2D Scaling
Theory :
In computer graphics, scaling is a process of modifying or altering the size of objects.
Let-
• Xnew = Xold x Sx
• Ynew = Yold x Sy
Algorithm :
Sx 0
0 Sy
Code :
#include<graphics.h>
#include<math.h>
int sx,sy,xn1,yn1,xn2,xn3,yn3,yn2,gd,gm;
scanf("%d %d",&sx,&sy);
xn1=x1*sx;
yn1=y1*sy;
xn2=x2*sx;
yn2=y2*sy;
xn3=x3*sx;
yn3=y3*sy;
initgraph(&gd,&gm,"");
line(x1,y1,x2,y2);
line(x1,y1,x3,y3);
line(x2,y2,x3,y3);
delay(600);
line(xn1,yn1,xn3,yn3);
line(xn2,yn2,xn3,yn3);
delay(600);
cleardevice();
int main()
int ch,x1,y1,x2,y2,x3,y3;
scanf("%d %d %d %d %d %d",&x1,&y1,&x2,&y2,&x3,&y3);
scaling(x1,y1,x2,y2,x3,y3);
return 0;
Output:
To implement 2D sheering.
Theory:
X-Shear
The X-Shear preserves the Y coordinate and changes are made to X
coordinates, which causes the vertical lines to tilt right or left as shown in
below figure.
X’ = X
Y-Shear
The Y-Shear preserves the X coordinates and changes the Y coordinates
which causes the horizontal lines to transform into lines which slopes up or
down as shown in the following figure.
X’ = X + Shx . Y
Y’ = Y
Algorithm :
1. Start
4. Shearing
a. X-shear
b. Y-shear
c. Plot (x’,y’)
Code:
#include<iostream.h>
#include<graphics.h>
#include<conio.h>
void main()
{
int x1,y1,x2,y2,s,g,p,q;
float sx,sy;
cin>>x1>>x2;
cout<<“\nenter the y1 and y2 coordinates:-\n”;
cin>>y1>>y2;
cout<<“\nenter the shearing factor along the x-axis:-“;
cin>>sx;
cout<<“\nenter trhe scaling factor to the y-axis:-“;
Vaibhav verma 2k17/se/123
cin>>sy;
int gd=DETECT,gm;
initgraph(&gd,&gm,””);
setbkcolor(WHITE);
setcolor(8);
line(x1,y1,x2,y1);
line(x2,y2,x1,y2) ;
line(x2,y1,x2,y2);
line(x1,y2,x1,y1);
s=x1+int((sx*y1)+0.5);
p=y1+int((sy*x1)+0.5);
g=x2+int((sx*y2)+0.5);
q=y2+int((sy*x2)+0.5);
setcolor(8);
line(x1,y1,x2,p);
line(g,q,s,y2) ;
line(x2,p,g,q);
line(s,y2,x1,y1);
setcolor(8);
outtextxy(100,60,”Shearing on rectangle”);
getch();
Output :
newx=oldx+shearfactor*oldy
newy=oldy+shearfactor*oldx
Theory
Given a set of lines and a rectangular area of interest, the task is to remove
lines which are outside the area of interest and clip the lines which are partially
inside the area.
Algorithm
"inside" region.
find its region's four bit code. Four bit code can
Completely inside the given rectangle: Bitwise OR of region of two end points
of line is 0 (Both points are inside the rectangle)
Completely outside the given rectangle: Both endpoints share at least one
outside region which implies that the line does not cross the visible region.
(bitwise AND of endpoints != 0).
Partially inside the window: Both endpoints are in different regions. In this case,
the algorithm finds one of the two points that is outside the rectangular region.
The intersection of the line from outside point and rectangular window
becomes new corner point and the algorithm repeats
// including libraries
#include <bits/stdc++.h>
#include <graphics.h>
// Global Variables
struct lines {
};
int sign(int x)
if (x > 0)
return 1;
else
return 0;
// Here bits impiles initial Point whereas bite implies end points
setcolor(RED);
// Finding Bits
// initial will used for initial coordinates and end for final
if (bits[i] == 0)
initial += '0';
else
if (bite[i] == 0)
end += '0';
else
end += '1';
// if both points are inside the Accept the line and draw
return;
// this will contain cases where line maybe totally outside for partially inside
else {
if (val == 0)
temp += '0';
else
temp += '1';
// as per algo if AND is not 0000 means line is completely outside hene draw nothing
and retrurn
if (temp != "0000")
return;
// if boths bit are same hence we cannot find any intersection with boundary so
continue
if (bits[i] == bite[i])
continue;
if (i == 0 && bits[i] == 1) {
mylines.y1 = var;
mylines.x1 = xmin;
if (i == 0 && bite[i] == 1) {
mylines.y2 = var;
mylines.x2 = xmin;
if (i == 1 && bits[i] == 1) {
mylines.y1 = var;
mylines.x1 = xmax;
if (i == 1 && bite[i] == 1) {
mylines.y2 = var;
mylines.x2 = xmax;
if (i == 2 && bits[i] == 1) {
mylines.y1 = ymin;
mylines.x1 = var;
if (i == 2 && bite[i] == 1) {
mylines.y2 = ymin;
mylines.x2 = var;
if (i == 3 && bits[i] == 1) {
mylines.y1 = ymax;
mylines.x1 = var;
if (i == 3 && bite[i] == 1) {
mylines.y2 = ymax;
mylines.x2 = var;
initial += '0';
else
initial += '1';
if (bite[i] == 0)
end += '0';
else
end += '1';
// If now both points lie inside or on boundary then simply draw the updated line
return;
else
return;
// Driver Function
int main()
xmin = 40;
xmax = 100;
ymin = 40;
ymax = 80;
mylines[0].x1 = 30;
mylines[0].y1 = 65;
mylines[0].x2 = 55;
mylines[0].y2 = 30;
mylines[1].x1 = 60;
mylines[1].y1 = 20;
mylines[1].x2 = 100;
mylines[2].x1 = 60;
mylines[2].y1 = 100;
mylines[2].x2 = 80;
mylines[2].y2 = 70;
mylines[3].x1 = 85;
mylines[3].y1 = 50;
mylines[3].x2 = 120;
mylines[3].y2 = 75;
line(mylines[i].x1, mylines[i].y1,
mylines[i].x2, mylines[i].y2);
delay(1000);
// Calling clip() which in term clip the line as per window and draw it
clip(mylines[i]);
delay(1000);
delay(4000);
getch();
closegraph();
return 0;
Output
Theory
The inequalities describing the range of the clipping window which is used to
determine the intersections between the line and the clip window.
X = x1 + t(x2-x1)
Y = y1 + t(y2-y1)
tpk <= qk
When the line is parallel to a view window boundary, the p value for that boundary is
zero.
When pk < 0, as t increase line goes from the outside to inside (entering).
When pk > 0, line goes from inside to outside (exiting).
When pk = 0 and qk < 0 then line is trivially invisible because it is outside view window.
When pk = 0 and qk > 0 then the line is inside the corresponding window boundary.
Algorithm
If tmin < tmax, draw a line from (x1 + tmin(x2-x1), y1 + tmin(y2-y1)) to (x1 + tmax(x2-x1),
y1 + tmax(y2-y1))
If the line crosses over the window, (x1 + tmin(x2-x1), y1 + tmin(y2-y1)) and (x1 +
tmax(x2-x1), y1 + tmax(y2-y1)) are the intersection point of line and edge.
#include"graphics.h"
float r ;
if (p < 0.0) {
r = q /p ;
if ( r > *t2 )
retVal = FALSE;
else
if (r > *t1 )
*tl = r;
else
if (p>0.0) {
if ( r<*t1 )
retVal = FALSE;
else i f (r<*t2)
*t2 = r;
else
if (q<0.0)
retVal = FALSE;
return ( retVal ) ;
dy = p2.y - p1.y;
p1.x += t1*dx;
p1.y += t1*dy;
Output
Theory :
Algorithm:
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
#include<stdlib.h>
int main()
int gd,gm,n,*x,i,k=0;
wx1=220,wy1=140,wx2=420,wy2=140,wx3=420,wy3=340,wx4=220,wy4=340
;
int w[]={220,140,420,140,420,340,220,340,220,140
detectgraph(&gd,&gm);
printf("Window:-");
setcolor(RED);
drawpoly(5,w);
scanf("%d",&n);
x = malloc(n*2+1);
k=0;
for(i=0;i<n*2;i+=2)
printf("(x%d,y%d): ",k,k);
scanf("%d,%d",&x[i],&x[i+1]);
k++;
x[n*2]=x[0];
drawpoly method.
x[n*2+1]=x[1];
drawpoly(n+1,x);
getch();
setcolor(RED);
drawpoly(5,w);
setfillstyle(SOLID_FILL,BLACK);
floodfill(2,2,RED);
gotoxy(1,1);
getch();
cleardevice();
closegraph();
return 0;
Output:
1) A convex polygon and a convex clipping area are given. The task is to
clip polygon edges using the Sutherland–Hodgman Algorithm.
4) The new list of vertices generated is passed to the next edge of the clip
polygon in clockwise fashion until all the edges have been used.
Theory :
Algorithm:
1) In this algorithm we take a starting vertex like I1 and traverse the polygon
like I1, V3, I2.
5) Since I3 was not included in first traverse, hence, we start the second
traversal from I3. Therefore, first traversal gives polygon as: I1, V3, I2, I1
and second traversal gives polygon as: I3, V5, I4, I3.
Code:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void clip(float,float,float);
int i,j=0,n;
int rx1,rx2,ry1,ry2;
float x1[8],y1[8];
void main()
{
int gd=DETECT,gm;
int i,n;
float x[8],y[8],m;
clrscr();
initgraph(&gd,&gm,"");
printf("coordinates for rectangle : ");
scanf("%d%d%d%d",&rx1,&ry1,&rx2,&ry2);
printf("no. of sides for polygon : ");
scanf("%d",&n);
printf("coordinates : ");
scanf("%f%f",&x[i],&y[i]);
}
cleardevice();
outtextxy(10,10,"Before clipping");
rectangle(rx1,ry1,rx2,ry2);
for(i=0;i<n-1;i++)
line(x[i],y[i],x[i+1],y[i+1]);
line(x[i],y[i],x[0],y[0]);
getch();
cleardevice();
for(i=0;i<n-1;i++)
{
m=(y[i+1]-y[i])/(x[i+1]-x[i]);
clip(x[i],y[i],m);
clip(x[i+1],y[i+1],m);
}
m=(y[i]-y[0])/(x[i]-x[0]);
clip(x[i],y[i],m);
clip(x[0],y[0],m);
rectangle(rx1,ry1,rx2,ry2);
for(i=0;i<j-1;i++)
line(x1[i],y1[i],x1[i+1],y1[i+1]);
getch();
if(e<rx1)
f+=m*(rx1-e);
e=rx1;
else if(e>rx2)
{
f+=m*(rx2-e);
e=rx2;
}
if(f<ry1)
{
e+=(ry1-f)/m;
f=ry1;
}
else if(f>ry2)
{
e+=(ry2-f)/m;
f=ry2;
}
}
x1[j]=e;
Vaibhav verma 2k17/se/123
y1[j]=f;
j++;
Output:
Conclusion: