2020-02-17 23:21:07 -08:00
|
|
|
void Line( float x1, float y1, float x2, float y2, const Color& color )
|
2013-04-10 16:57:12 -07:00
|
|
|
{
|
|
|
|
|
// Bresenham's line algorithm
|
2014-01-17 05:32:22 +00:00
|
|
|
const bool steep = (fabs(y2 - y1) > fabs(x2 - x1));
|
|
|
|
|
if(steep)
|
|
|
|
|
{
|
|
|
|
|
std::swap(x1, y1);
|
|
|
|
|
std::swap(x2, y2);
|
|
|
|
|
}
|
2013-04-10 16:57:12 -07:00
|
|
|
|
2014-01-17 05:32:22 +00:00
|
|
|
if(x1 > x2)
|
|
|
|
|
{
|
|
|
|
|
std::swap(x1, x2);
|
|
|
|
|
std::swap(y1, y2);
|
|
|
|
|
}
|
2013-04-10 16:57:12 -07:00
|
|
|
|
2014-01-17 05:32:22 +00:00
|
|
|
const float dx = x2 - x1;
|
|
|
|
|
const float dy = fabs(y2 - y1);
|
2013-04-10 16:57:12 -07:00
|
|
|
|
2014-01-17 05:32:22 +00:00
|
|
|
float error = dx / 2.0f;
|
|
|
|
|
const int ystep = (y1 < y2) ? 1 : -1;
|
|
|
|
|
int y = (int)y1;
|
2013-04-10 16:57:12 -07:00
|
|
|
|
2014-01-17 05:32:22 +00:00
|
|
|
const int maxX = (int)x2;
|
|
|
|
|
|
2020-02-17 23:21:07 -08:00
|
|
|
for(int x=(int)x1; x<=maxX; x++)
|
2014-01-17 05:32:22 +00:00
|
|
|
{
|
|
|
|
|
if(steep)
|
2015-02-20 00:35:01 -05:00
|
|
|
{
|
|
|
|
|
SetPixel(y,x, color);
|
|
|
|
|
}
|
2014-01-17 05:32:22 +00:00
|
|
|
else
|
2015-02-20 00:35:01 -05:00
|
|
|
{
|
|
|
|
|
SetPixel(x,y, color);
|
|
|
|
|
}
|
2013-04-10 16:57:12 -07:00
|
|
|
|
2015-02-20 00:35:01 -05:00
|
|
|
error -= dy;
|
|
|
|
|
if(error < 0)
|
|
|
|
|
{
|
|
|
|
|
y += ystep;
|
|
|
|
|
error += dx;
|
|
|
|
|
}
|
2014-01-17 05:32:22 +00:00
|
|
|
}
|
2013-04-10 16:57:12 -07:00
|
|
|
}
|