Code:: 1. Draw A Straight Segment Connecting The Points (3,1) and (8, 7) Using Bresenham's Line Drawing Algorithm

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

1.

Draw a straight segment connecting the points (3,1) and (8, 7) using 
Bresenham's line drawing algorithm . 
 
Code: 
 
%function bresenham_line()
clc
clear all
% point = input('Give coord[ x0 y0 x1 y1]: ');
point=[3 1 8 7];
if (abs(point(4)-point(2)) > abs(point(3)-point(1))) % If the line is steep

x0 = point(2);
y0 = point(1);
x1 = point(4);
y1 = point(3);% then it would be converted to
token = 1; % non steep by changing coordinate
else
x0 = point(1);
y0 = point(2);
x1 = point(3);
y1 = point(4);
token = 0;
end
if(x0 >x1)
temp1 = x0; x0 = x1; x1 = temp1;
temp2 = y0; y0 = y1; y1 = temp2;
end
dx = abs(x1 - x0) ; % Distance to travel in x-direction
dy = abs(y1 - y0); % Distance to travel in y-direction
sx = sign(x1 - x0); % sx indicates direction of travel in X-dir
sy = sign(y1 - y0); % Ensures positive slope linfe
clf, subplot(2,1,1) ,hold on , grid on;
if (token == 0)
axis([x0-1 x1+1 y0-1 y1+1]);
else
axis([y0-1 y1+1 x0-1 x1+1]);
end
title('Bresenham Line Generation Algorithm: Point form')
x = x0; y = y0; % Initialization of line
param = 2*dy - dx ; % Initialization of error parameter
for i = 0:dx-1 % FOR loop to travel along X
x_coord(i+1) = x; % Saving in matrix form for plot
y_coord(i+1) = y;
if (token ==0) % Plotting in point form
plot(x,y,'r*'); % For steep line coordinate is again
else % converted for actual line drawing.
plot(y,x,'b*');
end

if param >= 0 % if parameter value is exceeded


y = y +1*sy; % then y coordinate is increased
param = param + 2*(dy)- 2*(dx ); % and parameter value is decreased
else
param = param + 2*dy;
end
x = x + 1*sx; % X-coordinate is increased for next point

end
subplot(2,1,2); hold on; grid on; % Plotting in line fragment form

if (token == 0)
axis([x0-1 x1+1 y0-1 y1+1]);
else
axis([y0-1 y1+1 x0-1 x1+1]);
end

if (token ==0)
plot(x_coord,y_coord,'r-','LineWidth',2);
else
plot(y_coord,x_coord,'r-','LineWidth',2);
end
title('Bresenham Line Generation Algorithm: Line fragment form')
2. Use midpoint circle drawing algorithm to draw a complete circle 
i) with origin at (0,0) and radius 10 units. 
ii) with origin at (5,6) and radius 10 units. 

Code: 

point = [0 0];
%point = [5 6];
r=10
xc=point(1);
yc=point(2);

p=1.25-r;
x=0;
y=10;

subplot(1,1,1),hold on,grid on;


title('Midpoint Circle Algorithm With Origin (0, 0)');
%title('Midpoint Circle Algorithm With Origin (5, 6)');

i=0;
while (x<=y)
X(i+1)=x;
Y(i+1)=y;
i=i+1;

% Ploting co-ordinate points


plot(x+xc, y+yc, 'b*'),hold on,grid on;
plot(y+xc, x+yc, 'b*'),hold on,grid on;
plot(-x+xc, y+yc, 'b*'),hold on,grid on;
plot(-y+xc, x+yc, 'b*'),hold on,grid on;
plot(-x+xc, -y+yc, 'b*'),hold on,grid on;
plot(-y+xc, -x+yc, 'b*'),hold on,grid on;
plot(x+xc, -y+yc, 'b*'),hold on,grid on;
plot(y+xc, -x+yc, 'b*'),hold on,grid on;

if p<0
x=x+1;
else
x=x+1;
y=y-1;
end
if p<0
p = p+2*x+3;
else
p = p+2*x+5-2*y;
end
X1=x;Y1=y;
end

% Ploting Line through the points


plot( X+xc, Y+yc, 'r-', 'LineWidth', 2), hold on, grid on;
plot(Y+xc, X+yc, 'r-', 'LineWidth', 2), hold on ,grid on;
plot(-X+xc, Y+yc, 'r-', 'LineWidth', 2), hold on, grid on;
plot(-Y+xc, X+yc, 'r-', 'LineWidth', 2), hold on, grid on;
plot(-X+xc, -Y+yc, 'r-', 'LineWidth', 2), hold on, grid on;
plot(-Y+xc, -X+yc, 'r-', 'LineWidth', 2), hold on, grid on;
plot(X+xc, -Y+yc, 'r-', 'LineWidth', 2), hold on, grid on;
plot(Y+xc, -X+yc, 'r-', 'LineWidth', 2), hold on, grid on;

axis([-15,15,-16,16]);

saveas(gcf,'MID1.png')
%saveas(gcf,'MID2.png')
3. Draw a square S with end points (2,2), (4,2) (4,4) and (2,4). Any 
translation the square the square by (5 units, 3 units) in x and y direction. 

Code: 
clf,hold on;
title('Original Square & Square after Translation');
tx=5;ty=3;
x1=2;
x2=4;
y1=2;
y2=4;
x = [x1, x2, x2, x1, x1];
y = [y1, y1, y2, y2, y1];
plot(x, y, 'b-', 'LineWidth', 3);
grid on;
axis([x1-tx-1 x2+tx+1 y1-ty-1 y2+ty+1]);

