A MATLAB Dice Function to display the face of the dice
4 views (last 30 days)
Show older comments
Please help me with this question, it is related to plotting :
Write a MATLAB function called ‘dice’. The function should open a figure and display the given face of a regular six-sided die. The face ‘number’ is the only input argument, k. Examples of the desired output are shown in Figure. Notice the rounded corners. The orientation of faces 2, 3 and 6 does not matter as long as the white dots form the desired pattern.
2 Comments
Steven Lord
on 6 Mar 2021
This sounds like a homework assignment. If it is, show us the code you've written to try to solve the problem and ask a specific question about where you're having difficulty and we may be able to provide some guidance.
If you aren't sure where to start because you're not familiar with how to write MATLAB code, I suggest you start with the MATLAB Onramp tutorial (https://www.mathworks.com/support/learn-with-matlab-tutorials.html) to quickly learn the essentials of MATLAB.
If you aren't sure where to start because you're not familiar with the mathematics you'll need to solve the problem, I recommend asking your professor and/or teaching assistant for help.
Accepted Answer
Anand Soni
on 6 Mar 2021
3 Comments
Steven Lord
on 7 Mar 2021
Rather than calling scatter multiple times, each creating an individual pip, you can pass vectors of coordinate data into scatter.
x = 1.75:0.25:2.25;
y = flip(2:0.5:3);
[xx, yy] = meshgrid(x, y)
Now you just need to know which pips should be active for each die and you can index into xx and yy to create the scatter plot. I'm using black-filled pips because I'm not plotting it on a black background.
six = [1:3 7:9];
h = scatter(xx(six), yy(six), 'filled', 'k');
axis([1.5 2.5 1.5 3.5])
One benefit of this is that you only have one graphics handle, not six. This won't matter if you're only showing one die, but could if you're showing a lot of them.
size(h)
It also means that if you want to put the die in a different place, all you need to do is translate xx and yy to the new coordinate.
cla
hold on
scatter(xx(six), yy(six), 'filled', 'k'); % Original die
scatter(xx(six)+1, yy(six)-1, 'filled', 'r'); % Another one below and to the right
axis([1.5 4.5 0.5 3.5]) % Note different axes limits
More Answers (1)
Image Analyst
on 6 Mar 2021
Try rectangle() to make the rounded rectangle, and plot() or scatter(x,y,'filled') to make the spots, though you could use rectangle() to make the circular spots also.
See Also
Categories
Find more on Graphics Object Properties in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!