| 302 | }*/ |
| 303 | |
| 304 | void Bitmap::drawLine(int x1, int y1, int x2, int y2, u8 color) |
| 305 | { |
| 306 | // If the line doesn't intersect this rect |
| 307 | if ( std::max(x1,x2) < 0 || |
| 308 | std::max(y1,y2) < 0 || |
| 309 | std::min(x1,x2) >= this->width() || |
| 310 | std::min(y1,y2) >= this->height() ) |
| 311 | return; |
| 312 | |
| 313 | // If the line is completely vertical or horizontal |
| 314 | if ( x1 == x2 ) |
| 315 | { |
| 316 | int ymin = Util::clamp(std::min(y1,y2), 0, this->height()-1); |
| 317 | int ymax = Util::clamp(std::max(y1,y2), 0, this->height()-1); |
| 318 | |
| 319 | // Plot vertical line |
| 320 | for ( int i = ymin; i < ymax; ++i ) |
| 321 | this->plotX(x1, i, color); |
| 322 | return; |
| 323 | } |
| 324 | else if ( y1 == y2 ) |
| 325 | { |
| 326 | int xmin = Util::clamp(std::min(x1,x2), 0, this->width()-1); |
| 327 | int xmax = Util::clamp(std::max(x1,x2), 0, this->width()-1); |
| 328 | |
| 329 | // Plot horizontal line |
| 330 | memset(&this->data[y1 * this->width() + xmin], color, xmax - xmin ); |
| 331 | return; |
| 332 | } |
| 333 | |
| 334 | // If the line does intersect but needs to be clipped |
| 335 | //if ( x1 < 0 || y1 < 0 || x2 >= this->width() || y2 >= this->height() ) |
| 336 | //this->clipping(x1,y1,x2,y2); |
| 337 | |
| 338 | // Line algorithm source: http://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm#Simplification |
| 339 | int dx = std::abs(x2 - x1), dy = std::abs(y2 - y1); |
| 340 | int sx = x1 < x2 ? 1 : -1, sy = y1 < y2 ? 1 : -1; |
| 341 | int err = dx - dy; |
| 342 | |
| 343 | // @TODO: optimize |
| 344 | while ( this->plot(x1, y1, color), x1 != x2 || y1 != y2 ) |
| 345 | { |
| 346 | int e2 = 2*err; |
| 347 | if ( e2 > -dy ) |
| 348 | { |
| 349 | err -= dy; |
| 350 | x1 += sx; |
| 351 | } |
| 352 | if ( e2 < dx ) |
| 353 | { |
| 354 | err += dx; |
| 355 | y1 += sy; |
| 356 | } |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | void Bitmap::clear() |
| 361 | { |
no test coverage detected