我正在尝试通过 Matlab 创建一个四连游戏。
我的问题是我要么无法在每个网格方块中绘制图形,要么根本无法绘制“圆形”图形。
function [] = Kinect4(nrRows, nrCols)
board = zeros(nrRows, nrCols);
nrMoves = 0;
set(gca, 'xlim', [0 nrCols]);
set(gca, 'ylim', [0 nrRows]);
for r = 1 : 1 : nrRows - 1
line([0, nrCols], [r, r], ...
'LineWidth', 4, 'Color', [0 0 1]);
end
for c = 1 : 1 : nrCols - 1
line([c, c], [0, nrRows], ...
'LineWidth', 4, 'Color', [0 0 1]);
end
DrawBoard(nrRows, nrCols)
hold on;
while nrMoves < nrRows * nrCols %Computes ability to move polygon
[x, y] = ginput(1);
r = ceil(y); % convert to row index
c = ceil(x); % convert to column index
angles = 0 : 1 : 360;
x = cx + r .* cosd(angles);
y = cy + r .* sind(angles);
plot(x, y, 'Color', [1 1 1], 'LineWidth', 3);
axis square;
end
end
以下是对代码的一些修复。
DrawBoard(nrRows, nrCols)
。不确定您是否将其作为注释放在那里,因为您已经绘制了板子,或者 DrawBoard
是否是一个单独的函数。r
和 c
的计算,以给出您想要放置钉子的单元格的中心。这是通过从每个值中减去 0.5 来完成的。 x = cx + r .* cosd(angles);
更改为 x = c + 0.5*cosd(angles);
。在上一篇中,变量 cx
未定义,而不是 r
是钉子的半径,我使用 0.5
您可以将其替换为适当的变量。但想法是画一个半径为 0.5
的圆(以便它适合单元格),中心沿 x 轴偏移 c
。对 y
进行类似的更改以沿 y 轴偏移钉。 plot
命令中的颜色更改为[0 0 0]
,即黑色。 [1 1 1]
是白色的,在白色背景上无法看到:)。我建议使用 'k'
表示黑色,'b'
表示蓝色等。有关基本颜色规范,请参阅 matlab 文档。这是一个“工作”代码:
function [] = Kinect4(nrRows, nrCols)
board = zeros(nrRows, nrCols);
nrMoves = 0;
set(gca, 'xlim', [0 nrCols]);
set(gca, 'ylim', [0 nrRows]);
for r = 1 : 1 : nrRows - 1
line([0, nrCols], [r, r], ...
'LineWidth', 4, 'Color', [0 0 1]);
end
for c = 1 : 1 : nrCols - 1
line([c, c], [0, nrRows], ...
'LineWidth', 4, 'Color', [0 0 1]);
end
axis square;
hold on;
while nrMoves < nrRows * nrCols %Computes ability to move polygon
[x, y] = ginput(1);
r = ceil(y) - 0.5;
c = ceil(x) - 0.5;
angles = 0 : 1 : 360;
x = c + 0.5*cosd(angles);
y = r + 0.5*sind(angles);
plot(x, y, 'Color', [0 0 0], 'LineWidth', 3);
end
end