好的,现在我正在尝试通过Matlab编码创建一个连接四游戏;现在游戏仍然是婴儿,但我的问题是,我要么无法在每个网格中绘制图形,要么我无法得到“圆圈”图形。请以任何可能的方式提供帮助。此外,如果有人知道任何连接四个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
答案 0 :(得分:2)
以下是对代码的一些修复。
DrawBoard(nrRows, nrCols)
。由于您已经绘制了电路板或者DrawBoard
是一个单独的功能,因此不确定您是否将其作为注释放在那里。r
和c
的计算,以便为要放入引线的单元格的中心提供。这是通过从每个中减去0.5来完成的。 x = cx + r .* cosd(angles);
更改为x = c + 0.5*cosd(angles);
。在上一个中,变量cx
未定义,而r
不是peg的半径,我使用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