码迷,mamicode.com
首页 > 编程语言 > 详细

C#或unity中实现正弦函数

时间:2018-11-03 17:32:04      阅读:364      评论:0      收藏:0      [点我收藏+]

标签:lan   pre   unity   set   ddl   har   运动   too   returns   

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;
    }
}

原文:http://www.shiweikeji.club/?p=316

C#或unity中实现正弦函数

标签:lan   pre   unity   set   ddl   har   运动   too   returns   

原文地址:https://www.cnblogs.com/mathb/p/9901184.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!