Ejemplo n.º 1
0
        private MobSpawner <ZombieFactory> CreateMobSpawner(ILogger logger,
                                                            UnityTimeProvider timeProvider,
                                                            EntitiesSpawner entitiesSpawner,
                                                            TypeTargetLocator tankMobTargetLocator,
                                                            StateMachineFactory stateMachineFactory,
                                                            DefaultRandom random)
        {
            var zombieFactories = new List <ZombieFactory>();

            foreach (var zombiePrefab in Zombies)
            {
                var pool = new SimplePool <Zombie>(logger,
                                                   15,
                                                   10,
                                                   p =>
                {
                    var zombie = Instantiate(zombiePrefab);
                    return(zombie);
                });
                var zombieFactory = new ZombieFactory(timeProvider, entitiesSpawner, tankMobTargetLocator, stateMachineFactory, _deps.Doors, pool);
                zombieFactories.Add(zombieFactory);
            }

            var zombieSpawnPoints = GetZombieSpawnPoints(_deps.ZombieSpawnPoints);
            var mobSpawner        = new MobSpawner <ZombieFactory>(random, entitiesSpawner, new MobSpawnerSettings(AliveZombieCount, zombieSpawnPoints), zombieFactories);

            return(mobSpawner);
        }
Ejemplo n.º 2
0
            private static Point PopRandom(List <Point> Points, DefaultRandom Generator)
            {
                int   Idx = Generator.RandomInt(Points.Count - 1);
                Point P   = Points[Idx];

                Points.RemoveAt(Idx);
                return(P);
            }
Ejemplo n.º 3
0
        /// <summary>
        /// Construct a new instance of the Simulation class
        /// </summary>
        /// <param name="startDate">The starting date of the simulation</param>
        /// <param name="iterationNo">The iteration of this simulation</param>
        /// <param name="seed">the seed value for the random generator</param>
        /// <param name="shuffleTransitions">Sets whether the order in which state transitions are tested is shuffled randomly</param>
        protected Simulation(DateTime startDate, int iterationNo, int seed, bool shuffleTransitions)
        {
            IterationNo         = iterationNo;
            StartDate           = startDate;
            RandomProvider      = new DefaultRandom(seed);
            _shuffleTransitions = shuffleTransitions;

            RootContext = new Region <TAgent>("root", "root", "Global"); // This is fine because it is only a grouped context - not an instance of TG
            _allContexts.Add(RootContext.Name, RootContext);
        }
Ejemplo n.º 4
0
        public void RandomMinMaxValueTest()
        {
            var random = new DefaultRandom();

            for (var i = 0; i < 1000; i++)
            {
                var next = random.Next(10, 20);
                Assert.InRange(next, 10, 20);
            }
        }
Ejemplo n.º 5
0
        public void BasicLoremIpsumGeneratorTest()
        {
            var random = new DefaultRandom(12345);
            var gen    = new LoremIpsumGenerator(random);
            var text   = gen.CreateText(1);

            Assert.Equal("lorem", text);
            text = gen.CreateText(10);

            Assert.Equal("lorem ipsum dolor sit amet stet kasd gubergren tempor dolor", text);
        }
Ejemplo n.º 6
0
        public void BasicRandomTest()
        {
            var random = new DefaultRandom(12345);
            var next   = random.Next();

            Assert.Equal(143337951, next);
            next = random.Next();
            Assert.Equal(150666398, next);
            next = random.Next();
            Assert.Equal(1663795458, next);
        }
        public void TestRandomPointWithin()
        {
            DefaultRandom         random        = new DefaultRandom();
            AxisAlignedRectangle2 testRectangle = new AxisAlignedRectangle2(
                new Vector2(12.3f, 45.6f), new Vector2(98.7f, 65.4f)
                );

            for (int index = 0; index < Specifications.ProbabilisticFunctionSamples; ++index)
            {
                Vector2 randomPoint = testRectangle.RandomPointWithin(random);
                Assert.AreEqual(testRectangle.ClosestPointTo(randomPoint), randomPoint);
            }
        }
