Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
function bresenham_line()
clc
clear all
point = input('Give coordinates[ x0 y0 x1 y1]: ');
else
x0 = point(1);y0 = point(2); x1 = point(3);y1=point(4); % m<1
token = 0;
end
if(x0 >x1)
temp1 = x0; x0 = x1; x1 = temp1; % m>1
temp2 = y0; y0 = y1; y1 = temp2;
end
dx = abs(x1 - x0) ;
dy = abs(y1 - y0);
sx = sign(x1 - x0);
sy = sign(y1 - y0);
end
x = x + 1*sx; % X-coordinate is
increased for next point
end
subplot(2,1,2) % Plotting in line
fragment ‘form
if (token ==0)
plot(x_coord,y_coord,'r-','LineWidth',2);
else
plot(y_coord,x_coord,'r-','LineWidth',2);
end
grid on
axis([x0-1 x1+1 y0-1 y1+1]);
title('Bresenham Line Generation Algorithm: Line fragment form')
function draw(x1,y1)
clc
clear
point = input('Give coordinates[ x0 y0 x1 y1]: ');
x0 = point(1);y0 = point(2); x1 = point(3);y1=point(4);
dx = abs(x1-x0);
dy = abs(y1-y0);
sx = sign(x1-x0);
sy = sign(y1-y0);
if (dy > dx)
step = dy;
else
step = dx;
end
x(1) = x0; y(1) = y0; j = 1;
for i= 0:1:step
if (x1 == x)&(y1 == y)
break;
end
j = j+1;
x(j) = x(j-1) + (dx/step)*sx;
y(j) = y(j-1) + (dy/step)*sy;
end
plot(round(x),round(y),'rs');
grid on ,hold on
plot(x,y,'b*');
title('Digital Differential Line Drawing Algorithm')
MID POINT CIRCLE GENERATION ALGORITHM
octant_size = floor((sqrt(2)*(radius-1)+4)/2);
n_points = 8 * octant_size;
x = 0;
y = radius;
f = 1 - radius;
dx = 1;
dy = - 2 * radius;
% Store
% 1 octant
xc(1) = x0 + x;
yc(1) = y0 + y;
% 2nd octant
xc(8 * octant_size) = x0 - x;
yc(8 * octant_size) = y0 + y;
% 3rd octant
xc(4 * octant_size) = x0 + x;
yc(4 * octant_size) = y0 - y;
% 4th octant
xc(4 * octant_size + 1) = x0 - x;
yc(4 * octant_size + 1) = y0 - y;
% 5th octant
xc(2 * octant_size) = x0 + y;
yc(2 * octant_size) = y0 + x;
% 6th octant
xc(6 * octant_size + 1) = x0 - y;
yc(6 * octant_size + 1) = y0 + x;
% 7th octant
xc(2 * octant_size + 1) = x0 + y;
yc(2 * octant_size + 1) = y0 - x;
% 8th octant
xc(6 * octant_size) = x0 - y;
yc(6 * octant_size) = y0 - x;
for i = 2 : n_points/8
% We update x & y
if f > 0
y = y - 1;
dy = dy + 2;
f = f + dy;
end
x = x + 1;
dx = dx + 2;
f = f + dx;
% 1 octant
xc(i) = x0 + x;
yc(i) = y0 + y;
% 2nd octant
xc(8 * octant_size - i + 1) = x0 - x;
yc(8 * octant_size - i + 1) = y0 + y;
% 3rd octant
xc(4 * octant_size - i + 1) = x0 + x;
yc(4 * octant_size - i + 1) = y0 - y;
% 4th octant
xc(4 * octant_size + i) = x0 - x;
yc(4 * octant_size + i) = y0 - y;
% 5th octant
xc(2 * octant_size - i + 1) = x0 + y;
yc(2 * octant_size - i + 1) = y0 + x;
% 6th octant
xc(6 * octant_size + i) = x0 - y;
yc(6 * octant_size + i) = y0 + x;
% 7th octant
xc(2 * octant_size + i) = x0 + y;
yc(2 * octant_size + i) = y0 - x;
% 8th octant
xc(6 * octant_size - i + 1) = x0 - y;
yc(6 * octant_size - i + 1) = y0 - x;
end
function bresenham_circle(a,b,r)
clc, clear all, clf,close
a = input('Give the centre X coord : '); % X coord of centre
b = input('Give the centre Y coord : '); % Y coord of centre
r = input('Give the radius of circle : '); % Radius of the circle to be drawn
inc = input('Give the increment step for coordinate[Default:1]: '); % Use the
value
if isempty(inc) % of inc greater than 1
inc =1; % usually less than radius
End
x = 0;
y = r;
d= 3-2*r;
while(x < y)
funplot(a,b,x,y,r);
if d>=0
d = d +4*(x-y)+10;
x = x + 1/inc;
y = y - 1/inc;
else
d = d + 4*x +6;
x = x+1/inc;
end
End
function funplot(a,b,p,q,r)
for i= -1:2:1
for j= -1:2:1
plot(a+i*p,b+j*q,'r.');
plot(a+i*q,b+j*p,'r.');
grid off
hold on
end
end
title('Bresenham circle')
axis([a-r-1 a+r+1 b-r-1 b+r+1])
axis equal;
function ellipse()
clc,clear all
c = input('Give the centre coord[x,y]: '); %INTERACTIVE INPUT OF
CENTRE AND RADIUS
a = input('Give the value of major/minor axis[a,b]: ');
x = 0;
y = a(2);
p1= a(2)*a(2) - a(1)*a(1)*a(2) + 0.25*a(1)*a(1);
p2 = a(2)*a(2)*(x + 0.5)*(x+0.5) + a(1)*a(1)*(y-1)*(y-1) -
a(1)*a(1)*a(2)*a(2);
while (a(2)*a(2)*x) < (a(1)*a(1)*y)
for i= -1:2:1
for j= -1:2:1
plot(c(1)+i*x,c(2)+j*y,'r.');
hold on
end
end
if(p1<0)
p1 = p1 + 2*a(2)*a(2)*x + 3*a(2)*a(2);
x = x+1;
else
p1 = p1 - 2*a(1)*a(1)*y + 2*a(2)*a(2)*x + 2*a(1)*a(1) +
3*a(2)*a(2);
x = x + 1;
y = y - 1;
end
end
while y > 0
for i= -1:2:1
for j= -1:2:1
plot(c(1)+i*x,c(2)+j*y,'r.');
end
end
if(p2 < 0)
p2 = p2 + 2*a(2)*a(2)*x - 2*a(1)*a(1)*y + 3*a(1)*a(1) +
2*a(2)*a(2);
x = x + 1;
y = y - 1;
else
p1 = p1 - 2*a(1)*a(1)*y + 3*a(1)*a(1);
y = y - 1;
end
end
grid on , axis square
axis([c(1)-a(1)-5 c(1)+a(1)+5 c(2)-a(2)-5 c(2)+a(2)+5]);
title('Ellipse generation by Midpoint generation algorithm')
1. SLOPE ALGORITHM
function draw(x,y)
clc
clear
point = input('Give coord[ x1 y1 x2 y2]: ');
x1 = point(1);y1 = point(2); x2 =
point(3);y2=point(4);
dx = x2-x1;
step=abs(dx);
dy = y2-y1;
m=(dy/dx);
c=y1-(m*x1);
if(x2>x1)
x=x1;xend=x2;
else
x=x2;xend=x1;
end
for i=0:1:step
y=(m*x)+c;
plot(x,y,'rs');
hold on
grid on
plot(round(x),round(y),'b*');
x=x+1;
end
end