C#或unity中實現正弦函式
阿新 • • 發佈:2018-11-03
C#或unity中實現正弦函式
本類用於第一,需要繪製一條正弦曲線的朋友;第二,需要根據正弦曲線控制物體運動的朋友;裡面都有註釋,程式碼如下:
unity中使用的程式碼:
public class Curvy_Sin { /// <summary> /// 週期 /// </summary> private float lifeTime = 2; /// <summary> /// 波峰 /// </summary> private float MaxPos = 0; /// <summary> ///波谷 /// </summary> private float MinPos = 0; /// <summary> /// 計算時使用的週期,避免週期為小於零的情況 /// </summary> private float ZQ { get { if (lifeTime <= 0) { return 2; } else {return lifeTime; } } } /// <summary> /// 構造一個正弦曲線 /// </summary> /// <param name="zhouqi"></param> /// <param name="minp"></param> /// <param name="maxp"></param> public Curvy_Sin(float zhouqi, float minp, float maxp) {this.lifeTime = zhouqi; this.MinPos = minp; this.MaxPos = maxp; } /// <summary> /// 根據X軸獲取對應Y軸的值 /// </summary> /// <param name="x"></param> /// <returns></returns> public float GetZhengXianValue(float x) { float innner = (2 * (float)Mathf.PI) / ZQ; float ya = (float)Mathf.Sin(innner * x); float maall = (MaxPos - MinPos) * 0.5f; float middle = maall + MinPos; return (middle + maall * ya); } /// <summary> /// 修改正弦引數 /// </summary> /// <param name="zhouqi"></param> /// <param name="minp"></param> /// <param name="maxp"></param> public void SetCurvyZhengXian(float zhouqi, float minp, float maxp) { this.lifeTime = zhouqi; this.MinPos = minp; this.MaxPos = maxp; } }
C#中winform或WPF
public class Curvy_Sin { /// <summary> /// 週期 /// </summary> private float lifeTime = 2; /// <summary> /// 波峰 /// </summary> private float MaxPos = 0; /// <summary> /// 波谷 /// </summary> private float MinPos = 0; /// <summary> /// 計算時使用的週期,避免週期為小於零的情況 /// </summary> private float ZQ { get { if (lifeTime <= 0) { return 2; } else { return lifeTime; } } } /// <summary> /// 構造一個正弦曲線 /// </summary> /// <param name="zhouqi"></param> /// <param name="minp"></param> /// <param name="maxp"></param> public Curvy_Sin(float zhouqi, float minp, float maxp) { this.lifeTime = zhouqi; this.MinPos = minp; this.MaxPos = maxp; } /// <summary> /// 根據X軸獲取對應Y軸的值 /// </summary> /// <param name="x"></param> /// <returns></returns> public float GetZhengXianValue(float x) { float innner = (2 * (float)Math .PI) / ZQ; float ya = (float)Math.Sin(innner * x); float maall = (MaxPos - MinPos) * 0.5f; float middle = maall + MinPos; return (middle + maall * ya); } /// <summary> /// 修改正弦引數 /// </summary> /// <param name="zhouqi"></param> /// <param name="minp"></param> /// <param name="maxp"></param> public void SetCurvyZhengXian(float zhouqi, float minp, float maxp) { this.lifeTime = zhouqi; this.MinPos = minp; this.MaxPos = maxp; } }