【问题标题】:Plotting a circle divided in thirds with different colours each_ MATLAB用不同的颜色绘制一个三等分的圆_MATLAB
【发布时间】:2017-02-06 16:08:05
【问题描述】:

我想在 MATLAB 中绘制一个三等分的圆,并用不同的 RGB 颜色填充每三分之一。

我首先画了一个圆圈并用一种颜色填充它。然后我把那个圆圈分成三个相等的区域。现在我试图为圆圈的每个“切片”赋予不同的颜色。有人能帮我吗?

提前谢谢你。

这是我的代码:

% FilledCircle3 : function that takes 5 inputs
% 
% Call the function:
% FilledCircle3(x0,Row,Radius,N,Color)
% 
% Inputs Types
% ------------
%  x0 - Integer, Float
%  y0    - Integer, Float
%  Radius - Integer, Float
%  N      - Integer
%  Color  - Character String
% 
% Notes on N: The more N increases, the more accurate  is the circle
%             The standard value for N is 256
% 
% Notes on Color: 
%  'b'     blue          
%  'g'     green         
%  'r'     red           
%  'c'     cyan            
%  'm'     magenta       
%  'y'     yellow       



function []=FilledCircle3(x0,y0,Radius,N,Color)

if(N<=1)
    error('N must be greater than 1');
end

if (Color ~='b') && (Color ~='g') && (Color ~= 'r') && (Color ~='c') && (Color ~='m') && (Color ~='y') 
    error('This is not an available color');
end
hold on
axis equal
t=(0:N)*2*pi/N; %t=-pi:0.01:pi
x=Radius*cos(t)+x0;
y=Radius*sin(t)+y0;
plot(x,y,Color, fill(x,y,Color));
hold on 

% Divide circle into 3 sectors
n=3
tet=linspace(-pi,pi,n+1)
xi=r*cos(tet)+x0
yi=r*sin(tet)+y0
for k=1:numel(xi)
plot([x0 xi(k)],[y0 yi(k)])
hold on
end

【问题讨论】:

    标签: matlab plot colors matlab-figure geometry


    【解决方案1】:

    您应该使用fill 创建填充形状。

    thetas = linspace(-pi, pi, n+1);
    
    % Specify any colors that you want
    colors = hsv(n);
    
    for k = 1:n
        tt = linspace(thetas(k), thetas(k+1));
        xi = r * cos(tt) + x0;
        yi = r * sin(tt) + y0;
    
        fill([xi(:); x0], [yi(:); y0], colors(k,:));
    
        hold on
    end
    

    【讨论】:

    • 谢谢!您选择 hsv 颜色@Suever 有什么原因吗?
    • @Mraquel 我选择的颜色是任意的。你可以使用任何你想要的颜色。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多