How to draw Domino tiles in a figure?

2 次查看(过去 30 天)
Hi everyone,
I'm here to ask you if anyone knows, how to draw a bunch of domino tiles in a figure. I already generated a figure with the pixels black, but know I want to draw domino tiles into this figure.
And basically I want a figure something like this: (the color os the domino tiles can be just one).
Thanks,
Nuno

回答(1 个)

Animesh
Animesh 2024-5-17
Hi Nuno,
You can use this code to create a random set of dominos in matlab -
% Define the size of the domino tile
tileWidth = 50;
tileHeight = 100;
% Create a figure
figure;
% Define the dot positions for a real domino
dotPositions = {[], [0.5 0.25], [0.3 0.35; 0.7 0.15], [0.3 0.35; 0.5 0.25; 0.7 0.15], ...
[0.3 0.15; 0.3 0.35; 0.7 0.15; 0.7 0.35], ...
[0.3 0.15; 0.3 0.35; 0.5 0.25; 0.7 0.15; 0.7 0.35], ...
[0.3 0.15; 0.3 0.25; 0.3 0.35; 0.7 0.15; 0.7 0.25; 0.7 0.35]};
% Generate a random sequence of dominoes
numTiles = 10;
randomTilesTop = randi([0 6], numTiles, 1);
randomTilesBottom = randi([0 6], numTiles, 1);
% Loop over the number of tiles
for i = 1:numTiles
% Create a rectangle for the tile
rectangle('Position', [i*tileWidth 0 tileWidth tileHeight], 'FaceColor', 'w', 'EdgeColor', 'k');
% Draw a line in the middle of the tile
line([i*tileWidth i*tileWidth+tileWidth], [tileHeight/2 tileHeight/2], 'Color', 'k');
% Draw dots on the top half of the tile
for j = 1:size(dotPositions{randomTilesTop(i)+1}, 1)
% Calculate the position of the dot
x = i*tileWidth + dotPositions{randomTilesTop(i)+1}(j, 1)*tileWidth;
y = dotPositions{randomTilesTop(i)+1}(j, 2)*tileHeight;
% Draw the dot
rectangle('Position', [x-2 y-2 4 4], 'Curvature', [1 1], 'FaceColor', 'k');
end
% Draw dots on the bottom half of the tile
for j = 1:size(dotPositions{randomTilesBottom(i)+1}, 1)
% Calculate the position of the dot
x = i*tileWidth + dotPositions{randomTilesBottom(i)+1}(j, 1)*tileWidth;
y = tileHeight/2 + dotPositions{randomTilesBottom(i)+1}(j, 2)*tileHeight;
% Draw the dot
rectangle('Position', [x-2 y-2 4 4], 'Curvature', [1 1], 'FaceColor', 'k');
end
end
% Adjust the axis
axis equal;
axis off;

Community Treasure Hunt

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

Start Hunting!

Translated by