Exemplo n.º 1
0
        public void ShouldNotMoveWhenNoVelocity()
        {
            var particle = new Particle(Vector2D.Zero);
            particle.Move(TimeSpan.FromSeconds(1));

            Assert.That(particle.Position, Is.EqualTo(Vector2D.Zero));
        }
Exemplo n.º 2
0
        public Vector2D GetAcceleration(Particle particle)
        {
            var distance = GetDistance(particle);
            if (distance < 1)
                distance = 1;

            return Force/distance;
        }
Exemplo n.º 3
0
        public void ShouldBump(double startVelocityX, double startVelocityY, double collisionPlaneX, double collistionPlaneY, double resultVelocityX, double resultVelocityY)
        {
            // given
            var particle = new Particle(Vector2D.Zero, new Vector2D(startVelocityX, startVelocityY));
            var collisionNormalPlane = new Vector2D(collisionPlaneX, collistionPlaneY);

            // when
            particle.Bump(collisionNormalPlane);

            // then
            Assert.That((particle.Velocity - new Vector2D(resultVelocityX, resultVelocityY)).GetLength() < 0.00000001);
        }
Exemplo n.º 4
0
        public void ShouldMoveWithConstantSpeedWhenNoAcceleration()
        {
            var particle = new Particle(Vector2D.Zero, new Vector2D(1, 0));
            particle.Move(TimeSpan.FromSeconds(1));

            Assert.That(particle.Position, Is.EqualTo(new Vector2D(1, 0)));
            Assert.That(particle.Velocity, Is.EqualTo(new Vector2D(1, 0)));

            particle.Move(TimeSpan.FromSeconds(1));

            Assert.That(particle.Position, Is.EqualTo(new Vector2D(2, 0)));
            Assert.That(particle.Velocity, Is.EqualTo(new Vector2D(1, 0)));
        }
Exemplo n.º 5
0
        public void ShouldAccelerate()
        {
            var particle = new Particle(Vector2D.Zero, Vector2D.Zero, new Vector2D(1, 0));

            particle.Move(TimeSpan.FromSeconds(1));

            Assert.That(particle.Position, Is.EqualTo(new Vector2D(1, 0)));
            Assert.That(particle.Velocity, Is.EqualTo(new Vector2D(1, 0)));

            particle.Move(TimeSpan.FromSeconds(1));

            Assert.That(particle.Position, Is.EqualTo(new Vector2D(3, 0)));
            Assert.That(particle.Velocity, Is.EqualTo(new Vector2D(2, 0)));
        }
Exemplo n.º 6
0
        private double GetDistance(Particle particle)
        {
            var p = particle.Position;

            var squaredLength = (End - Start).GetSquaredLength();

            var lengthToA = (p - Start).GetSquaredLength();
            var lengthToB = (p - End).GetSquaredLength();
            if (lengthToA > squaredLength || lengthToB > squaredLength)
                return Math.Min(lengthToA, lengthToB);

            var a = Start;
            var n = (End - Start).Normalize();
            var distance = ((a - p) - n * ((a - p).Dot(n))).GetLength();

            return distance;
        }
Exemplo n.º 7
0
 private void UpdatePosition(Shape ellipse, Particle particle)
 {
     if (particle.IsStopped())
         ellipse.Fill = _cholesterolBrush;
     ellipse.SetValue(Canvas.LeftProperty, particle.Position.X - _particleSize /2);
     ellipse.SetValue(Canvas.TopProperty, particle.Position.Y - _particleSize / 2);
 }
Exemplo n.º 8
0
        void dispatcherTimer_Tick(object sender, EventArgs e)
        {
            _dispatcherTimer.Stop();
            _world.Move(TimeSpan.FromMilliseconds(5));

            var toRemove = new Collection<Model.Particle>();

            // find all particles that need to be removed
            foreach (var particle in _world.Particles)
            {
                if (particle.Position.X > canvas.ActualWidth || particle.Position.X < 0 || particle.Position.Y > canvas.ActualHeight || particle.Position.Y < 0)
                    toRemove.Add(particle);
            }

            foreach (var particle in toRemove)
            {
                _world.RemoveParticle(particle);
                canvas.Children.Remove(_visualizationMap[particle]);
                _visualizationMap.Remove(particle);
            }

            // generate new particles
            if ((DateTime.Now - _lastGenerated) > TimeSpan.FromMilliseconds(10))
            {
                int count = _randomGenerator.Next(2, 5);
                for (int i = 0; i < count; i++)
                {
                    var sx = _randomGenerator.Next(200, 390);
                    var sy = 300;
                    var vx = 0;
                    var vy = _randomGenerator.Next(-150, -75);

                    var particle = new Particle(new Vector2D(sx, sy), new Vector2D(vx, vy));
                    _world.AddParticle(particle);

                    var ellipse = CreateParticleVisualisation(particle);
                    _visualizationMap.Add(particle, ellipse);
                }
                _lastGenerated = DateTime.Now;
            }

            // find all particles that needs to be added
            foreach (var particle in _world.Particles.Where(p => !_visualizationMap.ContainsKey(p)).ToArray())
            {
                var ellipse = CreateParticleVisualisation(particle);
                _visualizationMap.Add(particle, ellipse);
            }

            // update position of each particle visualisation
            foreach (var particle in _world.Particles)
            {
                UpdatePosition(_visualizationMap[particle], particle);
            }

            _dispatcherTimer.Start();
        }
Exemplo n.º 9
0
        private Ellipse CreateParticleVisualisation(Particle particle)
        {
            var ellipse = new Ellipse();

            ellipse.Fill = _bloodBrush;
            ellipse.Width = _particleSize;
            ellipse.Height = _particleSize;

            UpdatePosition(ellipse, particle);

            canvas.Children.Add(ellipse);
            return ellipse;
        }
Exemplo n.º 10
0
 public void RemoveParticle(Particle particle)
 {
     Particles.Remove(particle);
 }
Exemplo n.º 11
0
 public void AddParticle(Particle particle)
 {
     Particles.Add(particle);
 }
Exemplo n.º 12
0
        public bool IsInRange(Particle particle)
        {
            var distance = GetDistance(particle);

            return distance < Range;
        }