Skip to content

Instantly share code, notes, and snippets.

@wallstop
Created November 3, 2020 22:22
Show Gist options
  • Save wallstop/81c6465f20d5a301c9e902a6ec0db652 to your computer and use it in GitHub Desktop.
Save wallstop/81c6465f20d5a301c9e902a6ec0db652 to your computer and use it in GitHub Desktop.
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