C#或unity中实现正弦函数
本类用于第一,需要绘制一条正弦曲线的朋友;第二,需要根据正弦曲线控制物体运动的朋友;里面都有注释,代码如下:
unity中使用的代码:
public class Curvy_Sin
{
/// <summary>
/// 周期
/// </summary>
private float lifeTime = ;
/// <summary>
/// 波峰
/// </summary>
private float MaxPos = ;
/// <summary>
/// 波谷
/// </summary>
private float MinPos = ;
/// <summary>
/// 计算时使用的周期,避免周期为小于零的情况
/// </summary>
private float ZQ
{
get
{
if (lifeTime <= )
{
return ;
}
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 = ( * (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 = ;
/// <summary>
/// 波峰
/// </summary>
private float MaxPos = ;
/// <summary>
/// 波谷
/// </summary>
private float MinPos = ;
/// <summary>
/// 计算时使用的周期,避免周期为小于零的情况
/// </summary>
private float ZQ
{
get
{
if (lifeTime <= )
{
return ;
}
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 = ( * (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;
}
}