Skip to content

Commit

Permalink
Update scaleLinear to better handle wrap around cases
Browse files Browse the repository at this point in the history
In particular, when there is a negative slope near wrap around values, we handle that case better. For example, using `uint32_t` for the input domain, and `uint8_t` for the output range, with the full spectrum of `uint8_t` values used for the output, there can be wrap around to the promoted type of `uint32_t`, which caused problems with the previous calculation.
  • Loading branch information
DanRStevens committed Mar 5, 2025
1 parent 5dc6f67 commit 7acdda4
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion NAS2D/Math/MathUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ namespace NAS2D
template <typename InputType, typename OutputType>
OutputType scaleLinear(const InputType& value, const InputType& domainPoint1, const InputType& domainPoint2, const OutputType& rangePoint1, const OutputType& rangePoint2)
{
return (value - domainPoint1) * (rangePoint2 - rangePoint1) / (domainPoint2 - domainPoint1) + rangePoint1;
const auto domainWidth = domainPoint2 - domainPoint1;
const auto distance1 = value - domainPoint1;
const auto distance2 = domainPoint2 - value;
return (distance1 * rangePoint2 + distance2 * rangePoint1) / domainWidth;
}

} // namespace NAS2D

0 comments on commit 7acdda4

Please sign in to comment.