From 0c60bce991fe3c862718adcd1240b069261c7281 Mon Sep 17 00:00:00 2001 From: EinEtw4s Date: Fri, 2 Dec 2022 15:56:57 +0100 Subject: [PATCH] Working Scaling Window --- Game_of_Life/MainForm1.Designer.cs | 85 +++++++++++++------- Game_of_Life/MainForm1.cs | 63 +++++++++++---- Game_of_Life/Pixelmap.cs | 122 ++++++++++++++++++++++++++--- 3 files changed, 217 insertions(+), 53 deletions(-) diff --git a/Game_of_Life/MainForm1.Designer.cs b/Game_of_Life/MainForm1.Designer.cs index 67f9c6a..7b9dc6e 100644 --- a/Game_of_Life/MainForm1.Designer.cs +++ b/Game_of_Life/MainForm1.Designer.cs @@ -31,33 +31,59 @@ partial class MainForm1 /// private void InitializeComponent() { - this.pictureBox1 = new Game_of_Life.PictureBoxWithInterpolationMode(); - this.label1 = new System.Windows.Forms.Label(); - ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit(); + this.pictureBox = new Game_of_Life.PictureBoxWithInterpolationMode(); + this.debugLabel = new System.Windows.Forms.Label(); + this.scalingSlider = new System.Windows.Forms.TrackBar(); + this.buttonRepopulate = new System.Windows.Forms.Button(); + ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit(); + ((System.ComponentModel.ISupportInitialize)(this.scalingSlider)).BeginInit(); this.SuspendLayout(); // - // pictureBox1 + // pictureBox // - this.pictureBox1.Dock = System.Windows.Forms.DockStyle.Fill; - this.pictureBox1.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Default; - this.pictureBox1.Location = new System.Drawing.Point(0, 0); - this.pictureBox1.Name = "pictureBox1"; - this.pictureBox1.Size = new System.Drawing.Size(800, 450); - this.pictureBox1.TabIndex = 0; - this.pictureBox1.TabStop = false; - this.pictureBox1.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBox1_Paint); - this.pictureBox1.MouseWheel += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseWheel); + this.pictureBox.Dock = System.Windows.Forms.DockStyle.Fill; + this.pictureBox.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Default; + this.pictureBox.Location = new System.Drawing.Point(0, 0); + this.pictureBox.Name = "pictureBox"; + this.pictureBox.Size = new System.Drawing.Size(800, 450); + this.pictureBox.TabIndex = 0; + this.pictureBox.TabStop = false; + this.pictureBox.Click += new System.EventHandler(this.pictureBox_Click); + this.pictureBox.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBox1_Paint); + this.pictureBox.MouseWheel += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseWheel); // - // label1 + // debugLabel // - this.label1.AutoSize = true; - this.label1.BackColor = System.Drawing.Color.Transparent; - this.label1.ForeColor = System.Drawing.Color.AliceBlue; - this.label1.Location = new System.Drawing.Point(12, 9); - this.label1.Name = "label1"; - this.label1.Size = new System.Drawing.Size(27, 15); - this.label1.TabIndex = 1; - this.label1.Text = ""; + this.debugLabel.AutoSize = true; + this.debugLabel.BackColor = System.Drawing.Color.Transparent; + this.debugLabel.ForeColor = System.Drawing.Color.AliceBlue; + this.debugLabel.Location = new System.Drawing.Point(12, 9); + this.debugLabel.Name = "debugLabel"; + this.debugLabel.Size = new System.Drawing.Size(0, 15); + this.debugLabel.TabIndex = 1; + // + // scalingSlider + // + this.scalingSlider.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); + this.scalingSlider.Location = new System.Drawing.Point(12, 405); + this.scalingSlider.Maximum = 500; + this.scalingSlider.Minimum = 10; + this.scalingSlider.Name = "scalingSlider"; + this.scalingSlider.Size = new System.Drawing.Size(178, 45); + this.scalingSlider.TabIndex = 2; + this.scalingSlider.Value = 10; + this.scalingSlider.ValueChanged += new System.EventHandler(this.scalingSlider_Changed); + // + // buttonRepopulate + // + this.buttonRepopulate.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); + this.buttonRepopulate.Location = new System.Drawing.Point(701, 12); + this.buttonRepopulate.Name = "buttonRepopulate"; + this.buttonRepopulate.Size = new System.Drawing.Size(87, 23); + this.buttonRepopulate.TabIndex = 3; + this.buttonRepopulate.Text = "REPOPULATE"; + this.buttonRepopulate.UseVisualStyleBackColor = true; + this.buttonRepopulate.Click += new System.EventHandler(this.buttonRepopulate_Click); // // MainForm1 // @@ -65,11 +91,14 @@ partial class MainForm1 this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.BackColor = System.Drawing.Color.Black; this.ClientSize = new System.Drawing.Size(800, 450); - this.Controls.Add(this.label1); - this.Controls.Add(this.pictureBox1); + this.Controls.Add(this.buttonRepopulate); + this.Controls.Add(this.scalingSlider); + this.Controls.Add(this.debugLabel); + this.Controls.Add(this.pictureBox); this.Name = "MainForm1"; this.Text = "MainForm1"; - ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit(); + ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit(); + ((System.ComponentModel.ISupportInitialize)(this.scalingSlider)).EndInit(); this.ResumeLayout(false); this.PerformLayout(); @@ -77,6 +106,8 @@ partial class MainForm1 #endregion - private PictureBoxWithInterpolationMode pictureBox1; - private Label label1; + private PictureBoxWithInterpolationMode pictureBox; + private Label debugLabel; + private TrackBar scalingSlider; + private Button buttonRepopulate; } \ No newline at end of file diff --git a/Game_of_Life/MainForm1.cs b/Game_of_Life/MainForm1.cs index 48bac6a..b2a71b6 100644 --- a/Game_of_Life/MainForm1.cs +++ b/Game_of_Life/MainForm1.cs @@ -1,3 +1,4 @@ +using System.ComponentModel; using System.Drawing.Drawing2D; using System.Windows.Forms; using System.Windows.Forms.VisualStyles; @@ -8,48 +9,82 @@ public partial class MainForm1 : Form { private int scalingFactor = 10; Pixelmap pixelmap; + private int height; + private int width; public MainForm1() { InitializeComponent(); - int height = 1000; - int width = 2000; + height = 1800; + width = 3200; pixelmap = new Pixelmap(height, width, scalingFactor); Random rand = new Random(); - for (int i = 0; i < 2000; i++) + for (int i = 0; i < 4000; i++) { pixelmap.setPixel(rand.Next(width), rand.Next(height), rand.Next(3)); } - label1.Text = $"Scaling Factor: {scalingFactor.ToString()}\n{((double)1 / (double)scalingFactor)} - Dimensions: {Math.Floor(pictureBox1.Size.Height * ((double)10 / (double)scalingFactor)).ToString()}x{Math.Floor(pictureBox1.Size.Width * ((double)10 / (double)scalingFactor)).ToString()}"; + debugLabel.Text = $"Scaling Factor: {scalingFactor.ToString()}\n{((double)10 / (double)scalingFactor)} - Dimensions: {Math.Floor(pictureBox.Size.Height * ((double)10 / (double)scalingFactor)).ToString()}x{Math.Floor(pictureBox.Size.Width * ((double)10 / (double)scalingFactor)).ToString()}"; } private void pictureBox1_Paint(object sender, System.Windows.Forms.PaintEventArgs e) { - int height = pictureBox1.Size.Height; - int width = pictureBox1.Size.Width; + int height = pictureBox.Size.Height; + int width = pictureBox.Size.Width; - label1.Text = $"Scaling Factor: {scalingFactor.ToString()}\n{((double)1 / (double)scalingFactor)} - Dimensions: {Math.Floor(height * ((double)10 / (double)scalingFactor)).ToString()}x{Math.Floor(width * ((double)10 / (double)scalingFactor)).ToString()}"; + debugLabel.Text = $"Scaling Factor: {scalingFactor.ToString()}\n{((double)10 / (double)scalingFactor)} - Dimensions: {Math.Floor(height * ((double)10 / (double)scalingFactor)).ToString()}x{Math.Floor(width * ((double)10 / (double)scalingFactor)).ToString()}"; Graphics graphics = e.Graphics; - pictureBox1.SizeMode = PictureBoxSizeMode.Zoom; - pictureBox1.InterpolationMode = InterpolationMode.NearestNeighbor; - pixelmap.paintOnGraphics(graphics); + pictureBox.SizeMode = PictureBoxSizeMode.AutoSize; + pictureBox.InterpolationMode = InterpolationMode.NearestNeighbor; + pixelmap.paintOnGraphics(graphics, width, height); // Thread.Sleep(1000); } private void pictureBox1_MouseWheel(object sender, MouseEventArgs e) { - scalingFactor += e.Delta/64; + scalingFactor += e.Delta/16; if (scalingFactor < 10) { scalingFactor = 10; + } else if (scalingFactor > 500) + { + scalingFactor = 500; } - label1.Text = $"Scaling Factor: {scalingFactor.ToString()}\n{((double)1 / (double)scalingFactor)} - Dimensions: {Math.Floor(pictureBox1.Size.Height * ((double)10 / (double)scalingFactor)).ToString()}x{Math.Floor(pictureBox1.Size.Width * ((double)10 / (double)scalingFactor)).ToString()}"; - pictureBox1.Refresh(); + + scalingSlider.Value = scalingFactor; + pixelmap.setScalingFactor(scalingFactor); + debugLabel.Text = $"Scaling Factor: {scalingFactor.ToString()}\n{((double)10 / (double)scalingFactor)} - Dimensions: {Math.Floor(pictureBox.Size.Height * ((double)10 / (double)scalingFactor)).ToString()}x{Math.Floor(pictureBox.Size.Width * ((double)10 / (double)scalingFactor)).ToString()}"; + pictureBox.Refresh(); } -} \ No newline at end of file + + private void scalingSlider_Changed(object sender, EventArgs e) + { + scalingFactor = scalingSlider.Value; + pixelmap.setScalingFactor(scalingFactor); + debugLabel.Text = $"Scaling Factor: {scalingFactor.ToString()}\n{((double)10 / (double)scalingFactor)} - Dimensions: {Math.Floor(pictureBox.Size.Height * ((double)10 / (double)scalingFactor)).ToString()}x{Math.Floor(pictureBox.Size.Width * ((double)10 / (double)scalingFactor)).ToString()}"; + pictureBox.Refresh(); + } + + private void buttonRepopulate_Click(object sender, EventArgs e) + { + pixelmap.clear(); + Random rand = new Random(); + + for (int i = 0; i < 4000; i++) + { + pixelmap.setPixel(rand.Next(width), rand.Next(height), rand.Next(3)); + } + pictureBox.Refresh(); + } + + private void pictureBox_Click(object sender, EventArgs e) + { + pixelmap.cyclePixel(MousePosition.X, MousePosition.Y); + pictureBox.Refresh(); + } +} diff --git a/Game_of_Life/Pixelmap.cs b/Game_of_Life/Pixelmap.cs index 2dce120..eca3124 100644 --- a/Game_of_Life/Pixelmap.cs +++ b/Game_of_Life/Pixelmap.cs @@ -6,7 +6,9 @@ public class Pixelmap private int width; private Color[,] map; private int scalingFactor; - private int[] midPoint; + private int[] midPoint; + private int viewWidth; + private int viewHeight; public Pixelmap(int height, int width, int scalingFactor) { @@ -25,6 +27,23 @@ public class Pixelmap this.midPoint = new int[] { (width / 2), (height / 2) }; } + public void setScalingFactor(int scalingFactor) + { + this.scalingFactor = scalingFactor; + } + + public void clear() + { + map = new Color[width, height]; + for (int w = 0; w < width; w++) + { + for (int h = 0; h < height; h++) + { + map[w, h] = Color.Black; + } + } + } + public void setPixel(int x, int y, int val) { Color col = Color.Black;; @@ -38,6 +57,56 @@ public class Pixelmap map[x, y] = col; } + public void cyclePixel(int x, int y) + { + float zoomFactor = ((float)10 / (float)scalingFactor); + float aspectRatio = (float)viewHeight / (float)viewWidth; + + int borderWidth = (int)Math.Ceiling((float)width * zoomFactor); + int borderHeight = (int)Math.Ceiling((float)width * zoomFactor * aspectRatio); + + float scaleWidth = (float)viewWidth / (float)borderWidth; + float scaleHeight = (float)viewHeight / (float)borderHeight; + + + int leftBound = (int)Math.Floor(midPoint[0] - ((float)borderWidth / (float)2)); + if (leftBound < 0) + { + leftBound = 0; + } + + int upperBound = (int)Math.Floor(midPoint[1] - ((float)borderHeight / (float)2)); + if (upperBound < 0) + { + upperBound = 0; + } + + int scalePixelWidth; + if (scaleWidth < 1) + { + scalePixelWidth = 1; + } + else + { + scalePixelWidth = (int)scaleWidth; + } + + int scalePixelHeight; + if (scaleHeight < 1) + { + scalePixelHeight = 1; + } + else + { + scalePixelHeight = (int)scaleHeight; + } + + float xTrans = (float)x / scaleWidth; + float yTrans = (float)y / scaleHeight; + + setPixel((int)(xTrans + leftBound), (int)(yTrans + upperBound), 1); + } + public Bitmap ToBitmap() { Bitmap b = new Bitmap(width, height); @@ -53,30 +122,61 @@ public class Pixelmap return b; } - public Graphics paintOnGraphics(Graphics g) + public Graphics paintOnGraphics(Graphics g, int viewWidth, int viewHeight) { Color penColor = Color.FromArgb(125, Color.Beige); Pen linePen = new Pen(penColor, 1); - int borderWidth = (int)Math.Floor((double)width * ((double)10 / (double)scalingFactor)); - int borderHeight = (int)Math.Floor((double)height * ((double)10 / (double)scalingFactor)); - int leftBound = (int)Math.Floor(midPoint[0] - ((double)borderWidth / (double)2)); + this.viewHeight = viewHeight; + this.viewWidth = viewWidth; + + float zoomFactor = ((float)10 / (float)scalingFactor); + float aspectRatio = (float)viewHeight / (float)viewWidth; + + int borderWidth = (int)Math.Ceiling((float)width * zoomFactor); + int borderHeight = (int)Math.Ceiling((float)width * zoomFactor * aspectRatio); + + float scaleWidth = (float)viewWidth / (float)borderWidth; + float scaleHeight = (float)viewHeight / (float)borderHeight; + + int scalePixelWidth; + if (scaleWidth < 1) + { + scalePixelWidth = 1; + } + else + { + scalePixelWidth = (int)Math.Ceiling(scaleWidth); + } + + int scalePixelHeight; + if (scaleHeight < 1) + { + scalePixelHeight = 1; + } + else + { + scalePixelHeight = (int)Math.Ceiling(scaleHeight); + } + + + int leftBound = (int)Math.Ceiling(midPoint[0] - ((float)borderWidth / (float)2)); if (leftBound < 0) { leftBound = 0; } - int rightBound = (int)Math.Floor(midPoint[0] + ((double)borderWidth / (double)2)); + int rightBound = (int)Math.Ceiling(midPoint[0] + ((float)borderWidth / (float)2)); if (rightBound > width) { rightBound = width; } - int upperBound = (int)Math.Floor(midPoint[1] - ((double)borderHeight / (double)2)); + int upperBound = (int)Math.Ceiling(midPoint[1] - ((float)borderHeight / (float)2)); if (upperBound < 0) { upperBound = 0; } - int lowerBound = (int)Math.Floor(midPoint[1] + ((double)borderHeight / (double)2)); + int lowerBound = (int)Math.Ceiling(midPoint[1] + ((float)borderHeight / (float)2)); if (lowerBound > height) { lowerBound = height; @@ -84,16 +184,14 @@ public class Pixelmap for (int w = leftBound; w < rightBound; w++) { - // g.DrawLine(linePen, w * scalingFactor, 0, w * scalingFactor, height * scalingFactor); - for (int h = upperBound; h < lowerBound; h++) { - // g.DrawLine(linePen, 0, h * scalingFactor, width * scalingFactor, h * scalingFactor); Color pixelColor = map[w, h]; if (!pixelColor.Equals(Color.Black)) { Brush pixelBrush = new SolidBrush(pixelColor); - g.FillRectangle(pixelBrush, new Rectangle(((int)((double)w / ((double)1 / (double)scalingFactor))), ((int)((double)h / ((double)1 / (double)scalingFactor))), scalingFactor, scalingFactor)); + g.FillRectangle(pixelBrush, + new Rectangle((int)(((float)w - (float)leftBound) * scaleHeight), (int)(((float)h - (float)upperBound) * scaleHeight), (int)scalePixelWidth, (int)scalePixelHeight)); } } }