Ejemplo n.º 8
0
        public void RandomMinMaxValueTest2()
        {
            var random   = new DefaultRandom();
            var hitCount = new int[4];

            for (var i = 0; i < 1000; i++)
            {
                var next = random.Next(0, 3);
                hitCount[next]++;
            }
            Assert.True(hitCount[0] > 0);
            Assert.True(hitCount[3] > 0);
        }
Ejemplo n.º 9
0
        public void TestRandomPointOnPerimeterForYPlane()
        {
            IRandom randomNumberGenerator = new DefaultRandom();

            Plane3 yPlane = new Plane3(Vector3.UnitY * 2.0f, Vector3.Up);

            for (int index = 0; index < Specifications.ProbabilisticFunctionSamples; ++index)
            {
                Vector3 randomPoint = yPlane.RandomPointOnPerimeter(randomNumberGenerator);

                Assert.IsTrue(float.IsInfinity(randomPoint.X));
                Assert.AreEqual(2.0f, randomPoint.Y);
                Assert.IsTrue(float.IsInfinity(randomPoint.Z));
            }
        }
Ejemplo n.º 10
0
        public void TestRandomPointWithinForZPlane()
        {
            IRandom randomNumberGenerator = new DefaultRandom();

            Plane3 zPlane = new Plane3(Vector3.UnitZ * 2.0f, Vector3.Backward);

            for (int index = 0; index < Specifications.ProbabilisticFunctionSamples; ++index)
            {
                Vector3 randomPoint = zPlane.RandomPointWithin(randomNumberGenerator);

                Assert.IsTrue(float.IsInfinity(randomPoint.X));
                Assert.IsTrue(float.IsInfinity(randomPoint.Y));
                Assert.AreEqual(2.0f, randomPoint.Z);
            }
        }
Ejemplo n.º 11
0
        public void TestWeightedSampling()
        {
            var choices = new List <WeightedChoice <string> >
            {
                new WeightedChoice <string>("A", 0.25),
                new WeightedChoice <string>("B", 0.25),
                new WeightedChoice <string>("C", 0.25),
                new WeightedChoice <string>("D", 0.25)
            };

            var random = new DefaultRandom(1234);
            var result = WeightedSampler <string> .PickMultipleItems(choices, 2, random);

            Assert.AreEqual(2, result.Count);
        }
Ejemplo n.º 12
0
        public void BasicLoremIpsumGeneratorToFileTest()
        {
            var random = new DefaultRandom(12345);
            var gen    = new LoremIpsumGenerator(random);

            var tmp = new DefaultTempUtil();

            tmp.UseFile(tempFile =>
            {
                using (var writer = new StreamWriter(tempFile))
                    gen.WriteText(10, writer);
                var fileContents = File.ReadAllText(tempFile);
                Assert.Equal("lorem ipsum dolor sit amet stet kasd gubergren tempor ipsum", fileContents);
            });
        }
Ejemplo n.º 13
0
        public void TestRandomPointOnPerimeter()
        {
            Disc2 testDisc = new Disc2(new Vector2(123.4f, 567.8f), 9.0f);

            DefaultRandom randomNumberGenerator = new DefaultRandom();

            for (int index = 0; index < Specifications.ProbabilisticFunctionSamples; ++index)
            {
                Vector2 randomPoint = testDisc.RandomPointOnPerimeter(randomNumberGenerator);
                Assert.That(
                    (testDisc.Center - randomPoint).Length(),
                    Is.EqualTo(testDisc.Radius).Within(36).Ulps
                    // TODO: This method suffers from accuracy problems!
                    );
            }
        }
Ejemplo n.º 14
0
        public void TestReservoir()
        {
            var choices = new List <string>
            {
                "A", "B", "C", "D", "E"
            };

            var weights = new List <double>
            {
                0.3, 0.25, 0.2, 0.15, 0.1
            };

            var random   = new DefaultRandom(1234);
            var selected = Reservoir.ReservoirSampling(weights, 2, random);

            Assert.AreEqual(2, selected.Count());
        }
