14std::optional<float>
firstTerrainImpact(
float fromX,
float fromY,
float fromHeight,
float toX,
float toY,
15 float toHeight,
float originX,
float originY,
float cellSize,
16 const HeightAt& heightAt) {
17 const double x = (
static_cast<double>(fromX) - originX) / cellSize;
18 const double y = (
static_cast<double>(fromY) - originY) / cellSize;
19 const double dx = (
static_cast<double>(toX) - fromX) / cellSize;
20 const double dy = (
static_cast<double>(toY) - fromY) / cellSize;
21 int cellX =
static_cast<int>(std::floor(
x));
22 int cellY =
static_cast<int>(std::floor(
y));
23 const int stepX =
dx > 0.0 ? 1 : -1;
24 const int stepY =
dy > 0.0 ? 1 : -1;
25 const double infinity = std::numeric_limits<double>::infinity();
26 const double deltaX =
dx == 0.0 ? infinity : 1.0 / std::abs(
dx);
27 const double deltaY =
dy == 0.0 ? infinity : 1.0 / std::abs(
dy);
28 double nextX =
dx == 0.0 ? infinity : (cellX + (
dx > 0.0 ? 1.0 : 0.0) -
x) /
dx;
29 double nextY =
dy == 0.0 ? infinity : (cellY + (
dy > 0.0 ? 1.0 : 0.0) -
y) /
dy;
30 const double heightDelta =
static_cast<double>(toHeight) - fromHeight;
33 const double leave = std::min({nextX, nextY, 1.0});
35 const double terrain = heightAt(cellX, cellY);
36 const double entryHeight = fromHeight + heightDelta * enter;
37 if (
terrain >= entryHeight)
return static_cast<float>(enter);
38 if (heightDelta < 0.0) {
39 const double crossing = (
terrain - fromHeight) / heightDelta;
40 if (crossing >= enter && crossing < leave)
return static_cast<float>(crossing);
43 if (leave >= 1.0)
break;