function DrawLineBresenham(x1,y1,x2,y2)
%sort by x,sure x1<x2.
if x1>x2
tmp=x1;
x1=x2;
x2=tmp;
tmp=y1;
y1=y2;
y2=tmp;
end
dx=x2-x1;
dy=y2-y1;
twoDy=2*dy;
twoDy_Dx=2*(dy-dx);
twoDx=2*dx;
twoDx_Dy=2*(dx-dy);
twoDxPlusDy=2*(dx+dy);
%branch 1: k>0 ?
%k>0 <=> dy>0
if dy>0 %k>0
if dx==0 %if k==INF
for i=y1:y2
plot(x1,i,'*');
hold('on');
end
return ;
end
%branch 2:k=INF
%branch 3: k<1 <=> dy<dx
if dy<dx %k<1
p= twoDy-dx;
plot(x1,y1,'*');
hold('on');
while x1<x2
x1=x1+1;
if(p<0)
p=p+twoDy;
else
y1=y1+1;
p=p+twoDy_
Bresenham画直线,任意斜率
最新推荐文章于 2022-03-18 17:44:25 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)