Professional Documents
Culture Documents
Code:: 1. Draw A Straight Segment Connecting The Points (3,1) and (8, 7) Using Bresenham's Line Drawing Algorithm
Code:: 1. Draw A Straight Segment Connecting The Points (3,1) and (8, 7) Using Bresenham's Line Drawing Algorithm
Code:: 1. Draw A Straight Segment Connecting The Points (3,1) and (8, 7) Using Bresenham's Line Drawing Algorithm
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
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;
i=0;
while (x<=y)
X(i+1)=x;
Y(i+1)=y;
i=i+1;
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
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)];
a = a * r;
b = b * r;
c = c * r;
d = d * r;
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 ];
a = a * ref_y;
b = b * ref_y;
c = c * ref_y;
d = d * ref_y;
a = a * ref_x;
b = b * ref_x;
c = c * ref_x;
d = d * ref_x;
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 ];
a = a * refOfYthenX;
b = b * refOfYthenX;
c = c * refOfYthenX;
d = d * refOfYthenX;