A MATLAB Dice Function to display the face of the dice

조회 수: 7 (최근 30일)
Anand Soni
Anand Soni 2021년 3월 6일
댓글: Steven Lord 2021년 3월 7일
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
Steven Lord
Steven Lord 2021년 3월 6일
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.
Anand Soni
Anand Soni 2021년 3월 6일
I have done some work on it, but the problems I face
  1. I can't make the rectangle black and plot white dots on this
  2. I can't remove the axis, I just need a dice
  3. I would like to make bigger dots on the dice
It would be highly appreciated if you could help me with my Homework. Thanks
The Output is
The Code is
v=randi(6);
hold off
switch v
case 1
plot(2,2,'o')
hold on
case 2
plot(1.5,3,'o')
hold on
plot(2.5,1,'o')
hold on
case 3
plot(1.5,1,'o')
hold on
plot(2,2,'o')
plot(2.5,3,'o')
hold on
case 4
plot(1.5,1,'o')
hold on
plot(1.5,3,'o')
plot(2.5,1,'o')
plot(2.5,3,'o')
hold on
case 5
plot(1.5,1,'o')
hold on
plot(1.5,3,'o')
plot(2.5,1,'o')
plot(2.5,3,'o')
plot(2,2,'o')
case 6
plot(1.5,1,'o')
hold on
plot(1.5,3,'o')
plot(2.5,1,'o')
plot(2.5,3,'o')
plot(1.5,2,'o')
plot(2.5,2,'o')
hold on
end
set(get(gca,'children'),'MarkerFaceColor','k') %fill the dots
axis([0 4 0 4])
hold on
rectangle('Position',[1 0.5 2 3],'Curvature',0.2)
axis([0 5 0 5])

댓글을 달려면 로그인하십시오.

채택된 답변

Anand Soni
Anand Soni 2021년 3월 6일
From the suggestion by @Image Analyst, I used Scatter and it worked.
The Output:
The Code is :
hold on
rectangle('Position',[1.5 1.25 1 1.5],'Curvature',0.2, "FaceColor",'k')
axis([0 5 0 5])
v=randi(6);
switch v
case 1
hold on
scatter(2, 2, 'filled', 'w')
case 2
scatter(1.75, 2.5, 'filled', 'w')
hold on
scatter(2.25, 1.5, 'filled', 'w')
hold on
case 3
scatter(1.75, 1.5, 'filled', 'w')
hold on
scatter(2, 2, 'filled', 'w')
scatter(2.25, 2.5, 'filled', 'w')
hold on
case 4
scatter(1.75, 1.5, 'filled', 'w')
hold on
scatter(1.75, 2.5, 'filled', 'w')
scatter(2.25, 1.5, 'filled', 'w')
scatter(2.25, 2.5, 'filled', 'w')
hold on
case 5
scatter(1.75, 1.5, 'filled', 'w')
hold on
scatter(1.75, 2.5, 'filled', 'w')
scatter(2.25, 1.5, 'filled', 'w')
scatter(2.25, 2.5, 'filled', 'w')
scatter(2, 2, 'filled', 'w')
case 6
scatter(1.75, 1.5, 'filled', 'w')
hold on
scatter(1.75, 2.5, 'filled', 'w')
scatter(2.25, 1.5, 'filled', 'w')
scatter(2.25, 2.5, 'filled', 'w')
scatter(1.75, 2, 'filled', 'w')
scatter(2.25, 2, 'filled', 'w')
hold on
end
axis([0 4 0 4])
  댓글 수: 3
Steven Lord
Steven Lord 2021년 3월 7일
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)
xx = 3×3
1.7500 2.0000 2.2500 1.7500 2.0000 2.2500 1.7500 2.0000 2.2500
yy = 3×3
3.0000 3.0000 3.0000 2.5000 2.5000 2.5000 2.0000 2.0000 2.0000
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)
ans = 1×2
1 1
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

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Image Analyst
Image Analyst 2021년 3월 6일
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.

카테고리

Help CenterFile Exchange에서 Graphics Object Properties에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by