You are on page 1of 4

QUESTION1

function pixels = raster(xi,yi,xf,yf)


delx = xf-xi;
dely = yf-yi;

length = max(abs(delx),abs(dely));
x_inc = delx/length;
y_inc = dely/length;

x(1) = xi+0.5;
y(1) = yi+0.5;

for k = 1:length
x(k+1) = x(k) + x_inc;
y(k+1) = y(k) + y_inc;
end
pixels = [floor(x') floor(y')];

clear all;clc;

x1=0; y1=0;
x2=5; y2=3;
x3=8; y3=8;

x=[x1 x2 x3 x1];
y=[y1 y2 y3 y1];

line_1 = raster(x1,y1,x2,y2);
line_2 = raster(x2,y2,x3,y3);
line_3 = raster(x3,y3,x1,y1);

plot(x,y,'-');
hold on

for i = 1:length(line_1(:,1))

fill([line_1(i,1),line_1(i,1),line_1(i,1)+1,line_1(i,1)+1],[line_1(i,2),line_1(i,2)+1,line_1(
i,2)+1,line_1(i,2)],'r','FaceAlpha',0.3);
hold on
end

for i = 1:length(line_2(:,1))

fill([line_2(i,1),line_2(i,1),line_2(i,1)+1,line_2(i,1)+1],[line_2(i,2),line_2(i,2)+1,line_2(
i,2)+1,line_2(i,2)],'g','FaceAlpha',0.3);
hold on
end

for i = 1:length(line_3(:,1))

fill([line_3(i,1),line_3(i,1),line_3(i,1)+1,line_3(i,1)+1],[line_3(i,2),line_3(i,2)+1,line_3(
i,2)+1,line_3(i,2)],'b','FaceAlpha',0.3);
hold on
end
grid on
Published with MATLAB® R2022b
QUESTION2
function pixels = integer_Bresenham(xi,yi,xf,yf)

dely = abs(yf-yi);
delx = abs(xf-xi);

length = max(abs(delx),abs(dely));

x = zeros(length+1,1);
y = zeros(length+1,1);

e = 2*delx-dely;
count = 1;
x(1) = xi;
y(1) = yi;

for y_cur = (yi+1):yf


count = count+1;
if(e>0)
e = e - 2*dely;
x(count) = x(count-1);
else
e = e + 2*delx;
x(count) = x(count-1) - 1;
end
y(count) = y_cur;
end

pixels = [x y];

clc; clear all;


x0 = 0; y0 = 0;
x1 = -5; y1 = 8;

delx = x1-x0;
dely = y1-y0;

m = dely/delx; % Since slope is -1.6 the value of y keeps advancing for


every iteration

x=[x0 x1];
y=[y0 y1];

l = integer_Bresenham(x0,y0,x1,y1);

plot(x,y,'-');
hold on

for i = 1:length(l(:,1))
fill([l(i,1),l(i,1),l(i,1)-1,l(i,1)-
1],[l(i,2),l(i,2)+1,l(i,2)+1,l(i,2)],'r','FaceAlpha',0.3);
hold on
end
grid on
Published with MATLAB® R2022b

You might also like