Ejemplo n.º 15
0
        public void TestRandomPointWithin()
        {
            Disc2 testDisc = new Disc2(new Vector2(123.4f, 567.8f), 9.0f);

            DefaultRandom randomNumberGenerator = new DefaultRandom();

            for (int index = 0; index < Specifications.ProbabilisticFunctionSamples; ++index)
            {
                Vector2 randomPoint = testDisc.RandomPointWithin(randomNumberGenerator);

                float randomPointDistance = (testDisc.Center - randomPoint).Length();
                if (!FloatHelper.AreAlmostEqual(randomPointDistance, testDisc.Radius, 36))
                {
                    Assert.LessOrEqual(randomPointDistance, testDisc.Radius);
                }
            }
        }
Ejemplo n.º 16
0
            private static Point GenerateRandomPointAround(Point P, float MinDist, DefaultRandom Generator)
            {
                // start with non-uniform distribution
                float R1 = Generator.RandomFloat();
                float R2 = Generator.RandomFloat();

                // radius should be between MinDist and 2 * MinDist
                float Radius = MinDist * (R1 + 1.0f);

                // random angle
                float Angle = 2 * 3.141592653589f * R2;

                // the new point is generated around the point (x, y)
                float X = (float)(P.x + Radius * Math.Cos(Angle));
                float Y = (float)(P.y + Radius * Math.Sin(Angle));

                return(new Point(X, Y));
            }
        public void TestRandomPointOnPerimeter()
        {
            DefaultRandom         random        = new DefaultRandom();
            AxisAlignedRectangle2 testRectangle = new AxisAlignedRectangle2(
                new Vector2(12.3f, 45.6f), new Vector2(98.7f, 65.4f)
                );

            float[] possibleXs = new float[] { 12.3f, 98.7f };
            float[] possibleYs = new float[] { 45.6f, 65.4f };

            for (int index = 0; index < Specifications.ProbabilisticFunctionSamples; ++index)
            {
                Vector2 randomPoint = testRectangle.RandomPointOnPerimeter(random);
                Assert.AreEqual(testRectangle.ClosestPointTo(randomPoint), randomPoint);

                Assert.IsTrue(
                    arrayContains(possibleXs, randomPoint.X) || arrayContains(possibleYs, randomPoint.Y)
                    );
            }
        }
Ejemplo n.º 18
0
        public void Extend(ITestController testController, object suite)
        {
            var suiteType           = suite.GetType();
            var fieldsWithAttribute = suiteType.GetFieldsWithAttribute <AutoDataAttribute>()
                                      .OrderBy(x => x.Item1.Name).ToList();

            if (fieldsWithAttribute.Count == 0)
            {
                return;
            }

            var seed          = GetSeed(suiteType);
            var random        = new DefaultRandom(seed);
            var configuration = GetAutoDataConfiguration(suiteType);

            var generator = TestDataGeneratorFactory.Create(x => configuration(x).UseRandom(random));

            // TODO: add seed to data
            testController.AddAction <SetupExtension>(
                $"<Create_AutoData><{seed}>",
                x => fieldsWithAttribute.ForEach(t => CreateAndAssignAuto(suite, generator, t.Item2, t.Item1)));
        }
