Tej
Tej

Reputation: 13

Matlab's patch function with interpolation produces different result based on order of vertices

I have a patch with x,y,c coordinates as given below. Upon changing the order of points, MATLAB's patch color interpolation changes.

x = [0.9000    1.0000    1.0000  0.9000]';
y = [0.5000    0.5000    0.6000  0.6000]';
c = [0.0278    0.0371    0.0325    0.0282]';

figure();
patch(x,y,c);

order = [2:4,1];
figure();
patch(x(order),y(order),c(order));

The above produces two different color patches. However, the coordinates and the color have only changed the sequence in cyclic manner. Any suggestions to overcome this?

Upvotes: 1

Views: 183

Answers (1)

MrAzzaman
MrAzzaman

Reputation: 4768

I think that this is because you're not closing your patch -- if the last point in your patch is not the same as the first point, MATLAB automatically closes it. Apparently this does something strange with the color interpolation. If you modify the code slightly so that your patch is closed, like so:

figure;
order = [1:4,1];
patch(x(order),y(order),c(order));
figure;
order = [2:4,1:2];
patch(x(order),y(order),c(order));
figure;
order = [3:4,1:3];
patch(x(order),y(order),c(order));

Then you get the same patch every time.

Upvotes: 4

Related Questions