How do I change the colour in each sector depending on the angle?
조회 수: 2 (최근 30일)
이전 댓글 표시
I created 6 sectors via linspace and patch function. However I can't figure out how to fill in each sector depending on the heading. e.g from sample code, when heading 90 degree that sector should be '5' or yellow, the other sectors towards the left/right should be getting more and more 'blue' or towards the value '1'. It will be great if I can define the starting value for each sector or reducing as it move towards the left/right of the heading.
Many thanks for the help in advance.
x0=0
y0=0
r=5
angleDEG=90
angleRAD=angleDEG/180*pi
HSpread=45
HSpreadRAD=HSpread/180*pi
t = linspace(angleRAD-HSpreadRAD,angleRAD+HSpreadRAD,6); %(begining angle, end angle, resolution)
x = [x0 (x0+r*cos(t)) x0];
y = [y0 (y0+r*sin(t)) y0];
c = linspace(1,5,length(y));
figure;
a=patch(x,y,c)
a.FaceAlpha = 0.3;
colorbar
axis equal
Currently this is what I got (pic insert):
댓글 수: 0
채택된 답변
DGM
2021년 8월 3일
편집: DGM
2021년 8월 3일
I don't know about how to wrangle patch() to do it right. It probably can, but I don't have that much hair to spare. This is a version using surf() instead. Anyone is free to trump this with a solution using patch().
% parameters
x0 = 0;
y0 = 0;
rmax = 5;
th0 = 45; % start angle
thInc = 90; % included angle
rsteps = 10;
thsteps = 6;
ang = [th0 th0+thInc]/180*pi;
r = linspace(0,rmax,rsteps);
t = linspace(ang(1),ang(2),thsteps).';
x = x0 + r.*cos(t);
y = y0 + r.*sin(t);
z = ones(thsteps,rsteps).*0; % just plot everything in x-y plane
c = repmat(8*(0.5-abs((t-min(ang))/range(ang)-0.5)) + 1,[1 rsteps]);
a = surf(x,y,z,c);
a.FaceAlpha = 0.5;
% do the border
xl = [x0 x0+rmax*cos(t.') x0];
yl = [y0 y0+rmax*sin(t.') y0];
line(xl,yl,'color','k')
shading interp
colorbar
caxis([1 5])
axis equal
view(2)
댓글 수: 6
DGM
2021년 8월 5일
I don't know. That looks pretty good to me. If it works well for your needs, I'd roll with it.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Surface and Mesh Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!