Ejemplo n.º 19
0
        public void TestRandomPointOnSurface()
        {
            Sphere3 unitSphere  = new Sphere3(Vector3.Zero, 1.0f);
            Sphere3 innerSphere = new Sphere3(Vector3.Zero, 1.0f - Specifications.HullAccuracy);
            Sphere3 outerSphere = new Sphere3(Vector3.Zero, 1.0f + Specifications.HullAccuracy);

            DefaultRandom random = new DefaultRandom();

            Vector3 total = Vector3.Zero;

            for (int i = 0; i < Specifications.ProbabilisticFunctionSamples; ++i)
            {
                Vector3 point = unitSphere.RandomPointOnSurface(random);

                Assert.IsFalse(
                    innerSphere.Contains(point), "Random point lies on the sphere's hull"
                    );
                Assert.IsTrue(
                    outerSphere.Contains(point), "Random point lies on the sphere's hull"
                    );

                total += point;
            }

            total /= Specifications.ProbabilisticFunctionSamples;

            Assert.AreEqual(
                0.0f, total.X, Specifications.ProbabilisticFunctionDeviation,
                "Random points average on the center of the sphere on the X axis"
                );
            Assert.AreEqual(
                0.0f, total.Y, Specifications.ProbabilisticFunctionDeviation,
                "Random points average on the center of the sphere on the Y axis"
                );
            Assert.AreEqual(
                0.0f, total.Z, Specifications.ProbabilisticFunctionDeviation,
                "Random points average on the center of the sphere on the Z axis"
                );
        }
Ejemplo n.º 20
0
        public override void Init(List <SceneNode> sceneNodes)
        {
            var logger = CreateLogger();

            _deps = GetDeps(sceneNodes);
            var random = new DefaultRandom(new Random());

            var tankMobTargetLocator = new TypeTargetLocator(EntityType.Tank);
            var targetLocators       = new List <ITargetLocator>
            {
                tankMobTargetLocator
            };

            var entitiesSpawner = new EntitiesSpawner(targetLocators);
            var timeProvider    = new UnityTimeProvider();

            CreateTank(logger, timeProvider, entitiesSpawner);

            var stateMachineFactory = new StateMachineFactory();
            var mobSpawner          = CreateMobSpawner(logger, timeProvider, entitiesSpawner, tankMobTargetLocator, stateMachineFactory, random);

            mobSpawner.Start();
        }
Ejemplo n.º 21
0
        public void TestRandomPointOnPerimeter()
        {
            IRandom randomNumberGenerator = new DefaultRandom();

            // A random point has to involve infinity (since the chance that of hitting the
            // meager numeric range of a float, or any other finite number system, is about
            // zero for a infinitely large plane). But given the orientation of the plane,
            // only these combinations of positive and negative infinity can be possible.
            Vector3[] possiblePoints = new Vector3[] {
                new Vector3(float.NegativeInfinity, float.PositiveInfinity, float.PositiveInfinity),
                new Vector3(float.NegativeInfinity, float.NegativeInfinity, float.PositiveInfinity),
                new Vector3(float.PositiveInfinity, float.PositiveInfinity, float.NegativeInfinity),
                new Vector3(float.PositiveInfinity, float.NegativeInfinity, float.NegativeInfinity),
            };

            Plane3 plane = new Plane3(Vector3.Zero, Vector3.Normalize(Vector3.One));

            for (int index = 0; index < Specifications.ProbabilisticFunctionSamples; ++index)
            {
                Vector3 randomPoint = plane.RandomPointOnPerimeter(randomNumberGenerator);
                CollectionAssert.Contains(possiblePoints, randomPoint);
            }
        }
    public void TestRandomPointOnPerimeter() {
      DefaultRandom random = new DefaultRandom();
      AxisAlignedRectangle2 testRectangle = new AxisAlignedRectangle2(
        new Vector2(12.3f, 45.6f), new Vector2(98.7f, 65.4f)
      );

      float[] possibleXs = new float[] { 12.3f, 98.7f };
      float[] possibleYs = new float[] { 45.6f, 65.4f };

      for(int index = 0; index < Specifications.ProbabilisticFunctionSamples; ++index) {
        Vector2 randomPoint = testRectangle.RandomPointOnPerimeter(random);
        Assert.AreEqual(testRectangle.ClosestPointTo(randomPoint), randomPoint);

        Assert.IsTrue(
          arrayContains(possibleXs, randomPoint.X) || arrayContains(possibleYs, randomPoint.Y)
        );
      }
    }
