主要是在原来算法的基础上略微修改,增加了对首尾端点的处理:
#include "opencv.hpp"
// 交换整数 a 、b 的值
inline void swap_int(int*a, int *b)
{
*a ^= *b; *b ^= *a; *a ^= *b;
}
// Bresenham's line algorithm
bool GetLinePts(int x1, int y1, int x2, int y2, std::vector<cv::Point>& vPts)
{
//参数 c 为颜色值
//增加第一个端点
cv::Point ptStart(x1, y1);
cv::Point ptEnd(x2, y2);
vPts.push_back(ptStart);
int dx = abs(x2 - x1), dy = abs(y2 - y1), yy = 0;
if (dx < dy)
{
yy = 1;
swap_int(&x1, &y1);
swap_int(&x2, &y2);
swap_int(&dx, &dy);
}
int ix = (x2 - x1) > 0 ? 1 : -1, iy = (y2 - y1) > 0 ? 1 : -1, cx = x1, cy = y1, n2dy = dy * 2, n2dydx = (dy - dx) * 2, d = dy * 2 - dx;
if (yy) { // 如果直线与 x 轴的夹角大于 45 度
while (cx != x2)
{
if (d < 0)
{
d += n2dy;
}
else
{
cy += iy;
d += n2dydx;
}
vPts.push_back(cv::Point(cy, cx));
cx += ix;
}
}
else
{
// 如果直线与 x 轴的夹角小于 45 度
while (cx != x2)
{
if (d < 0)
{
d += n2dy;
}
else
{
cy += iy;
d += n2dydx;
}
vPts.push_back(cv::Point(cx, cy));
cx += ix;
}
}
//需要包含首尾端点,进行处理
if (vPts.size() >= 2 && vPts[0] == vPts[1])
{
vPts.erase(vPts.begin());
}
if (vPts.size() && vPts[vPts.size() - 1] != ptEnd)
{
vPts.push_back(ptEnd);
}
return true;
}