Unity中關於Mathf的Sin、Cos

在開發設計中,有些需求會涉及數學知識,就我來說吧,最近想到了好想法,然後試着碼了一下。

做圓的參數方程的時候,卡住了。因爲圓的參數方程 x=a+r cosθ y=b+r sinθ(θ∈ [0,2π) ) (a,b) 爲圓心座標,r 爲圓半徑,θ 爲參數,(x,y) 爲經過點的座標。


問題出現在cosθ和sinθ了,按常理我們直接Mathf.Cos(_angle*Mathf.Deg2Rad)---->(其中_angle爲角度)就可以求出我們想要的結果,但我沒考慮到誤差問題,所以求得的結果是無限不循環小數。解決方法是取四捨五入,即Mathf.Round()

下面,我測試了一下,先上結果(點擊圖片可放大):

 

測試代碼(不用掛載到對象上即可運行): 

 

using UnityEngine;
 
 
 
/*
 * 
 *  Writer:June
 * 
 *  Date: 2020.03.26
 * 
 *  Function:正弦餘弦值測試
 * 
 *  Remarks:
 * 
 */
 
public class MathfSinAndCosTest : MonoBehaviour
{
    public static float _angle = 270;
 
 
    [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterSceneLoad)]
    public static void DeBugMessage()
    {
        Debug.LogFormat("角度:{0},角度轉成弧度:{1},正常輸出Cos({1}):{2},四捨五入後Cos({1}):{3},正常輸出Sin({1}):{4},四捨五入Sin({1}):{5}",
            _angle,
            _angle * Mathf.Deg2Rad,
            Mathf.Cos(_angle * Mathf.Deg2Rad),
            Mathf.Round(Mathf.Cos(_angle * Mathf.Deg2Rad)),
            Mathf.Sin(_angle * Mathf.Deg2Rad),
            Mathf.Round(Mathf.Sin(_angle * Mathf.Deg2Rad))
            );
    }
 
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章