Trying to plot a checkerboard on a graph using rectangles and for loop.
3 views (last 30 days)
Show older comments
So for a 'background' of a graph I am working on must be in the style of a checkerboard, I have worked out how to do it for two squares but not sure what values to change to get it to the right size for the axis.
for k=1:2
a=0;
if(k==1)
rectangle('Position',[a 0 10 10], 'FaceColor' , 'white');
else
rectangle('Position',[a+10 0 10 10], 'FaceColor' , 'black');
end
end
The axis is -
axis([0,160,-60, 60]);
Thank you in advance
0 Comments
Answers (2)
Kelly Kearney
on 1 Oct 2014
Edited: Kelly Kearney
on 1 Oct 2014
By using the rectangle command, you're going to end up with a lot of patch objects, which will work, but the patch command allows you to create multiple polygons with a single call, and is usually my preference when creating lots of shapes with the same number of vertices.
% Lower left corner of each box
x = 0:10:150;
y = -60:10:50;
[x,y] = meshgrid(x,y);
% Checkerboard values
if mod(size(x,1),2)
val = ones(size(x));
v(1:2:end) = 2;
else
val = ones(size(x,1)+1,size(x,2));
val(1:2:end) = 2;
val = val(1:end-1,:);
end
% Coordinates of a box, relative to lower left corner
xref = [0 0 10 10 0];
yref = [0 10 10 0 0];
% Add to get coordinates of each box
xbox = bsxfun(@plus, x(:), xref);
ybox = bsxfun(@plus, y(:), yref);
% Plot using patch with RGB cdata
cmap = [0 0 0; 1 1 1];
col = permute(cmap(val(:), :), [3 1 2]);
h = patch(xbox', ybox', col);
0 Comments
Geoff Hayes
on 1 Oct 2014
Lex - your code creates two neighbouring squares, each of size 10x10. Suppose you just wanted to draw one full row given your x-axis dimension which is from -60 to 60. You know that each square is of size ten, so you can just move from 0 to 160 in steps of 10, drawing the square in the correct colour
figure;
clr = [1 1 1]; % initial colour is 1 which will correspond to white
for x=0:10:150 % we don't end at 160 because then we would draw a rectangle beyond our axis
rectangle('Position',[x 0 10 10], 'FaceColor', clr);
if x~=150
clr = ~clr;
end
end
axis([0,160,-60, 60]);
The above just takes exactly what you did and extends it for one complete row in your figure. Note how we make use of the fact that the checkerboard has white or black tiles. Rather than specifying 'white' or 'black' as the FaceColor, we can use an RGB vector (with values between 0 and 1) to do this instead: [0 0 0] corresponds to black, and [1 1 1] corresponds to white. On each iteration of the loop we just invert the clr vector to switch from white to black. And we only do that switching for every iteration except the last one...which will become important as we move to the next line whose initial colour should be the same colour as the last colour on the previous line.
So if one loop allows us to create a row, we would need a second loop to do the same for multiple rows along the y-axis. Using the above as a guide, how would you modify it to draw multiple rows?
0 Comments
See Also
Categories
Find more on 2-D and 3-D Plots in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!