public FormAdmin() { InitializeComponent(); var dpiGraphics = Graphics.FromHwnd(IntPtr.Zero); AutoScaleDimensions = new SizeF(dpiGraphics.DpiX, dpiGraphics.DpiX); AutoScaleMode = AutoScaleMode.Dpi; dpiGraphics.Dispose(); MessageBox.Show(((HexMathHelper.GetLayerSumHexagonsCount(63) - 1) * 6).ToString()); }
public static float GetHexagonRadiusFromImage(Bitmap bitmap, float distanceBetweenHexes) { var width = bitmap.Width; var height = bitmap.Height; var centerX = width / 2f; var centerY = height / 2f; var centerHexagonCenter = new PointF(centerX, centerY); var centerBinaryStringBuilder = new StringBuilder(); var drawingR = HexMathHelper.GetDrawingR(MinRadius, 0f); var centerHexagonPixels = MathHelper.GetPixelsFromCenter(centerHexagonCenter, drawingR); for (int i = centerHexagonPixels.Count - 1; i >= 0; i--) { var value = bitmap.GetPixel((int)centerHexagonPixels[i].X, (int)centerHexagonPixels[i].Y); if (IsPixelBlack(value)) //black { centerBinaryStringBuilder.Append('1'); } else { centerBinaryStringBuilder.Append('0'); } } var metaData = BinaryHelper.ToBoolArray(Reverse(centerBinaryStringBuilder.ToString())); var useLargeAlphabet = metaData[0]; var imageActiveLayer = 0; for (int i = metaData.Length - 1; i >= 1; i--) { imageActiveLayer += (metaData[i] ? 1 : 0) * (int)Math.Pow(2, metaData.Length - i - 1); } //во всю ширину картинки попадает (3 * R * layer + 2 * R) пикселей //dx = 1.5f * R * layer + R //width = 3f * drawingR * layer + 2 * R //var layerWidth = GetWidth(imageActiveLayer, 20f, 0f); //var trueRadius = ((layerWidth) / (3f * imageActiveLayer + 2)); var imageRadius = ((width) / (3f * imageActiveLayer + 2)); return(imageRadius); }
//precision [0..255] public string ParseCorrectBitmap(Bitmap bitmap) { var drawingR = HexMathHelper.GetDrawingR(_r, _distanceBetweenHexes); var width = bitmap.Width; var height = bitmap.Height; var centerPoint = new PointF(width / 2f, height / 2f); var centerHexagonDataString = ParseHexagon(bitmap, centerPoint, drawingR); var metaData = BinaryHelper.ToBoolArray(Reverse(centerHexagonDataString)); var useLargeAlphabet = metaData[0]; _activeAlphabet = metaData[0] ? _largeAlphabet : _alphabet; var imageActiveLayer = 0; for (int i = metaData.Length - 1; i >= 1; i--) { imageActiveLayer += (metaData[i] ? 1 : 0) * (int)Math.Pow(2, metaData.Length - i - 1); } var sumHexagons = HexMathHelper.GetLayerSumHexagonsCount(imageActiveLayer) - 1; var binaryStringBuilder = new StringBuilder(sumHexagons * 6); for (int index = sumHexagons - 1; index >= 0; index--) { var position = index; //индекс символа в текущем слое var currentLayer = 1; //вычисляем слой буквы HexMathHelper.GetItemLayer(ref position, ref currentLayer); var hexesInSide = HexMathHelper.GetHexagonsInSide(currentLayer); var hexagonPoint = centerPoint; HexMathHelper.TranlateToTopRightCorner(ref hexagonPoint, drawingR, currentLayer); HexMathHelper.TranslatePointAroundHexagon(ref hexagonPoint, position, hexesInSide, drawingR); var hexagonDataString = ParseHexagon(bitmap, hexagonPoint, drawingR); binaryStringBuilder.Append(hexagonDataString); } var binaryString = Reverse(binaryStringBuilder.ToString()); return(FromBinaryString(binaryString, useLargeAlphabet)); }
/// <summary> /// Функция возвращает суммарную высоту картинки зная радиус и кол-во слоёв /// </summary> /// <param name="layer">Количество слоёв</param> /// <param name="radius">Радиус шестиугольника</param> /// <param name="distanceBetweenHexes">Расстояние между шестиугольниками</param> /// <returns></returns> public static float GetHeight(int layer, float radius, float distanceBetweenHexes) { return((float)Math.Ceiling((2 * layer + 1) * MathHelper.Sqrt3 * HexMathHelper.GetDrawingR(radius, distanceBetweenHexes))); }
public Bitmap GenerateBitmap(string content) { if (content.Length == 0) { throw new IndexOutOfRangeException("Content Length 0"); } //если хотя бы 1 символ в обычном алфавите отсутствует var useLargeAlphabet = content.Any(c => !_alphabet.Contains(c)); //длина бинарного символа (кол-во бит на символ) var binarySymbolLength = GetAlphabetBinaryDigits(useLargeAlphabet); var sb = new StringBuilder(content.Length * binarySymbolLength); _activeAlphabet = useLargeAlphabet ? _largeAlphabet : _alphabet; foreach (var t in content) { sb.Append(BinaryHelper.ToBinaryString(Array.IndexOf(_activeAlphabet, t), binarySymbolLength)); } var binaryString = sb.ToString(); if (binaryString.Length > HexMathHelper.GetLayerSumHexagonsCount(32) * 6) { throw new IndexOutOfRangeException("Message is too long"); } var binaryPieces = BinaryHelper.DivideBy6(binaryString); var maxUsedIndex = binaryPieces.Count - 1; var maxUsedLayer = 0; HexMathHelper.GetItemLayer(ref maxUsedIndex, ref maxUsedLayer); var drawingR = HexMathHelper.GetDrawingR(_r, _distanceBetweenHexes); var width = HexMathHelper.GetWidth(maxUsedLayer, _r, _distanceBetweenHexes) + 1; var height = HexMathHelper.GetHeight(maxUsedLayer, _r, _distanceBetweenHexes) + 1; var bitmap = new Bitmap((int)width, (int)height); var graphics = Graphics.FromImage(bitmap); graphics.FillRectangle(Brushes.White, 0, 0, width, height); var maxUsedLayerBinaryString = Convert.ToString(maxUsedLayer, 2); maxUsedLayerBinaryString = maxUsedLayerBinaryString.PadLeft(5, '0'); var maxUserLayerBoolArray = BinaryHelper.ToBoolArray(maxUsedLayerBinaryString); var centerPoint = new PointF(width / 2f, height / 2f); var metaData = new bool[6]; metaData[0] = useLargeAlphabet; for (int i = 0; i < maxUserLayerBoolArray.Length; i++) { metaData[i + 1] = maxUserLayerBoolArray[i]; } //graphics.DrawLine(Pens.DarkSlateGray, centerX, 0, centerX, height); //graphics.DrawLine(Pens.DarkSlateGray, 0, centerY, width, centerY); DrawHexagon(centerPoint, _r, graphics, metaData); for (var i = 0; i < HexMathHelper.GetLayerSumHexagonsCount(maxUsedLayer) - 1; i++) { bool[] binaryPiece; if (i < binaryPieces.Count) { binaryPiece = BinaryHelper.ToBoolArray(binaryPieces[i]); } else { binaryPiece = new[] { false, false, false, false, false, false }; } var position = i; //индекс символа в текущем слое var currentLayer = 1; //вычисляем слой буквы HexMathHelper.GetItemLayer(ref position, ref currentLayer); var hexesInSide = HexMathHelper.GetHexagonsInSide(currentLayer); var hexagonPoint = centerPoint; HexMathHelper.TranlateToTopRightCorner(ref hexagonPoint, drawingR, currentLayer); HexMathHelper.TranslatePointAroundHexagon(ref hexagonPoint, position, hexesInSide, drawingR); //рисуем гексагон DrawHexagon(hexagonPoint, _r, graphics, binaryPiece); } graphics.Dispose(); return(bitmap); }