matble中生成Voronoi图

 

 

Definitions

Consider a set of coplanar points P. For each point Px in the set P, you can draw a boundary enclosing all the intermediate points lying closer to Px than to other points in the set P. Such a boundary is called a Voronoi polygon, and the set of all Voronoi polygons for a given point set is called a Voronoi diagram.

Visualization

Use one of these methods to plot a Voronoi diagram:

                      If you provide no output argument, voronoi plots the diagram.

                      To gain more control over color, line style, and other figure properties, use the syntax [vx,vy] = voronoi(...). This syntax returns the vertices of the finite Voronoi edges, which you can then plot with the plot function.

                      To fill the cells with color, use voronoin with n = 2 to get the indices of each cell, and then use patch and other plot functions to generate the figure. Note that patch does not fill unbounded cells with color.

Examples

Example 1

This code uses the voronoi function to plot the Voronoi diagram for 10 randomly generated points.

x = gallery('uniformdata',[1 10],0);

y = gallery('uniformdata',[1 10],1);

voronoi(x,y)

Example 2

This code uses the vertices of the finite Voronoi edges to plot the Voronoi diagram for the same 10 points.

x = gallery('uniformdata',[1 10],0);

y = gallery('uniformdata',[1 10],1);

[vx, vy] = voronoi(x,y);

plot(x,y,'r+',vx,vy,'b-'); axis equal

Note that you can add this code to get the figure shown in Example 1.

     xlim([min(x) max(x)])

     ylim([min(y) max(y)])

Example 3

This code uses voronoin and patch to fill the bounded cells of the same Voronoi diagram with color.

x = gallery('uniformdata',[10 2],5);

[v,c]=voronoin(x);

for i = 1:length(c)

if all(c{i}~=1)   % If at least one of the indices is 1,

                  % then it is an open region and we can't

                  % patch that.

patch(v(c{i},1),v(c{i},2),i); % use color i.

end

end

 

  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值