贝塔(Beta)函数

作者:追风剑情 发布于:2019-10-12 10:11 分类:Algorithms

贝塔(Beta)函数是统计学中的密度函数,用来描述概率分布。可利用密度函数来设计游戏地图中不同位置的刷怪机率。

工具:Unity2018

示例

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using UnityEngine;
  6. using UnityEngine.UI;
  7.  
  8. public class BetaCurve : MonoBehaviour
  9. {
  10. public float P = 2;
  11. public float Q = 2;
  12. public RawImage rawImage;
  13. public Text text;
  14. public string savePath;
  15. public bool refresh = true;
  16. public bool save = false;
  17.  
  18. void Update()
  19. {
  20. if (!refresh)
  21. return;
  22. refresh = false;
  23. //利用Beta函数在Texture上画曲线
  24. Texture2D texture = new Texture2D(100, 100);
  25. for (float t=0; t<=1; t+=0.01f)
  26. {
  27. float v = BetaLerp(t);
  28. int x = (int)(t * texture.width);
  29. int y = (int)(v * texture.width);
  30. texture.SetPixel(x, y, Color.red);
  31. }
  32. texture.Apply();
  33. rawImage.texture = texture;
  34.  
  35. if (text)
  36. text.text = string.Format("P={0}\nQ={1}", P, Q);
  37.  
  38. if (save)
  39. SavePNG(string.Format("{0}/P{1}_Q{2}.png", savePath, P, Q), texture);
  40. }
  41.  
  42. float BetaLerp(float x)
  43. {
  44. //贝塔(Beta)函数
  45. float y = Mathf.Pow(x, P - 1) * Mathf.Pow(1 - x, Q - 1);
  46. return y;
  47. }
  48.  
  49. public static void SavePNG(string filePath, Texture2D texture)
  50. {
  51. try
  52. {
  53. byte[] pngData = texture.EncodeToPNG();
  54. File.WriteAllBytes(filePath, pngData);
  55. }
  56. catch (Exception ex)
  57. {
  58. Debug.Log(ex.StackTrace);
  59. }
  60. }
  61. }


运行测试

111.png 222.png 333.png 444.png

555.png 666.png 777.png 888.png 

999.png 10000.png 20000.png 300000.png

标签: Algorithms

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号