XNA C# 得到球内随机一点

经过总结,在立方体中取随机点再剔除是最快的方法,所以,找到球内的随机一点,只需如此如此:
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;

经过总结,在立方体中取随机点再剔除是最快的方法,所以,找到球内的随机一点,只需如此如此:

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;

namespace GameHelpers
{
    public class GameHelper
    {
        public static Random Random = new Random();
        public static float RandomNext(float min, float max)
        {
            return (min + (float)((max - min) * Random.NextDouble()));
        }
        /// <summary>
        /// 得到球状区域内的随机一点
        /// </summary>
        /// <param name="center">球心</param>
        /// <param name="radius">半径</param>
        /// <returns>结果</returns>
        public static Vector3 RandomPointInBall(Vector3 center, float radius)
        {
            Vector3 a;
            do
            {
                a = new Vector3(RandomNext(-radius, radius), RandomNext(-radius, radius), RandomNext(-radius, radius));

            } while (Vector3.Distance(a, Vector3.Zero) > radius);//直到随机出的点在球内..
            return a + center;
        }
}

1 thought on “XNA C# 得到球内随机一点”

Leave a Reply

Your email address will not be published. Required fields are marked *