private void BtnSave_Click(object sender, EventArgs e) { if (!Population.Cast <uint>().Any(u => u == 0) || MessageBox.Show("There are regions without recorded population values. Are you sure you want to save?", "Unset Values", MessageBoxButtons.YesNo) == DialogResult.Yes) { SaveFileDialog sfg = new SaveFileDialog(); sfg.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"; sfg.DefaultExt = ".txt"; sfg.AddExtension = true; if (sfg.ShowDialog() == DialogResult.OK) { FileStream fs = new FileStream(sfg.FileName, FileMode.Create); using (StreamWriter sw = new StreamWriter(fs)) { for (int i = 0; i < 13; i++) { sw.WriteLine(string.Format("Regions for province {0}", i)); for (int j = 0; j < 48; j++) { sw.WriteLine(string.Format(" Population for Region {0}: {1}", new object[] { j, Population[i, j] })); } } } } } ResetData(); }
public GameVertex(uint dimension, uint[,] state) { // Состояние игры - квадратное поле, в котором по одному разу встречаются все числа от нуля до длины стороны в квадрате if (dimension != state.GetLength(0) && dimension != state.GetLength(1)) { throw new ArgumentException(); } for (uint i = 0; i < dimension * dimension; i++) { if (state.Cast <uint>().Count(p => p == i) != 1) { throw new ArgumentException(); } } this.dimension = dimension; this.state = state; }
public static SKImage GenerateMaskFromValuesMatrix(uint[,] matrix, int width, int height, uint?overrideNormalizationMin, uint?overrideNormalizationMax) { var maxValue = overrideNormalizationMax ?? matrix.Cast <uint>().Max(); if (maxValue == 0) { return(null); } var minValue = overrideNormalizationMin ?? matrix.Cast <uint>().Where(x => x > 0).Min(); var steps = maxValue - minValue + 1; var pallete = GenerateGradient(new GradientStop[] { new GradientStop { Color = Color.FromArgb(122, 0, 0, 255), Position = 0 }, new GradientStop { Color = Color.FromArgb(255, 0, 0, 255), Position = 0.16 }, new GradientStop { Color = Color.Green, Position = 0.33 }, new GradientStop { Color = Color.Yellow, Position = 0.66 }, new GradientStop { Color = Color.Red, Position = 1 } }, steps); var skBitmap = new SKBitmap(width, height); var pixelsAddr = skBitmap.GetPixels(); unsafe { byte *ptr = (byte *)pixelsAddr.ToPointer(); for (int row = 0; row < height; row++) { for (int col = 0; col < width; col++) { var originalValue = matrix[row, col]; if (originalValue < minValue) { *ptr++ = 0; // blue *ptr++ = 0; // green *ptr++ = 0; // red *ptr++ = 0; // alpha } else { if (originalValue > maxValue) { originalValue = maxValue; } var gradientNumber = originalValue - minValue; var color = pallete[gradientNumber]; *ptr++ = color.B; // blue *ptr++ = color.G; // green *ptr++ = color.R; // red *ptr++ = color.A; // alpha } } } } var image = SKImage.FromBitmap(skBitmap); skBitmap.Dispose(); return(image); }