| 402 | |
| 403 | template <typename LightTraits> |
| 404 | float CellularLightArray<LightTraits>::lineAttenuation(Vec2F const& start, Vec2F const& end, |
| 405 | float perObstacleAttenuation, float maxAttenuation) { |
| 406 | // Run Xiaolin Wu's line algorithm from start to end, summing over colliding |
| 407 | // blocks using perObstacleAttenuation. |
| 408 | float obstacleAttenuation = 0.0; |
| 409 | |
| 410 | // Apply correction because integer coordinates are lower left corner. |
| 411 | float x1 = start[0] - 0.5; |
| 412 | float y1 = start[1] - 0.5; |
| 413 | float x2 = end[0] - 0.5; |
| 414 | float y2 = end[1] - 0.5; |
| 415 | |
| 416 | float dx = x2 - x1; |
| 417 | float dy = y2 - y1; |
| 418 | |
| 419 | if (fabs(dx) < fabs(dy)) { |
| 420 | if (y2 < y1) { |
| 421 | swap(y1, y2); |
| 422 | swap(x1, x2); |
| 423 | } |
| 424 | |
| 425 | float gradient = dx / dy; |
| 426 | |
| 427 | // first end point |
| 428 | float yend = round(y1); |
| 429 | float xend = x1 + gradient * (yend - y1); |
| 430 | float ygap = rfpart(y1 + 0.5); |
| 431 | int ypxl1 = yend; |
| 432 | int xpxl1 = ipart(xend); |
| 433 | |
| 434 | if (cell(xpxl1, ypxl1).obstacle) |
| 435 | obstacleAttenuation += rfpart(xend) * ygap * perObstacleAttenuation; |
| 436 | |
| 437 | if (cell(xpxl1 + 1, ypxl1).obstacle) |
| 438 | obstacleAttenuation += fpart(xend) * ygap * perObstacleAttenuation; |
| 439 | |
| 440 | if (obstacleAttenuation >= maxAttenuation) |
| 441 | return maxAttenuation; |
| 442 | |
| 443 | float interx = xend + gradient; |
| 444 | |
| 445 | // second end point |
| 446 | yend = round(y2); |
| 447 | xend = x2 + gradient * (yend - y2); |
| 448 | ygap = fpart(y2 + 0.5); |
| 449 | int ypxl2 = yend; |
| 450 | int xpxl2 = ipart(xend); |
| 451 | |
| 452 | if (cell(xpxl2, ypxl2).obstacle) |
| 453 | obstacleAttenuation += rfpart(xend) * ygap * perObstacleAttenuation; |
| 454 | |
| 455 | if (cell(xpxl2 + 1, ypxl2).obstacle) |
| 456 | obstacleAttenuation += fpart(xend) * ygap * perObstacleAttenuation; |
| 457 | |
| 458 | if (obstacleAttenuation >= maxAttenuation) |
| 459 | return maxAttenuation; |
| 460 | |
| 461 | for (int y = ypxl1 + 1; y < ypxl2; ++y) { |