Exemplo n.º 1
0
        private void RenderLiveBitmap()
        {
            _bmpLive?.Dispose();
            _bmpLive = new Bitmap(pictureBox1.Width, pictureBox1.Height);
            using Graphics graphics = Graphics.FromImage(_bmpLive);
            graphics.FillRectangle(_backgroundBrush, 0, 0, _bmpLive.Width, _bmpLive.Height);
            foreach (var obstacle in Obstacles.Get())
            {
                graphics.FillRectangle(_obstacleBrush, obstacle.X, obstacle.Y, obstacle.Width, obstacle.Height);
            }

            graphics.FillEllipse(_goalBrush, _goal.X - 4, _goal.Y - 4, 8, 8);

            foreach (var dotPosition in _population.GetDotPositions())
            {
                if (dotPosition.IsBest)
                {
                    graphics.FillEllipse(_dotBrush, dotPosition.X - DotPosition.Radius, dotPosition.Y - DotPosition.Radius, DotPosition.Diameter, DotPosition.Diameter);
                }
                else
                {
                    graphics.DrawEllipse(_dotPen, dotPosition.X - DotPosition.Radius, dotPosition.Y - DotPosition.Radius, DotPosition.Diameter, DotPosition.Diameter);
                }
            }
        }
Exemplo n.º 2
0
        public void Update()
        {
            if (!_brain.HasDirections)
            {
                IsDead = true;
                return;
            }

            var acceleration = _brain.GetNextDirection();

            _velocity = _velocity.Add(acceleration);

            while (_velocity.HasValue())
            {
                if (IsDead || _reachedGoal)
                {
                    return;
                }

                if (!_brain.HasDirections && !_velocity.HasValue())
                {
                    IsDead = true;
                    return;
                }

                var(velocityX, velocityY) = _velocity.GetNext();
                _positionX += velocityX;
                _positionY += velocityY;

                if (DistanceToGoalSquared() <= DotPosition.Diameter)
                {
                    _reachedGoal = true;
                    IsDead       = true;
                    return;
                }

                var dotPosition = GetDotPosition();
                foreach (var obstacle in Obstacles.Get().Where(obstacle => obstacle.Hits(dotPosition)))
                {
                    _obstaclesHit++;
                    obstacle.UpdateVelocity(dotPosition, _velocity);
                }
            }
        }