public void DrawHexagonAbsolute(double x, double y, HexagonData data) { Polygon hexagon = new Polygon(); hexagon.Stroke = Brushes.Black; hexagon.Fill = Brushes.LightBlue; hexagon.StrokeThickness = 1; hexagon.Points = new PointCollection() { new Point(0, _hexagonSideLength / 2), new Point((Math.Sqrt(3) * _hexagonSideLength) / 2, 0), new Point(Math.Sqrt(3) * _hexagonSideLength, _hexagonSideLength / 2), new Point(Math.Sqrt(3) * _hexagonSideLength, 1.5 * _hexagonSideLength), new Point((Math.Sqrt(3) * _hexagonSideLength) / 2, 2 * _hexagonSideLength), new Point(0, 1.5 * _hexagonSideLength), }; hexagon.DataContext = data; Canvas.SetLeft(hexagon, x); Canvas.SetTop(hexagon, y); _canvas.Children.Add(hexagon); }
public void DrawHexagonOnGrid(int rowIndex, int colIndex) { double x; if (rowIndex % 2 == 0) // rowIndex is even { x = ((_hexagonSideLength * Math.Sqrt(3.0)) / 2.0) + (colIndex * _hexagonSideLength * Math.Sqrt(3.0)); } else // rowIndex is odd { x = (_hexagonSideLength * Math.Sqrt(3.0)) * (colIndex + 1); } double y = _hexagonSideLength + rowIndex * 1.5 * _hexagonSideLength; var distanceToMainAxis = Math.Abs(Math.Floor(_hexagonMaxLengthCount / 2.0) - rowIndex); var hexagonData = new HexagonData(rowIndex, colIndex, distanceToMainAxis); DrawHexagonAbsolute(x - ((_hexagonSideLength * Math.Sqrt(3.0)) / 2.0), y - _hexagonSideLength, hexagonData); }