Skip to content
SirHall edited this page Jun 21, 2019 · 4 revisions

MathE

To use the Excessives math llibrary, just add: include Excessives; to the top of your *.cs file.

Constants

There are a few supported mathematical that may come in useful whilst working on your projects, these include:

  • MathE.GOLDENRATIO = 1.6180339887498948482
  • MathE.PLASTICNUMBER = 1.32471795724474602596
  • MathE.TAU = 6.283185307179586
  • MathE.SPEEDOFLIGHT = 299792458
  • MathE.PLANCKLENGTH = 1.61622938 * (10 * -35)
  • MathE.PLANCKTIME = 5.3911613 * (10 ^ -44)
  • MathE.PLANCKTEMPERATURE = 1.41680833 * (10 ^ 32)
  • MathE.PLANCKMASS = 4.341 * (10 ^ -9)
  • MathE.GRAVITATIONALCONSTANT = 6.6740831 * (10 ^ -11)
  • MathE.PLANCKSCONSTANT = 6.62 * (10 ^ -34)
  • MathE.LIGHTYEAR = 9460730472580800

Clamping

Often you will need to ensure that a value never falls outside a specific range, this can be done with clamping. If value falls outside the min-max range, it will clamp it back to those extremes.

int oldVal = 100
MathE.Clamp(oldVal, 15, 1).WriteLine();

15

Add Towards

This is something that is quite unique to the Excessives library as it is something that the dev of this library required a lot, but never really saw this functionality elsewhere.

Say we have a value a. What if we wanted to make a approach another value b by some c amount? You certainly could just add c to a, but what if a was larger than b? Yes you could just subtract c from a, but what requires ugly checks to be made inside your code. What if we simply want a to approach b by c, no matter where the values are in relation to eachother?

That's where AddTowards comes in. Simply let a approach b by some c value. If c is positive, move a towards b, if negative, then move a away from b.

There are three variants of the add towards method.

Obstructed

MathE.AddTowardsObstructed(value, delta, target);

The obstructed variant will move value towards target by delta. However if value reaches target, then stop it. Essentially, if value = 9, target = 10 and delta = 3 it will move value only one step to ensure that value does not pass target and will return the value 10.

Overflow

MathE.AddTowardsOverflow(value, delta, target);

The difference between the over flow and the obstructed variant is that this will not prevent value from going past target. If value = 9, target = 10 and delta = 3, then this will return 12, which when fed back in as the new value for value, it will try to move value back towards target, overpass it again and return the value 9.

Rebound

MathE.AddTowardsRebound(value, delta, target);

The most complex of the three, it also has the most interesting behaviour.

If value = 5, target = 10 and delta = 8, it will use up '5 delta' to move the value towards the target. If value reaches target in less than what the delta specified (here being 8), it will use the remaining delta to rebound the value back. Therefore, here it only took 5 to move the value to the target, leaving us with a remaining delta of 3. Therefore the value will then be rebound in the opposite direction by 3, returning the value 7.

ClampWrap

MathE.ClampWrap(value, min, max);

If you have ever worked with rotations it can be frustrating to ensure that the range stays within [0-360). Whilst it may seem that this is an easy fix with a modulus, what if our value dips into the negative range and we want a minimum other than 0? This is where MathE.ClampWrap() comes in.

It will ensure that if you require a value to stay in the range [min-max), it will wrap your value around the edges of that range perfectly.

If your value goes 5 units above the maximum, it will cleanly wrap around your value to be the minimum + 5. (This coming from modulo ofcourse).

ReMap

MathE.ReMap(value, inMin, inMax, outMin, outMax);

Let's say you're working with a value that you expect to fall within the range [0-1]. You then need this value to be remapped so that if it was originally halfway through [0-1] (simply the value 0.5), that it now needs to be halfway through the range [127-2048]. Or what if your value was 0.8? It needs to be 80% of the way through [127-2048] (a value of 1663.8).

Or even remap from the range [283-402] to [3-1023.502], it's easy and possible with MathE.ReMap().

ToFloat/ToInt

MathE.ToInt(boolVal); MathE.ToFloat(boolVal);

This is a simple boolean extension method that returns 0/0.0f for false, and 1/1.0f for true.

RoundToN

MathE.RoundToN(val, n); MathE.CeilToN(val, n); MathE.FloorToN(val, n);

Many times you need to be able to round numbers, but what if you need to round your number towards the nearest 3.05? Or round it up(or down) to the nearest 6?

Interpolation

MathE.Lerp(a, b, t);

Given the range [a-b], and a t, this returns a value that is t through that range.

Or if a = 3 and b = 15.4, and we need to find the value that is 23%(or a t of 0.23) from a to b, then this will return 5.852.

MathE.QuadLerp(a, b, t);

QuadLerp will interpolate from [a-b] given t quadratically, instead of linearly.

MathE.SineLerp(a, b, t);

SineLerp will interpolate from [a-b] given t, but this time using a sine-wave segment.

Reverse Interpolation

MathE.UnLerp(a, b, lerped); MathE.UnQuadLerp(a, b, lerped);

Given a range [a-b] and a lerped value that falls within that range, the unlerp methods will return the original t.

Radian/Degrees Conversion

yourDouble/Float.ToDeg(); yourDouble/Float.ToRad();

Simply converts between degrees and radians.