Created
June 14, 2018 15:12
-
-
Save Drenerdo/924244034c8531835cdad47dbceaca73 to your computer and use it in GitHub Desktop.
fileofsomething.cs
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 UnityEngine; | |
using System; | |
using System.Collections; | |
using Mathx; | |
namespace Mathx | |
{ | |
public enum TrigType { Sin, Cos, Tan } | |
public static class Trig3 | |
{ | |
public static Vector3 Sin3 (Vector3 t) | |
{ | |
return new Vector3 (Mathf.Sin (t.x), Mathf.Sin (t.y), Mathf.Sin (t.z)); | |
} | |
public static Vector3 Cos3 (Vector3 t) | |
{ | |
return new Vector3 (Mathf.Cos (t.x), Mathf.Cos (t.y), Mathf.Cos (t.z)); | |
} | |
public static Vector3 Tan3 (Vector3 t) | |
{ | |
return new Vector3 (Mathf.Tan (t.x), Mathf.Tan (t.y), Mathf.Tan (t.z)); | |
} | |
} | |
[Serializable] | |
public abstract class TrigObject | |
{ | |
#region Public Properties | |
public float frequency = 1f; | |
#endregion | |
#region Private Properties | |
private float | |
phase, | |
oldFrequency; | |
#endregion | |
#region Constructors | |
public TrigObject () | |
{ | |
oldFrequency = frequency; | |
} | |
public TrigObject (float frequency) | |
{ | |
this.frequency = frequency; | |
oldFrequency = frequency; | |
} | |
#endregion | |
#region Main Methods | |
public float Solve (float t, float offset = 0f) | |
{ | |
if (frequency != oldFrequency) | |
CalculateNewFrequency (t); | |
return TrigFunction (t * oldFrequency + phase + offset); | |
} | |
#endregion | |
#region Utility Methods | |
protected abstract float TrigFunction (float t); | |
void CalculateNewFrequency (float t) | |
{ | |
float current = (t * oldFrequency + phase) % (2f * Mathf.PI); | |
float next = (t * frequency) % (2f * Mathf.PI); | |
phase = current - next; | |
oldFrequency = frequency; | |
} | |
#endregion | |
} | |
[Serializable] | |
public class Sin : TrigObject | |
{ | |
protected override float TrigFunction (float t) | |
{ | |
return Mathf.Sin (t); | |
} | |
} | |
[Serializable] | |
public class Cos : TrigObject | |
{ | |
protected override float TrigFunction (float t) | |
{ | |
return Mathf.Cos (t); | |
} | |
} | |
[Serializable] | |
public class Tan : TrigObject | |
{ | |
protected override float TrigFunction (float t) | |
{ | |
return Mathf.Tan (t); | |
} | |
} | |
[Serializable] | |
public class Trig : TrigObject | |
{ | |
public TrigType trigType = TrigType.Sin; | |
public Trig (TrigType trigType) | |
{ | |
this.trigType = trigType; | |
} | |
protected override float TrigFunction (float t) | |
{ | |
switch (trigType) | |
{ | |
case TrigType.Sin: | |
return Mathf.Sin (t); | |
case TrigType.Cos: | |
return Mathf.Cos (t); | |
case TrigType.Tan: | |
return Mathf.Tan (t); | |
default: | |
Debug.Log ("TrigType is not Sin, Cos or Tan. Using Sin."); | |
return Mathf.Sin (t); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment