Created
November 3, 2020 22:22
-
-
Save wallstop/81c6465f20d5a301c9e902a6ec0db652 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Runtime.Serialization; | |
namespace Assets.Scripts.Core.Math | |
{ | |
[DataContract] | |
[Serializable] | |
public sealed class Parabola | |
{ | |
public readonly float Length; | |
public readonly float A; | |
public readonly float B; | |
/// <summary> | |
/// Creates a Parabola that reaches a max height and has a specified length. | |
/// </summary> | |
/// <param name="max">Max height of parabola.</param> | |
/// <param name="length">Length of parabola (between x intercepts).</param> | |
public Parabola(float max, float length) | |
{ | |
if (length <= 0) | |
{ | |
throw new ArgumentException(string.Format("Expected a length greater than 0, but found: {0:0.00}.", | |
length)); | |
} | |
Length = length; | |
A = -4 * max / (length * length); | |
B = -A * length; | |
} | |
public bool ValueAt(float x, out float y) | |
{ | |
if (x < 0 || Length < x) | |
{ | |
y = float.NaN; | |
return false; | |
} | |
y = A * (x * x) + B * x; | |
return true; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment