Created
April 7, 2018 06:23
-
-
Save cwharris/549fb4841f19327259cc80325a69f7b7 to your computer and use it in GitHub Desktop.
This file contains hidden or 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.Collections.Generic; | |
using System.Runtime.CompilerServices; | |
using UnityEngine; | |
namespace Game.Curves | |
{ | |
public static class BezierCurve | |
{ | |
[MethodImpl(MethodImplOptions.AggressiveInlining)] | |
public static Vector3 Evaluate( | |
float t, | |
Vector3 a, | |
Vector3 b, | |
Vector3 c, | |
Vector3 d | |
) | |
{ | |
var u = 1 - t; | |
return | |
1 * (u * u * u) * a + | |
3 * (u * u * t) * b + | |
3 * (u * t * t) * c + | |
1 * (t * t * t) * d; | |
} | |
private static IEnumerable<Vector2> Evaluate( | |
float position, | |
Queue<Vector2> queue | |
) | |
{ | |
while (queue.Count > 1) | |
{ | |
var count = queue.Count - 1; | |
for (var i = 0; i < count; i++) | |
{ | |
var a = queue.Dequeue(); | |
yield return a; | |
var b = queue.Peek(); | |
var m = a + (b - a) * position; | |
queue.Enqueue(m); | |
} | |
yield return queue.Dequeue(); | |
} | |
yield return queue.Dequeue(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment