/* -*- C++ -*- * Copyright ©2005 Hugo Mills * * This software is distributed under the terms of the GNU GPL v3 * For more information on the GPL, see the file COPYING or * visit http://www.gnu.org/ * * This software is distributed without warranty */ #include "magellan/output.h" void Output::drawline(const ivec2& from, const ivec2& to, int r, int g, int b) { int STATE = 0; ivec2 d = std::valarray(to-from); // Eurgh. Hacky. ivec2 p0; p0[0] = p0[1] = 0; ivec2 p1 = d; // Normalise to first octant // Flip in y axis if(d[0] < 0) { STATE |= 0x1; d[0] = -d[0]; p1[0] = -p1[0]; } // Flip in x axis if(d[1] < 0) { STATE |= 0x2; d[1] = -d[1]; p1[1] = -p1[1]; } // Flip in x=y if(d[0] < d[1]) { STATE |= 0x4; std::swap(d[0], d[1]); std::swap(p0[0], p0[1]); std::swap(p1[0], p1[1]); } // Now run Bresenham's algorithm int err = 0; ivec2 pos; for(; p0[0] d[0]) { p0[1]++; err -= d[0]; } // Transform coords back to something sensible to plot switch(STATE) { case 0: pos[0] = p0[0]; pos[1] = p0[1]; break; case 1: pos[0] = -p0[0]; pos[1] = p0[1]; break; case 2: pos[0] = p0[0]; pos[1] = -p0[1]; break; case 3: pos[0] = -p0[0]; pos[1] = -p0[1]; break; case 4: pos[0] = p0[1]; pos[1] = p0[0]; break; case 5: pos[0] = -p0[1]; pos[1] = p0[0]; break; case 6: pos[0] = p0[1]; pos[1] = -p0[0]; break; case 7: pos[0] = -p0[1]; pos[1] = -p0[0]; break; } pos += from; setpixel(pos, r, g, b); } }