用不同的颜色绘制一个用三分之一划分的圆_ MATLAB

时间:2017-02-06 16:08:06

标签: matlab plot colors matlab-figure geometry

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

我开始画一个圆圈并用一种颜色填充它。然后我将该圆分成3个相等的区域。现在我试图给圆圈的每个“切片”赋予不同的颜色。有人能帮我吗?

提前谢谢。

这是我的代码:

% 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

1 个答案:

答案 0 :(得分:0)

您应该使用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