private void PaintView() { if (_initializing) { return; } Graphics g = Graphics.FromImage(_image); g.FillRectangle(_brushBackground, 0, 0, _image.Width, _image.Height); g.SmoothingMode = SmoothingMode.AntiAlias; // Get control width and height. int width = Width; int height = Height; // Determine smallest dimension. Use that as the edge length of the square grid. width = height = Math.Min(width, height); // Pixel size is calculated using integer division to produce cleaner lines when drawing. // The inherent rounding down may produce a grid 1 pixel smaller then the determined edge length. // Also make room for a button above the grid (next test case button). int visualFieldPixelSize = (height - GridTop) / _world.GridSize; width = height = visualFieldPixelSize * _world.GridSize; // Paint pixel outline grid. // Vertical lines. int xg = GridLeft; for (int i = 0; i <= _world.GridSize; i++, xg += visualFieldPixelSize) { g.DrawLine(__penGrey, xg, GridTop, xg, GridTop + height); } // Horizontal lines. int yg = GridTop; for (int i = 0; i <= _world.GridSize; i++, yg += visualFieldPixelSize) { g.DrawLine(__penGrey, GridLeft, yg, GridLeft + width, yg); } // Paint grid squares. Background color. Brush sensorBrush = _world.IsPreyCaptured() ? _brushBackgroundSensorCaptured : _brushBackgroundSensor; yg = GridTop; for (int y = 0; y < _world.GridSize; y++, yg += visualFieldPixelSize) { xg = GridLeft; for (int x = 0; x < _world.GridSize; x++, xg += visualFieldPixelSize) { // Calc distance of sqaure from agent. if (IntPoint.CalculateDistance(_world.AgentPosition, x, y) <= _world.SensorRange) { g.FillRectangle(sensorBrush, xg + 1, yg + 1, visualFieldPixelSize - 2, visualFieldPixelSize - 2); } } } // Paint agent and prey squares. IntPoint a = _world.AgentPosition; IntPoint p = _world.PreyPosition; g.FillRectangle(_brushAgent, GridLeft + (a._x * visualFieldPixelSize) + 1, GridTop + (a._y * visualFieldPixelSize) + 1, visualFieldPixelSize - 2, visualFieldPixelSize - 2); g.FillRectangle(_brushPrey, GridLeft + (p._x * visualFieldPixelSize) + 1, GridTop + (p._y * visualFieldPixelSize) + 1, visualFieldPixelSize - 2, visualFieldPixelSize - 2); Refresh(); }