Gradient fill of a function inside a circular area Matlab
up vote
2
down vote
favorite
I'm trying to create a gradient fill inside a circular area according to a given function. I hope the plot below explains it at best

I'm not sure how to approach this, as in the simulation I'm working on the direction of the gradient changes (not always in the x direction as below, but free to be along all the defined angles), so I'm looking for a solution that will be flexible in that manner as well.
The code I have is below
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');
matlab gradient
|
show 4 more comments
up vote
2
down vote
favorite
I'm trying to create a gradient fill inside a circular area according to a given function. I hope the plot below explains it at best

I'm not sure how to approach this, as in the simulation I'm working on the direction of the gradient changes (not always in the x direction as below, but free to be along all the defined angles), so I'm looking for a solution that will be flexible in that manner as well.
The code I have is below
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');
matlab gradient
As I see it here you have a function of 2 variables, which in turn calculate a third (something like F(x,y)). Then any colourmap will colour along the third dimension, probably colouring your gradient.
– Adriaan
Nov 7 at 9:13
@Adriaan, not exactly, it is even simpler. The function is defined soley by the values at the perimeter (blue circles), therefore if the gradient for example is along the x axis, the function is only of x.
– jarhead
Nov 7 at 9:44
What is the issue? Filling an area from its boundary values? That is non-trivial (and non-unique) so you need to define yourself how to do it. If the plotting is an issue, generating a meshgrid with values and usingimagescshould work
– Ander Biguri
Nov 7 at 10:23
1
have you looked at thepatchgraphics object? You can define several polygons, define a value/color for vertices, edges and surface with a number of interpolation options.
– Brice
Nov 7 at 10:48
I don't understand the second (intermediate) mapping plot. From the first plot i get that you want to attach a color to a value of the curve ... but how does that project to your circle in the third picture ? ... and where do you calculate a gradient ?
– Hoki
Nov 7 at 10:56
|
show 4 more comments
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I'm trying to create a gradient fill inside a circular area according to a given function. I hope the plot below explains it at best

I'm not sure how to approach this, as in the simulation I'm working on the direction of the gradient changes (not always in the x direction as below, but free to be along all the defined angles), so I'm looking for a solution that will be flexible in that manner as well.
The code I have is below
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');
matlab gradient
I'm trying to create a gradient fill inside a circular area according to a given function. I hope the plot below explains it at best

