C # XNA: searching for a function like MathHelper.Clamp

I have a function that will take a range of values ​​that should be normalized to the range [-1, 1]. MathHelper.Clamp()just accepts values ​​that are outside the range, and sets them depending on which border exceeded it. Does XNA or C # have anything that does what I'm looking for? (I am looking through the documentation but cannot find it.)

This is mouse movement. Values ​​- this is the difference in the coordinates of the mouse between one tick and the next. I am not sure what the maximum value is for this.

This function will take values ​​one at a time, so there is no iteration over all values ​​to scale them all or see what min and max are.

This is what I have:

// max - min cannot == 0
private float normalizeToRange(float value, float min, float max) {
      return (2.0f * (value - min) / (max - min)) -1.0f;
}
+3
5

2 * (x-min)/(max-min) + -1 .

(x-min)/(max-min) 0 1

2 0 2

1 -1 1

+1

, , , .

, -500 +500, , input/500.0, .

, - , , -1 1, , -1 1.

(O (n), bleh!... , , O (n ) ), , . , . , , , , .

, . , , -300, - 700. (abs (max-min)) 1000. 2 (-1 1) 2... 1000/2 = 500.

... 334. , 334 + 300 = 634. , 634/500 = 1.268. 1 ( ( 0 2), -1 1, 0,268, . , .

, 700 ( ), ((700 + 300)/500) - 1 = 1. , -300, ((-300 + 300)/500) - 1 = -1.

, , .

, , , , , , , .

, ... ... , . - , .

:. :

. - . , .

, - ... , , (-5, 3), , (1000000, 1000000) . , , , .

+1

, , .

public static class Extension
{
    public static IEnumerable<double> Normalize(this IEnumerable<double> source)
    {
        var min = source.Min();
        var max = source.Max();
        var range = max - min;
        foreach (var d in source)
        {
            yield return ((d - min) / range) * 2 - 1;
        }
    }
}
0

, XNA , . , ...

  • - : range = highValue - lowValue
  • (minValue + )/range * 2 - 1 -1 1.

, thorgoughly:) .

0

.

, .

"origo" "" N- (N - ), , 1.

, (1, 1) (0.707, 0.707), 1 (0, 0).

:

dist = sqrt(vector[0]^2 + vector[1]^2 + ... + vector[N-1]^2)
vector[x] = vector[x] / dist, for x=0..N-1

(2, 1, 3) :

dist = sqrt(2^2 + 1^2 + 3^2) = sqrt(24) ~ 4.9
vector[0] = vector[0] / 4.9 ~ 0.41
vector[1] = vector[1] / 4.9 ~ 0.2
vector[2] = vector[2] / 4.9 ~ 0.61
vector = (0.41, 0.2, 0.61)

, 1 0, - .

(, .)

maxValue = abs(max(vector[0], vector[1], ..., vector[N-1]))
vector[x] = vector[x] / maxValue, for x=0..N-1

, , :

maxValue = abs(max(2, 1, 3)) = abs(3) = 3
vector[0] = vector[0] / 3 ~ 0.67
vector[1] = vector[1] / 3 ~ 0.33
vector[2] = vector[2] / 3 ~ 1
vector = (0.67, 0.33, 1)
0
source

Source: https://habr.com/ru/post/1731268/


All Articles