%Translation
hold on;
axis([x1-tx-1 x2+tx+1 y1-ty-1 y2+ty+1]);
x1=x1+tx;
x2=x2+tx;
y1=y1+ty;
y2=y2+ty;
X=[x1, x2, x2, x1, x1];
Y=[y1, y1, y2, y2, y1];
plot(X, Y, 'r-', 'LineWidth', 3);
grid on
legend('before translation','after translation','Location','northwest')

saveas(gcf,'translation.png')

 
4. Scale up the square S with respect to (5,5) by a scaling factor 2. 
 
Code: 
 
clf,hold on;
title('Original Square & Square after Scaling');
x1=2;
x2=4;
y1=2;
y2=4;
Sf=2;xf=5;yf=5;
x = [x1, x2, x2, x1, x1];
y = [y1, y1, y2, y2, y1];
plot(x, y, 'b-', 'LineWidth', 3);
%hold on;
grid on;
%axis([x1-1 x2+1 y1-1 y2+1]);

% Scaling
%subplot(2,1,2),hold on;
x1=xf+(x1-xf)*Sf;
x2=xf+(x2-xf)*Sf;
y1=yf+(y1-yf)*Sf;
y2=yf+(y2-yf)*Sf;
X=[x1, x2, x2, x1, x1];
Y=[y1, y1, y2, y2, y1];
%title('Square after Scaling')
plot(X, Y, 'r-', 'LineWidth', 3);
grid on
axis([x1-1 x2+2 y1-1 y2+2]);
legend('before scaling','after scaling','Location','northwest')

saveas(gcf,'scaling.png')
5. Rotate square s w.r.t origin at angle 90 degree anticlockwise 
 
Code: 
Clc
clear all
a = [ 2 , 2 ];
b = [ 2 , 4 ];
c = [ 4 , 4 ];
d = [ 4 , 2 ];

theta = 90;
r = [cosd(theta) sind(theta); -sind(theta) cosd(theta)];

clf, hold on;


title('Original Square Before & After Rotation');
X=[a(1), b(1), c(1), d(1), a(1)];
Y=[a(2), b(2), c(2), d(2), a(2)];
plot(X, Y, 'b-', 'LineWidth', 3);

a = a * r;
b = b * r;
c = c * r;
d = d * r;

X=[a(1), b(1), c(1), d(1), a(1)];


Y=[a(2), b(2), c(2), d(2), a(2)];
plot(X, Y, 'r-', 'LineWidth', 3);

x = [-5 5];
y = [0, 0];
plot(x,y,'k', 'LineWidth', 1)
plot(y, x,'k', 'LineWidth', 1)
grid on;
set (gca , 'YTick', [-5 -4 -3 -2 -1 0 1 2 3 4 5])
axis([-5 5 -5 5]);
legend('before rotation','after rotation','Location','southwest')
6. a) Reflect square S first about Y axis then about X axis. Represent. 
 
Code: 
clc
clear all
a = [ 2 , 2 ];
b = [ 2 , 4 ];
c = [ 4 , 4 ];
d = [ 4 , 2 ];

ref_y = [-1 0 ; 0 1];


ref_x = [1 0 ; 0 -1];

clf, hold on;


title('Original Square & Reflection about Y axis & Reflection about X axis');
X=[a(1), b(1), c(1), d(1), a(1)];
Y=[a(2), b(2), c(2), d(2), a(2)];
plot(X, Y, 'g-', 'LineWidth', 3);

a = a * ref_y;
b = b * ref_y;
c = c * ref_y;
d = d * ref_y;

X=[a(1), b(1), c(1), d(1), a(1)];


Y=[a(2), b(2), c(2), d(2), a(2)];
plot(X, Y, 'r-', 'LineWidth', 3);

a = a * ref_x;
b = b * ref_x;
c = c * ref_x;
d = d * ref_x;

X=[a(1), b(1), c(1), d(1), a(1)];


Y=[a(2), b(2), c(2), d(2), a(2)];
plot(X, Y, 'b-', 'LineWidth', 3);
x = [-5 5];
y = [0, 0];
plot(x,y,'k', 'LineWidth', 1)
plot(y, x,'k', 'LineWidth', 1)
grid on;
set (gca , 'YTick', [-5 -4 -3 -2 -1 0 1 2 3 4 5])
axis([-5 5 -5 5]);
legend('before reflection','Reflection about Y axis','Reflection about x
axis','Location','southeast')

 
 
6. b) Represent the above transform (reflect twice) with an equivalent 
rotation. 

Code:
Clc
clear all
a = [ 2 , 2 ];
b = [ 2 , 4 ];
c = [ 4 , 4 ];
d = [ 4 , 2 ];

refOfYthenX = [-1 0 ; 0 -1];

clf, hold on;


title('Original Square & Equivalent reflection about Y and then X axis');
X=[a(1), b(1), c(1), d(1), a(1)];
Y=[a(2), b(2), c(2), d(2), a(2)];
plot(X, Y, 'r-', 'LineWidth', 3);

a = a * refOfYthenX;
b = b * refOfYthenX;
c = c * refOfYthenX;
d = d * refOfYthenX;

X=[a(1), b(1), c(1), d(1), a(1)];


Y=[a(2), b(2), c(2), d(2), a(2)];
plot(X, Y, 'b-', 'LineWidth', 3);
x = [-5 5];
y = [0, 0];
plot(x,y,'k', 'LineWidth', 1)
plot(y, x,'k', 'LineWidth', 1)
grid on;
set (gca , 'YTick', [-5 -4 -3 -2 -1 0 1 2 3 4 5])
axis([-5 5 -5 5]);
legend('before reflection','after reflection','Location','northwest')

You might also like