Ejemplo n.º 23
0
    public void TestRandomPointOnPerimeterForYPlane() {
      IRandom randomNumberGenerator = new DefaultRandom();

      Plane3 yPlane = new Plane3(Vector3.UnitY * 2.0f, Vector3.Up);
      for(int index = 0; index < Specifications.ProbabilisticFunctionSamples; ++index) {
        Vector3 randomPoint = yPlane.RandomPointOnPerimeter(randomNumberGenerator);

        Assert.IsTrue(float.IsInfinity(randomPoint.X));
        Assert.AreEqual(2.0f, randomPoint.Y);
        Assert.IsTrue(float.IsInfinity(randomPoint.Z));
      }
    }
Ejemplo n.º 24
0
    public void TestRandomPointWithinForZPlane() {
      IRandom randomNumberGenerator = new DefaultRandom();

      Plane3 zPlane = new Plane3(Vector3.UnitZ * 2.0f, Vector3.Backward);
      for(int index = 0; index < Specifications.ProbabilisticFunctionSamples; ++index) {
        Vector3 randomPoint = zPlane.RandomPointWithin(randomNumberGenerator);

        Assert.IsTrue(float.IsInfinity(randomPoint.X));
        Assert.IsTrue(float.IsInfinity(randomPoint.Y));
        Assert.AreEqual(2.0f, randomPoint.Z);
      }
    }
Ejemplo n.º 25
0
    public void TestRandomPointWithin() {
      IRandom randomNumberGenerator = new DefaultRandom();

      // A random point has to involve infinity (since the chance that of hitting the
      // meager numeric range of a float, or any other finite number system, is about
      // zero for a infinitely large plane). But given the orientation of the plane,
      // only these combinations of positive and negative infinity can be possible.
      Vector3[] possiblePoints = new Vector3[] {
        new Vector3(float.NegativeInfinity, float.PositiveInfinity, float.PositiveInfinity),
        new Vector3(float.NegativeInfinity, float.NegativeInfinity, float.PositiveInfinity),
        new Vector3(float.PositiveInfinity, float.PositiveInfinity, float.NegativeInfinity),
        new Vector3(float.PositiveInfinity, float.NegativeInfinity, float.NegativeInfinity),
      };

      Plane3 plane = new Plane3(Vector3.Zero, Vector3.Normalize(Vector3.One));
      for(int index = 0; index < Specifications.ProbabilisticFunctionSamples; ++index) {
        Vector3 randomPoint = plane.RandomPointWithin(randomNumberGenerator);
        //Assert.That(randomPoint, Is.OneOf(possiblePoints));
        CollectionAssert.Contains(possiblePoints, randomPoint);
      }
    }
Ejemplo n.º 26
0
    public void TestRandomPointWithin() {
      Disc2 testDisc = new Disc2(new Vector2(123.4f, 567.8f), 9.0f);

      DefaultRandom randomNumberGenerator = new DefaultRandom();
      for(int index = 0; index < Specifications.ProbabilisticFunctionSamples; ++index) {
        Vector2 randomPoint = testDisc.RandomPointWithin(randomNumberGenerator);

        float randomPointDistance = (testDisc.Center - randomPoint).Length();
        if(!FloatHelper.AreAlmostEqual(randomPointDistance, testDisc.Radius, 36)) {
          Assert.LessOrEqual(randomPointDistance, testDisc.Radius);
        }
      }

    }
Ejemplo n.º 27
0
    public void TestRandomPointOnPerimeter() {
      Disc2 testDisc = new Disc2(new Vector2(123.4f, 567.8f), 9.0f);

      DefaultRandom randomNumberGenerator = new DefaultRandom();
      for(int index = 0; index < Specifications.ProbabilisticFunctionSamples; ++index) {
        Vector2 randomPoint = testDisc.RandomPointOnPerimeter(randomNumberGenerator);
        Assert.That(
          (testDisc.Center - randomPoint).Length(),
          Is.EqualTo(testDisc.Radius).Within(36).Ulps
          // TODO: This method suffers from accuracy problems!
        );
      }
      
    }
