中点画圆和Bresenham画圆
程序员文章站
2022-03-22 14:45:33
...
下面是用matlab实现的中点画圆和Bresenham画圆
这两个function都用到了圆的八对称性,故先把这个函数的实现撂在这:
function DrawCirclePoint(x0,y0,x,y)
plot(x+x0,y+y0,'rs');
plot(-x+x0,y+y0,'rs');
plot(x+x0,-y+y0,'rs');
plot(-x+x0,-y+y0,'rs');
plot(y+x0,x+y0,'rs');
plot(-y+x0,x+y0,'rs');
plot(y+x0,-x+y0,'rs');
plot(-y+x0,-x+y0,'rs');
hold on;
end
下面是中点画圆函数实现
function MidpointCircle(x0,y0,r)
%用本身的自带函数画的圆
rectangle('Position',[x0-r,y0-r,2*r,2*r],'Curvature',[1,1],'linewidth',1);
axis equal;hold on;grid on;
x = 0; y=r; d=1.25-r;
DrawCirclePoint(x0,y0,x,y);%利用圆的八对称性
while(x<=y)
plot(x+8,y+8,'rs');
hold on;
if(d<0)
d = d+2*x+3; x = x + 1;
else
d = d+2*(x-y) + 5;
x = x+1;
y = y -1;
end
DrawCirclePoint(x0,y0,x,y);
end
end
下面是Bresenham画圆
function BresenhamCircleMy(x0,y0,r)
axis equal;
grid on;
rectangle('Position',[x0-r,y0-r,2*r,2*r],'Curvature',[1,1],'linewidth',1);%这一句是画个正经的圆
hold on;
title('Bresenham画圆');
x=0;y=r;p=3-2*r;
DrawCirclePoint(x0,y0,x,y);%利用圆的八对称性
while x <= y
if p<0
p=p+4*x+6;
else
p=p+4*(x-y)+10;
y=y-1;
end
x=x+1;
DrawCirclePoint(x0,y0,x,y);%利用圆的八对称性
end
end
上一篇: 春分一般吃啥?这些食物一定不能错过!
下一篇: 苹果为汽车项目从各部门抽调人才