Patch function gives crosses when multiple squares attempted

1 view (last 30 days)
Trying to place four squares with patch gradients, code works when only doing one but when trying with multiple it produces X with left and right triangles. Coord order same as when works for single
shapesx=[0 5 5 0;0 5 5 0;5 10.5 10.5 5;5 10.5 10.5 5];
shapesy=[0 0 5 5;5 5 10.5 10.5;0 0 5 5;5 5 10.5 10.5];
shapesc=[0 2.5 5 2.5;2.5 5 7.5 5;2.5 5 7.5 5; 5 7.5 10 7.5];
patch(shapesx,shapesy,shapesc)

Answers (1)

Mike Garrity
Mike Garrity on 30 Nov 2015
Yeah, this can be a bit confusing.
If you look at the help for the XYC form of patch, it says:
one polygon ("face") per column is added
So in this case:
shapesx=[0 5 5 0];
shapesy=[0 0 5 5];
shapesc=[0 2.5 5 2.5];
patch(shapesx,shapesy,shapesc)
We have 4 columns which each have 1 row. So in theory, we should get 4 1-sided polygons. But this is a special case. These 1x4 arrays are called "row vectors". If you give patch (and many other graphics commands) a row vector when it expects a "column vector", then it will transpose for you. So it's just as if you had done this:
patch(shapesx',shapesy',shapesc')
But in your 4x4 case, this special case doesn't apply. So in that case you do need to worry about the fact that each column is going to become a polygon, rather than each row.
So I think what you really want is this:
shapesx=[0 5 5 0;0 5 5 0;5 10.5 10.5 5;5 10.5 10.5 5];
shapesy=[0 0 5 5;5 5 10.5 10.5;0 0 5 5;5 5 10.5 10.5];
shapesc=[0 2.5 5 2.5;2.5 5 7.5 5;2.5 5 7.5 5; 5 7.5 10 7.5];
patch(shapesx',shapesy',shapesc')
or this:
shapesx=[0 0 5 5 ; ...
5 5 10.5 10.5; ...
5 5 10.5 10.5; ...
0 0 5 5 ];
shapesy=[0 5 0 5 ; ...
0 5 0 5 ; ...
5 10.5 5 10.5; ...
5 10.5 5 10.5];
shapesc=[0 2.5 2.5 5 ; ...
2.5 5 5 7.5; ...
5 7.5 7.5 10 ; ...
2.5 5 5 7.5];
patch(shapesx,shapesy,shapesc)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!