Skip to content

Instantly share code, notes, and snippets.

@sid24ss
Created September 21, 2014 20:12
Show Gist options
  • Save sid24ss/9bfc5d35beeb673c8fb6 to your computer and use it in GitHub Desktop.
Save sid24ss/9bfc5d35beeb673c8fb6 to your computer and use it in GitHub Desktop.
Bresenham line points
inline std::vector< std::pair<int,int> > getBresenhamLinePoints(int x1, int y1, int x2, int y2)
{
std::vector <std::pair<int, int> > out_points;
bool steep = (std::abs(y2 - y1) > std::abs(x2 - x1));
if(steep){
std::swap(x1, y1);
std::swap(x2, y2);
}
if(x1 > x2){
std::swap(x1, x2);
std::swap(y1, y2);
}
int dx = x2 - x1;
int dy = std::abs(y2 - y1);
double err = dx / 2.0f;
int ystep = (y1 < y2)? 1 : -1;
int y = (y1);
for (int x = x1; x <= x2; ++x) {
if(steep){
// y,x
out_points.push_back(std::make_pair(y,x));
} else {
// x, y
out_points.push_back(std::make_pair(x, y));
}
err = err - dy;
if(err < 0){
y = y + ystep;
err = err + dx;
}
}
return out_points;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment