Introduction
Sometimes you just need a simple straight line function, and that's what ClampedLine<class T>
gives you. If you feel like getting fancy, you have the option of "clamping" Y to minimum and maximum values outside of a range in X, as shown in the brilliantly detailed picture above. The code is simple, as opposed to fast, and should compile just about anywhere. After you create a ClampedLine
, the "function call" T operator()(T X)
can be used to pull out a Y value for a particular X.
This certainly isn't original, but I couldn't find a free equivalent anywhere after a quick search.
Here are a couple of examples, rather than rambling on:
#include "clampedline.h"
ClampedLine<float> celsiusFromFahrenheit(32.0, 0, 212.0, 100.0, false);
float y = celsiusFromFahrenheit(72.0);
ClampedLine<long> adjustedScore(60, 10, 90, 100, true);
long finalScore = adjustedScore(-5);
finalScore = adjustedScore(60);
finalScore = adjustedScore(75);
finalScore = adjustedScore(90);
finalScore = adjustedScore(140);
Using the code
- Include the header file "clampedline.h". Note it doesn't include any other headers.
- You'll need two points (X0, Y0) (X1,Y1) on your line. If you specify a clamped line, then values of X below X0 will give a Y value of Y0, and values of X above X1 will return a Y value of Y1.
- A simple signed arithmetic type such as long or double works best as the "T" parameter for the template. If you try an unsigned type for T, watch out for values of X that give a nominally negative value of Y.
- If your
ClampedLine
lives a long time, you could package it cleanly in a member function, something like:
long MyClass::FinalScore(long rawScore)
{
static ClampedLine<long> adjustedScore(
60, 10, 90, 100, true);
return adjustedScore(rawScore);
}
- The code isn't copyrighted. Scale, offset and clamp to your heart's content, specialize it, add getters and setters. In a brief fit of lucidity I tested this little class before zipping it, but you shouldn't take my word for that.
Points of Interest
When "T" is an integer type, results are rounded (see the code for the standard trick to do that). And you'll get values out without complaint if you have X0 equal to X1 in the constructor for your ClampedLine
, just don't expect them to be the values you want:)
History
- Version 1.0, Oct 7, 2004.
Ken likes tennis, the piano, Buffy, and programming, in no particular order.