コード例 #1
0
        public void DoubleEventTest()
        {
            DoubleEvent e       = new DoubleEvent();
            double      counter = 0;

            e.AddListener((val) => counter += 1.0 + val);
            Assert.AreEqual(counter, 0);
            e.Invoke(1.5);
            Assert.AreEqual(counter, 2.5);
            e.Invoke(2.0);
            e.Invoke(0.5);
            Assert.AreEqual(counter, 7.0);
        }
コード例 #2
0
    public void SimulationStep(double deltaTime)
    {
        var   rad2          = radius * radius;
        Vec3D sumVelocities = new Vec3D();
        // Lex sort
        var birds = _birds.OrderBy(b => b.particlePosition.x)
                    .ThenBy(b => b.particlePosition.y)
                    .ThenBy(b => b.particlePosition.y)
                    .ToArray();

        for (var i = 0; i < birds.Length; ++i)
        {
            var bird = birds[i];

            double avgAlpha      = bird.alpha;
            double avgTheta      = bird.theta;
            int    neighborCount = 1;
            for (var k = 1;
                 Distance2PBC(bird.particlePosition, birds[(i + k) % birds.Length].particlePosition) <= rad2;
                 k++)
            {
                avgAlpha      += birds[(i + k) % birds.Length].alpha;
                avgTheta      += birds[(i + k) % birds.Length].theta;
                neighborCount += 1;
            }

            for (var k = 1;
                 Distance2PBC(bird.particlePosition,
                              birds[((birds.Length + i - k) % birds.Length) % birds.Length].particlePosition) <= rad2;
                 k++)
            {
                avgAlpha      += birds[((birds.Length + i - k) % birds.Length) % birds.Length].alpha;
                avgTheta      += birds[((birds.Length + i - k) % birds.Length) % birds.Length].theta;
                neighborCount += 1;
            }

            avgAlpha = (avgAlpha / neighborCount) % (2 * Math.PI);
            avgTheta = (avgTheta / neighborCount) % (2 * Math.PI);

            var noiseAlpha = RandomGaussian(0, 0.1);
            var noiseTheta = RandomGaussian(0, 0.1);
//
//            var noiseAlpha = (_rng.NextDouble() * 2 * Math.PI) - Math.PI;
//            var noiseTheta = (_rng.NextDouble() * 2 * Math.PI) - Math.PI;

            var alpha    = avgAlpha + noiseIntensity * noiseAlpha;
            var theta    = avgTheta + noiseIntensity * noiseTheta;
            var sinAlpha = Math.Sin(alpha);
            var cosAlpha = Math.Cos(alpha);
            var sinTheta = Math.Sin(theta);
            var cosTheta = Math.Cos(theta);

            var velocity = new Vec3D(cosTheta * sinAlpha, sinTheta * sinAlpha, cosAlpha);
            var position = bird.particlePosition.Add(velocity.Times(deltaTime * simulationSpeed));

            bird.newParticlePosition = PositionPBC(position);
            bird.newParticleVelocity = velocity;
            bird.newAlpha            = alpha;
            bird.newTheta            = theta;

            sumVelocities = sumVelocities.Add(velocity);
        }

        foreach (var b in birds)
        {
            b.ApplyNewValues();
        }

        var orderCoeff = sumVelocities.Length() / _birds.Count;

        OrderCoefficientChanged.Invoke(orderCoeff);
    }