MATLAB绘图笔记——画圆的几种方法

列举了三种画圆的方法,推荐使用方法3。

方法1:

filledCircle([1,3],3,100,'k'); 
hold on 
filledCircle([-0.25,4.25],1,100,'r'); 
filledCircle([2.5,3],1,100,'g'); 
filledCircle([0.1,1.7],1,100,'b'); 
axis off
function h = filledCircle(center,r,N,color)
%---------------------------------------------------------------------------------------------
% FILLEDCIRCLE Filled circle drawing
% 
% filledCircle(CENTER,R,N,COLOR) draws a circle filled with COLOR that 
% has CENTER as its center and R as its radius, by using N points on the 
% periphery.
%
% Usage Examples,
%
% filledCircle([1,3],3,1000,'b'); 
% filledCircle([2,4],2,1000,'r');
%
% Sadik Hava <sadik.hava@gmail.com>
% May, 2010
%
% Inspired by: circle.m [Author: Zhenhai Wang]
%---------------------------------------------------------------------------------------------
THETA=linspace(0,2*pi,N);
RHO=ones(1,N)*r;
[X,Y] = pol2cart(THETA,RHO);
X=X+center(1);
Y=Y+center(2);
h=fill(X,Y,color);
axis square;
% COPYRIGHT STUFF... :D (Since I am modifying Zhenhai's code.)
%
% Copyright (c) 2002, Zhenhai Wang
% All rights reserved.
% 
% Redistribution and use in source and binary forms, with or without 
% modification, are permitted provided that the following conditions are 
% met:
% 
%     * Redistributions of source code must retain the above copyright 
%       notice, this list of conditions and the following disclaimer.
%     * Redistributions in binary form must reproduce the above copyright 
%       notice, this list of conditions and the following disclaimer in 
%       the documentation and/or other materials provided with the distribution
%       
% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
% AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
% IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
% ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
% LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
% CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
% SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
% INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
% CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
% ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
% POSSIBILITY OF SUCH DAMAGE.

方法2:
绘制圆的最简单(尽管不是很直观)的方法是使用矩形函数,其中’curvature’属性设置为[1 1]。 要填充圆圈,请通过“facecolor”属性指定颜色。 圆边框的颜色由’edgecolor’属性控制。

MinVal = -1;
MaxVal = 1;
MaxRadius = 0.5;
nCircles = 5;
Dimension = 2;
Circles = zeros(nCircles, Dimension);
Radius = zeros(nCircles, 1);
cmap = hsv(nCircles); %// define colors. You could change `hsv` to `jet`, `cool`, ...

for i = 1 : nCircles
    Circles(i,:) = unifrnd(MinVal, MaxVal, [1, Dimension]);
    Radius(i) = unifrnd(0, MaxRadius, 1);
end

figure;
hold on;
for i = 1 : nCircles
    rectangle('Curvature', [1 1], ...
        'Position', [Circles(i,:)-Radius(i) repmat(2*Radius(i),1,2)], ...
        'facecolor', cmap(i,:), 'edgecolor', 'none') %// plot filled circle
end

axis equal; %// same aspect ratio in both axes
grid on;

方法3:
要使圆可见,即使它们已被遮盖,您也可以使用具有透明度(alpha)的颜色。 矩形不支持透明度,因此必须使用patch功能。通过patch绘图并通过适当的属性指定颜色和透明度。

MinVal = -1;
MaxVal = 1;
MaxRadius = 0.5;
nCircles = 5;
Dimension = 2;
Circles = zeros(nCircles, Dimension);
Radius = zeros(nCircles, 1);
cmap = hsv(nCircles); %// define colors. You could change `hsv` to `jet`, `cool`, ...
alpha = .5; %// define level of transparency

for i = 1 : nCircles
    Circles(i,:) = unifrnd(MinVal, MaxVal, [1, Dimension]);
    Radius(i) = unifrnd(0, MaxRadius, 1);
end

t = 0 : .1 : 2 * pi;

figure;
hold on;
for i = 1 : nCircles
    x = Radius(i) * cos(t) + Circles(i,1);
    y = Radius(i) * sin(t) + Circles(i,2);
    patch(x, y, cmap(i,:), 'facealpha', alpha, 'edgecolor', 'none'); %// plot filled circle with transparency
end

axis equal; %// same aspect ratio in both axes
grid on;

参考
https://stackoverflow.com/questions/30823519/how-colorize-circles-in-a-plot-in-matlab

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值