圆形区域内函数的渐变填充Matlab
Gradient fill of a function inside a circular area Matlab
我正在尝试根据给定函数在圆形区域内创建渐变填充。我希望下面的情节充其量能解释它
我不确定如何处理这个问题,因为在模拟中我正在研究梯度变化的方向(并不总是像下面的 x 方向,但可以自由地沿着所有定义的角度),所以我寻找一种同样灵活的解决方案。
我的代码如下
clear t
N=10;
for i=0:N
t(i+1) = 0+(2*i*pi) / N;
end
F = exp(-cos(t))./(2.*pi*besseli(1,1));
figure(1)
subplot(1,3,1)
plot(t*180/pi,F,'-ob')
xlim([0 360])
xlabel('angle')
subplot(1,3,2)
hold on
plot(cos([t 2*pi]), sin([t 2*pi]),'-k','linewidth',2);
plot(cos([t 2*pi]), sin([t 2*pi]),'ob');
plot(cos(t).*F,sin(t).*F,'b','linewidth',2);
subplot(1,3,3)
hold on
plot(cos([t 2*pi]), sin([t 2*pi]),'-k','linewidth',2);
plot(cos([t 2*pi]), sin([t 2*pi]),'ob');
要填充表面,您需要使用patch命令。
t = linspace(0, 2*pi, 100);
x = cos(t);
y = sin(t);
c = x; % colored following x value and current colormap
figure
patch(x,y,c)
hold on
scatter(x,y)
hold off
colorbar
结果图:
颜色在每个点的 c 中定义,并在形状内插值,所以我相信您应该可以自由地随心所欲地着色!
例如旋转版本:
t = linspace(0, 2*pi, 100);
x = cos(t);
y = sin(t);
c = cos(t+pi/4)
figure
patch(x,y,c)
colorbar
想明白是怎么回事,就想每个点都有颜色,matlab在里面插值。所以在这里我只是将每个点的强度旋转了 pi /4。
为此,您需要填充形状,并且您可能需要自定义颜色 (c) 参数以使其符合您的需要。例如,如果你的渐变方向被编码在一个向量中,你想要将所有点投影到该向量上以获得所有点沿渐变的值。
例如:
% v controls the direction of the gradient
v = [0.1, 1];
t = linspace(0, 2*pi, 100);
F = exp(-cos(t))./(2.*pi*besseli(1,1));
% reconstructing point coordinate all around the surface
% this closes the path so with enough points so that interpolation works correctly
pts = [[t', F']; [t(end:-1:1)', ones(size(t'))*min(F)]];
% projecting all points on the vector to get the color
c = pts * (v');
clf
patch(pts(:,1),pts(:,2),c)
hold on
scatter(t, F)
hold off
我正在尝试根据给定函数在圆形区域内创建渐变填充。我希望下面的情节充其量能解释它
我的代码如下
clear t
N=10;
for i=0:N
t(i+1) = 0+(2*i*pi) / N;
end
F = exp(-cos(t))./(2.*pi*besseli(1,1));
figure(1)
subplot(1,3,1)
plot(t*180/pi,F,'-ob')
xlim([0 360])
xlabel('angle')
subplot(1,3,2)
hold on
plot(cos([t 2*pi]), sin([t 2*pi]),'-k','linewidth',2);
plot(cos([t 2*pi]), sin([t 2*pi]),'ob');
plot(cos(t).*F,sin(t).*F,'b','linewidth',2);
subplot(1,3,3)
hold on
plot(cos([t 2*pi]), sin([t 2*pi]),'-k','linewidth',2);
plot(cos([t 2*pi]), sin([t 2*pi]),'ob');
要填充表面,您需要使用patch命令。
t = linspace(0, 2*pi, 100);
x = cos(t);
y = sin(t);
c = x; % colored following x value and current colormap
figure
patch(x,y,c)
hold on
scatter(x,y)
hold off
colorbar
结果图:
颜色在每个点的 c 中定义,并在形状内插值,所以我相信您应该可以自由地随心所欲地着色!
例如旋转版本:
t = linspace(0, 2*pi, 100);
x = cos(t);
y = sin(t);
c = cos(t+pi/4)
figure
patch(x,y,c)
colorbar
想明白是怎么回事,就想每个点都有颜色,matlab在里面插值。所以在这里我只是将每个点的强度旋转了 pi /4。
为此,您需要填充形状,并且您可能需要自定义颜色 (c) 参数以使其符合您的需要。例如,如果你的渐变方向被编码在一个向量中,你想要将所有点投影到该向量上以获得所有点沿渐变的值。
例如:
% v controls the direction of the gradient
v = [0.1, 1];
t = linspace(0, 2*pi, 100);
F = exp(-cos(t))./(2.*pi*besseli(1,1));
% reconstructing point coordinate all around the surface
% this closes the path so with enough points so that interpolation works correctly
pts = [[t', F']; [t(end:-1:1)', ones(size(t'))*min(F)]];
% projecting all points on the vector to get the color
c = pts * (v');
clf
patch(pts(:,1),pts(:,2),c)
hold on
scatter(t, F)
hold off