struct Point
{
Point()
{
posx = 0;
posy = 0;
}
Point(int x, int y)
{
posx = x;
posy = y;
}
int posx;
int posy;
};

 

void bresenham(int x1, int y1, int x2, int y2, vector<Point>& path)
{
path.clear();
int dx = abs(x2 - x1);
int dy = abs(y2 - y1);

int inc_x = (x2 > x1) ? 1 : -1;
int inc_y = (y2 > y1) ? 1 : -1;

int eps = 0;

Point pos(x1, y1);
path.push_back(pos);

int x = x1;
int y = y1;
if(dx > dy)
{
for(x = x1; x != x2; x += inc_x)
eps += dy;
if((eps << 1) >= dx)
{
y += inc_y;
eps -= dx;
}
pos.posx = x;
pos.posy = y;
path.push_back(pos);
}
}
else
{
for(y = y1; y != y2; y+= inc_y)
{
eps += dx;
if((eps << 1) >= dy)
{
x += inc_x;
eps -= dy;
}
pos.posx = x;
pos.posy = y;
path.push_back(pos);
}
}

pos.posx = x;
pos.posy = y;

path.push_back(pos);
}

posted on 2017-02-27 19:15  c碰  阅读(626)  评论(0编辑  收藏  举报