3 #include "pointstobitmap.h"
8 * NOTE: I am using vector<pair<T,T> > because it is convenient; don't have to round to pixels, can print to stdout and gnuplot, etc
9 * vector<pair<T,T> > should obviously not be used in a practical implementation; directly access the pixel buffer
12 template <class T> vector<pair<T, T> > LineDDA(const T & x1, const T & y1, const T & x2, const T & y2)
16 uint64_t steps = lround(max(abs(dx), abs(dy)));
17 T xInc = T(dx)/T(steps);
18 T yInc = T(dy)/T(steps);
20 vector<pair<T,T> > result(steps);
21 for (uint64_t k = 0; k <= steps; ++k)
23 result[k] = pair<T,T>(round(x), round(y));
30 * Only works for 0 < m < 1
32 template <class T> vector<pair<T, T> > LineBresenham(const T & x1, const T & y1, const T & x2, const T & y2)
38 T two_dxdy = 2*(dy-dx);
53 vector<pair<T,T> > result;
64 result.emplace_back(pair<T,T>(x,y));
69 int main(int argc, char ** argv)
71 auto f = LineDDA<float>(0,0,400,200);
72 PointsToBitmap<float>(f, 1, 0,0,0,"lines.bmp");
73 auto i = LineBresenham<int>(0,100,400,300);
74 i.push_back(pair<int,int>(0,0));
75 PointsToBitmap<int>(i, 1, 255,0,0,"lines.bmp", true);