I'm not sure how to approach this, as in the simulation I'm working on the direction of the gradient changes (not always in the x direction as below, but free to be along all the defined angles), so I'm looking for a solution that will be flexible in that manner as well.
The code I have is below
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');
matlab gradient
matlab gradient
asked Nov 7 at 9:02
jarhead
60741334
60741334
As I see it here you have a function of 2 variables, which in turn calculate a third (something like F(x,y)). Then any colourmap will colour along the third dimension, probably colouring your gradient.
– Adriaan
Nov 7 at 9:13
@Adriaan, not exactly, it is even simpler. The function is defined soley by the values at the perimeter (blue circles), therefore if the gradient for example is along the x axis, the function is only of x.
– jarhead
Nov 7 at 9:44
What is the issue? Filling an area from its boundary values? That is non-trivial (and non-unique) so you need to define yourself how to do it. If the plotting is an issue, generating a meshgrid with values and usingimagescshould work
– Ander Biguri
Nov 7 at 10:23
1
have you looked at thepatchgraphics object? You can define several polygons, define a value/color for vertices, edges and surface with a number of interpolation options.
– Brice
Nov 7 at 10:48
I don't understand the second (intermediate) mapping plot. From the first plot i get that you want to attach a color to a value of the curve ... but how does that project to your circle in the third picture ? ... and where do you calculate a gradient ?
– Hoki
Nov 7 at 10:56
|
show 4 more comments
As I see it here you have a function of 2 variables, which in turn calculate a third (something like F(x,y)). Then any colourmap will colour along the third dimension, probably colouring your gradient.
– Adriaan
Nov 7 at 9:13
@Adriaan, not exactly, it is even simpler. The function is defined soley by the values at the perimeter (blue circles), therefore if the gradient for example is along the x axis, the function is only of x.
– jarhead
Nov 7 at 9:44
What is the issue? Filling an area from its boundary values? That is non-trivial (and non-unique) so you need to define yourself how to do it. If the plotting is an issue, generating a meshgrid with values and usingimagescshould work
– Ander Biguri
Nov 7 at 10:23
1
have you looked at thepatchgraphics object? You can define several polygons, define a value/color for vertices, edges and surface with a number of interpolation options.
– Brice
Nov 7 at 10:48
I don't understand the second (intermediate) mapping plot. From the first plot i get that you want to attach a color to a value of the curve ... but how does that project to your circle in the third picture ? ... and where do you calculate a gradient ?
– Hoki
Nov 7 at 10:56
As I see it here you have a function of 2 variables, which in turn calculate a third (something like F(x,y)). Then any colourmap will colour along the third dimension, probably colouring your gradient.
– Adriaan
Nov 7 at 9:13
As I see it here you have a function of 2 variables, which in turn calculate a third (something like F(x,y)). Then any colourmap will colour along the third dimension, probably colouring your gradient.
– Adriaan
Nov 7 at 9:13
@Adriaan, not exactly, it is even simpler. The function is defined soley by the values at the perimeter (blue circles), therefore if the gradient for example is along the x axis, the function is only of x.
– jarhead
Nov 7 at 9:44
@Adriaan, not exactly, it is even simpler. The function is defined soley by the values at the perimeter (blue circles), therefore if the gradient for example is along the x axis, the function is only of x.
– jarhead
Nov 7 at 9:44
What is the issue? Filling an area from its boundary values? That is non-trivial (and non-unique) so you need to define yourself how to do it. If the plotting is an issue, generating a meshgrid with values and using
imagesc should work– Ander Biguri
Nov 7 at 10:23
What is the issue? Filling an area from its boundary values? That is non-trivial (and non-unique) so you need to define yourself how to do it. If the plotting is an issue, generating a meshgrid with values and using
imagesc should work– Ander Biguri
Nov 7 at 10:23
1
1
have you looked at the
patch graphics object? You can define several polygons, define a value/color for vertices, edges and surface with a number of interpolation options.– Brice
Nov 7 at 10:48
have you looked at the
patch graphics object? You can define several polygons, define a value/color for vertices, edges and surface with a number of interpolation options.– Brice
Nov 7 at 10:48
I don't understand the second (intermediate) mapping plot. From the first plot i get that you want to attach a color to a value of the curve ... but how does that project to your circle in the third picture ? ... and where do you calculate a gradient ?
– Hoki
Nov 7 at 10:56
I don't understand the second (intermediate) mapping plot. From the first plot i get that you want to attach a color to a value of the curve ... but how does that project to your circle in the third picture ? ... and where do you calculate a gradient ?
– Hoki
Nov 7 at 10:56
|
show 4 more comments
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
To fill surface, you need to use the patch command.
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
Resulting graph:

Colors are defined in c per point, and are interpolated inside the shape, so I'm sure that you should have all freedom to color as you want!
For example, the rotated version:
t = linspace(0, 2*pi, 100);
x = cos(t);
y = sin(t);
c = cos(t+pi/4)
figure
patch(x,y,c)
colorbar

To understand how it is going on, just think that every point has a color, and matlab interpolate inside. So here I just rotated the intensity per point by pi /4.
For this to work you need to have a filled shape, and you may need to customize the color (c) parameter so that it matches your need. For example, if your gradient direction is encoded in a vector, you want to project all your point onto that vector to get the value along the gradient for all points.
For example:
% 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

this is great, but how can you rotate the gradient to a different angle?
– jarhead
Nov 7 at 15:28
for rotation, see my edits!
– beesleep
Nov 7 at 16:50
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
To fill surface, you need to use the patch command.
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
Resulting graph:

Colors are defined in c per point, and are interpolated inside the shape, so I'm sure that you should have all freedom to color as you want!
For example, the rotated version:
t = linspace(0, 2*pi, 100);
x = cos(t);
y = sin(t);
c = cos(t+pi/4)
figure
patch(x,y,c)
colorbar

To understand how it is going on, just think that every point has a color, and matlab interpolate inside. So here I just rotated the intensity per point by pi /4.
For this to work you need to have a filled shape, and you may need to customize the color (c) parameter so that it matches your need. For example, if your gradient direction is encoded in a vector, you want to project all your point onto that vector to get the value along the gradient for all points.
For example:
% 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

this is great, but how can you rotate the gradient to a different angle?
– jarhead
Nov 7 at 15:28
for rotation, see my edits!
– beesleep
Nov 7 at 16:50
add a comment |
up vote
2
down vote
accepted
To fill surface, you need to use the patch command.
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
Resulting graph:

Colors are defined in c per point, and are interpolated inside the shape, so I'm sure that you should have all freedom to color as you want!
For example, the rotated version:
t = linspace(0, 2*pi, 100);
x = cos(t);
y = sin(t);
c = cos(t+pi/4)
figure
patch(x,y,c)
colorbar

To understand how it is going on, just think that every point has a color, and matlab interpolate inside. So here I just rotated the intensity per point by pi /4.
For this to work you need to have a filled shape, and you may need to customize the color (c) parameter so that it matches your need. For example, if your gradient direction is encoded in a vector, you want to project all your point onto that vector to get the value along the gradient for all points.
For example:
% 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

this is great, but how can you rotate the gradient to a different angle?
– jarhead
Nov 7 at 15:28
for rotation, see my edits!
– beesleep
Nov 7 at 16:50
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
To fill surface, you need to use the patch command.
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
Resulting graph:

Colors are defined in c per point, and are interpolated inside the shape, so I'm sure that you should have all freedom to color as you want!
For example, the rotated version:
t = linspace(0, 2*pi, 100);
x = cos(t);
y = sin(t);
c = cos(t+pi/4)
figure
patch(x,y,c)
colorbar

To understand how it is going on, just think that every point has a color, and matlab interpolate inside. So here I just rotated the intensity per point by pi /4.
For this to work you need to have a filled shape, and you may need to customize the color (c) parameter so that it matches your need. For example, if your gradient direction is encoded in a vector, you want to project all your point onto that vector to get the value along the gradient for all points.
For example:
% 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

To fill surface, you need to use the patch command.
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
Resulting graph:

Colors are defined in c per point, and are interpolated inside the shape, so I'm sure that you should have all freedom to color as you want!
For example, the rotated version:
t = linspace(0, 2*pi, 100);
x = cos(t);
y = sin(t);
c = cos(t+pi/4)
figure
patch(x,y,c)
colorbar

To understand how it is going on, just think that every point has a color, and matlab interpolate inside. So here I just rotated the intensity per point by pi /4.
For this to work you need to have a filled shape, and you may need to customize the color (c) parameter so that it matches your need. For example, if your gradient direction is encoded in a vector, you want to project all your point onto that vector to get the value along the gradient for all points.
For example:
% 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

edited Nov 7 at 15:55
answered Nov 7 at 15:02
beesleep
559210
559210
this is great, but how can you rotate the gradient to a different angle?
– jarhead
Nov 7 at 15:28
for rotation, see my edits!
– beesleep
Nov 7 at 16:50
add a comment |
this is great, but how can you rotate the gradient to a different angle?
– jarhead
Nov 7 at 15:28
for rotation, see my edits!
– beesleep
Nov 7 at 16:50
this is great, but how can you rotate the gradient to a different angle?
– jarhead
Nov 7 at 15:28
this is great, but how can you rotate the gradient to a different angle?
– jarhead
Nov 7 at 15:28
for rotation, see my edits!
– beesleep
Nov 7 at 16:50
for rotation, see my edits!
– beesleep
Nov 7 at 16:50
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53186266%2fgradient-fill-of-a-function-inside-a-circular-area-matlab%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
As I see it here you have a function of 2 variables, which in turn calculate a third (something like F(x,y)). Then any colourmap will colour along the third dimension, probably colouring your gradient.
– Adriaan
Nov 7 at 9:13
@Adriaan, not exactly, it is even simpler. The function is defined soley by the values at the perimeter (blue circles), therefore if the gradient for example is along the x axis, the function is only of x.
– jarhead
Nov 7 at 9:44
What is the issue? Filling an area from its boundary values? That is non-trivial (and non-unique) so you need to define yourself how to do it. If the plotting is an issue, generating a meshgrid with values and using
imagescshould work– Ander Biguri
Nov 7 at 10:23
1
have you looked at the
patchgraphics object? You can define several polygons, define a value/color for vertices, edges and surface with a number of interpolation options.– Brice
Nov 7 at 10:48
I don't understand the second (intermediate) mapping plot. From the first plot i get that you want to attach a color to a value of the curve ... but how does that project to your circle in the third picture ? ... and where do you calculate a gradient ?
– Hoki
Nov 7 at 10:56