Ejemplo n.º 28
0
            public static float[,] Generate(int NumPoints = 0, float MinDist = -1f,
                                            bool Circle   = true, int NewPointsCount = 30)
            {
                if (MinDist <= 0.0f && NumPoints <= 0)
                {
                    NumPoints = 2000;
                }
                if (MinDist <= 0.0f)
                {
                    MinDist = (float)Math.Sqrt((float)NumPoints) / NumPoints;
                }
                if (NumPoints <= 0)
                {
                    NumPoints = (int)(1 / (MinDist * MinDist));
                }

                var Generator    = new DefaultRandom();
                var SamplePoints = new List <Point>();
                var ProcessList  = new List <Point>();

                // create the grid
                float CellSize = MinDist / (float)Math.Sqrt(2.0f);

                int GridW = (int)Math.Ceiling(1.0f / CellSize);
                int GridH = (int)Math.Ceiling(1.0f / CellSize);

                Grid Grid = new Grid(GridW, GridH, CellSize);

                Point FirstPoint;

                do
                {
                    FirstPoint = new Point(Generator.RandomFloat(), Generator.RandomFloat());
                } while (!(Circle ? FirstPoint.IsInCircle() : FirstPoint.IsInRectangle()));

                // update containers
                ProcessList.Add(FirstPoint);
                SamplePoints.Add(FirstPoint);
                Grid.Insert(FirstPoint);

                // generate new points for each point in the queue
                while (ProcessList.Count > 0 && SamplePoints.Count < NumPoints)
                {
                    Point Point = PopRandom(ProcessList, Generator);

                    for (int i = 0; i < NewPointsCount; i++)
                    {
                        Point NewPoint = GenerateRandomPointAround(Point, MinDist, Generator);

                        bool Fits = Circle ? NewPoint.IsInCircle() : NewPoint.IsInRectangle();

                        if (Fits && !Grid.IsInNeighbourhood(NewPoint, MinDist, CellSize))
                        {
                            ProcessList.Add(NewPoint);
                            SamplePoints.Add(NewPoint);
                            Grid.Insert(NewPoint);
                            continue;
                        }
                    }
                }

                var result = new float[SamplePoints.Count, 2];

                for (int i = 0; i < SamplePoints.Count; i++)
                {
                    result[i, 0] = SamplePoints[i].x * 2 - 1;
                    result[i, 1] = SamplePoints[i].y * 2 - 1;
                }

                return(result);
            }
Ejemplo n.º 29
0
 private static Point PopRandom(List<Point> Points, DefaultRandom Generator)
 {
     int Idx = Generator.RandomInt(Points.Count - 1);
     Point P = Points[Idx];
     Points.RemoveAt(Idx);
     return P;
 }
Ejemplo n.º 30
0
        public void DefaultRandom_ResultIsInRange0_10()
        {
            var uut = new DefaultRandom(0, 10);

            Assert.That(uut.GetResult, Is.GreaterThanOrEqualTo(0).And.LessThan(10));
        }
