【发布时间】:2014-01-10 22:30:18
【问题描述】:
我的任务是为大学项目制作机器人控制器。目前它进展顺利,但我有一个烦人的小错误,我似乎无法纠正它。
基本上,我必须设计一个对比控制器来实现随机移动,同时避开障碍物。所以,我有一个机器人,它在控制台上显示为“R”,位于 10 x 10 区域内。这是我用来初始化我的 2D 向量,然后绘制网格的代码:
void matrix::init() // init my 2D vector
{
dot = 10; // 10 by 10 area
vector2D.resize(dot);
for (int i=0; i<dot; i++)
{
vector2D[i].resize(dot);
}
}
void matrix::draw() // drawing the vector to the screen
{
for(int i=0; i<dot; i++)
{
for(int j=0; j<dot; j++)
{
cout <<vector2D[i][j]<<"."; // I being the Y access, J the X access
}
cout<<endl;
}
}
void matrix::update()
{
init();
draw();
}
这是在它自己的名为matrix.cpp 的类中,然后在main.cpp 中调用它,m.update(); m 是matrix 的对象
现在,正在使用matrix.cpp 类中的此代码设置屏幕上的机器人位置
void matrix::robotPosition(int x, int y)
{
bot = 'R';
cout << "X Pos"<< x <<endl;
cout << "Y Pos"<< y <<endl;
vector2D[x][y] = bot; // Outputting location of robot onto the grid / matrix
}
我已经开发了更多代码来控制屏幕上的位置,但我认为在我的问题的这一点上不需要。
int main()
{
matrix m;
robot r;
while(true)
{
m.update(); // vector2D init and draw
m.robotPosition(r.getX(), r.getY());
r.update();
system("pause");
}
}
每次我的程序循环通过 while 循环时,它都会在屏幕上绘制另一个机器人,但似乎并没有删除旧机器人。该代码通过使用char 'R'(这是我的机器人)在二维向量中分配某个X 和Y 来工作。我的想法是否正确,我必须在每个运动周期后绘制 2D 矩阵?
谢谢
【问题讨论】:
标签: c++ class robotics 2d-vector