Ejemplo n.º 31
0
            public static float[,] Generate(int NumPoints = 0, float MinDist = -1f,
                bool Circle = true, int NewPointsCount = 30)
            {
                if (MinDist <= 0.0f && NumPoints <= 0)
                    NumPoints = 2000;
                if (MinDist <= 0.0f)
                    MinDist = (float)Math.Sqrt((float)NumPoints) / NumPoints;
                if (NumPoints <= 0)
                    NumPoints = (int)(1 / (MinDist * MinDist));

                var Generator = new DefaultRandom();
                var SamplePoints = new List<Point>();
                var ProcessList = new List<Point>();

                // create the grid
                float CellSize = MinDist / (float)Math.Sqrt(2.0f);

                int GridW = (int)Math.Ceiling(1.0f / CellSize);
                int GridH = (int)Math.Ceiling(1.0f / CellSize);

                Grid Grid = new Grid(GridW, GridH, CellSize);

                Point FirstPoint;
                do
                {
                    FirstPoint = new Point(Generator.RandomFloat(), Generator.RandomFloat());
                } while (!(Circle ? FirstPoint.IsInCircle() : FirstPoint.IsInRectangle()));

                // update containers
                ProcessList.Add(FirstPoint);
                SamplePoints.Add(FirstPoint);
                Grid.Insert(FirstPoint);

                // generate new points for each point in the queue
                while (ProcessList.Count > 0 && SamplePoints.Count < NumPoints)
                {
                    Point Point = PopRandom(ProcessList, Generator);

                    for (int i = 0; i < NewPointsCount; i++)
                    {
                        Point NewPoint = GenerateRandomPointAround(Point, MinDist, Generator);

                        bool Fits = Circle ? NewPoint.IsInCircle() : NewPoint.IsInRectangle();

                        if (Fits && !Grid.IsInNeighbourhood(NewPoint, MinDist, CellSize))
                        {
                            ProcessList.Add(NewPoint);
                            SamplePoints.Add(NewPoint);
                            Grid.Insert(NewPoint);
                            continue;
                        }
                    }
                }

                var result = new float[SamplePoints.Count, 2];
                for (int i = 0; i < SamplePoints.Count; i++)
                {
                    result[i, 0] = SamplePoints[i].x * 2 - 1;
                    result[i, 1] = SamplePoints[i].y * 2 - 1;
                }

                return result;
            }
    public void TestRandomPointWithin() {
      DefaultRandom random = new DefaultRandom();
      AxisAlignedRectangle2 testRectangle = new AxisAlignedRectangle2(
        new Vector2(12.3f, 45.6f), new Vector2(98.7f, 65.4f)
      );

      for(int index = 0; index < Specifications.ProbabilisticFunctionSamples; ++index) {
        Vector2 randomPoint = testRectangle.RandomPointWithin(random);
        Assert.AreEqual(testRectangle.ClosestPointTo(randomPoint), randomPoint);
      }
    }
Ejemplo n.º 33
0
    public void TestRandomPointOnSurface() {
      Sphere3 unitSphere = new Sphere3(Vector3.Zero, 1.0f);
      Sphere3 innerSphere = new Sphere3(Vector3.Zero, 1.0f - Specifications.HullAccuracy);
      Sphere3 outerSphere = new Sphere3(Vector3.Zero, 1.0f + Specifications.HullAccuracy);

      DefaultRandom random = new DefaultRandom();

      Vector3 total = Vector3.Zero;
      for(int i = 0; i < Specifications.ProbabilisticFunctionSamples; ++i) {
        Vector3 point = unitSphere.RandomPointOnSurface(random);

        Assert.IsFalse(
          innerSphere.Contains(point), "Random point lies on the sphere's hull"
        );
        Assert.IsTrue(
          outerSphere.Contains(point), "Random point lies on the sphere's hull"
        );

        total += point;
      }

      total /= Specifications.ProbabilisticFunctionSamples;

      Assert.AreEqual(
        0.0f, total.X, Specifications.ProbabilisticFunctionDeviation,
        "Random points average on the center of the sphere on the X axis"
      );
      Assert.AreEqual(
        0.0f, total.Y, Specifications.ProbabilisticFunctionDeviation,
        "Random points average on the center of the sphere on the Y axis"
      );
      Assert.AreEqual(
        0.0f, total.Z, Specifications.ProbabilisticFunctionDeviation,
        "Random points average on the center of the sphere on the Z axis"
      );
      
    }
Ejemplo n.º 34
0
            private static Point GenerateRandomPointAround(Point P, float MinDist, DefaultRandom Generator )
            {
                // start with non-uniform distribution
                float R1 = Generator.RandomFloat();
                float R2 = Generator.RandomFloat();

                // radius should be between MinDist and 2 * MinDist
                float Radius = MinDist * (R1 + 1.0f);

                // random angle
                float Angle = 2 * 3.141592653589f * R2;

                // the new point is generated around the point (x, y)
                float X = (float)(P.x + Radius * Math.Cos(Angle));
                float Y = (float)(P.y + Radius * Math.Sin(Angle));

                return new Point(X, Y);
            }