Working Scaling Window

This commit is contained in:
EinEtw4s 2022-12-02 15:56:57 +01:00
parent c90a01d9f3
commit 0c60bce991
3 changed files with 217 additions and 53 deletions

View file

@ -31,33 +31,59 @@ partial class MainForm1
/// </summary> /// </summary>
private void InitializeComponent() private void InitializeComponent()
{ {
this.pictureBox1 = new Game_of_Life.PictureBoxWithInterpolationMode(); this.pictureBox = new Game_of_Life.PictureBoxWithInterpolationMode();
this.label1 = new System.Windows.Forms.Label(); this.debugLabel = new System.Windows.Forms.Label();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit(); 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(); this.SuspendLayout();
// //
// pictureBox1 // pictureBox
// //
this.pictureBox1.Dock = System.Windows.Forms.DockStyle.Fill; this.pictureBox.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBox1.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Default; this.pictureBox.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Default;
this.pictureBox1.Location = new System.Drawing.Point(0, 0); this.pictureBox.Location = new System.Drawing.Point(0, 0);
this.pictureBox1.Name = "pictureBox1"; this.pictureBox.Name = "pictureBox";
this.pictureBox1.Size = new System.Drawing.Size(800, 450); this.pictureBox.Size = new System.Drawing.Size(800, 450);
this.pictureBox1.TabIndex = 0; this.pictureBox.TabIndex = 0;
this.pictureBox1.TabStop = false; this.pictureBox.TabStop = false;
this.pictureBox1.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBox1_Paint); this.pictureBox.Click += new System.EventHandler(this.pictureBox_Click);
this.pictureBox1.MouseWheel += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseWheel); 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.debugLabel.AutoSize = true;
this.label1.BackColor = System.Drawing.Color.Transparent; this.debugLabel.BackColor = System.Drawing.Color.Transparent;
this.label1.ForeColor = System.Drawing.Color.AliceBlue; this.debugLabel.ForeColor = System.Drawing.Color.AliceBlue;
this.label1.Location = new System.Drawing.Point(12, 9); this.debugLabel.Location = new System.Drawing.Point(12, 9);
this.label1.Name = "label1"; this.debugLabel.Name = "debugLabel";
this.label1.Size = new System.Drawing.Size(27, 15); this.debugLabel.Size = new System.Drawing.Size(0, 15);
this.label1.TabIndex = 1; this.debugLabel.TabIndex = 1;
this.label1.Text = ""; //
// 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 // MainForm1
// //
@ -65,11 +91,14 @@ partial class MainForm1
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.Color.Black; this.BackColor = System.Drawing.Color.Black;
this.ClientSize = new System.Drawing.Size(800, 450); this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.label1); this.Controls.Add(this.buttonRepopulate);
this.Controls.Add(this.pictureBox1); this.Controls.Add(this.scalingSlider);
this.Controls.Add(this.debugLabel);
this.Controls.Add(this.pictureBox);
this.Name = "MainForm1"; this.Name = "MainForm1";
this.Text = "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.ResumeLayout(false);
this.PerformLayout(); this.PerformLayout();
@ -77,6 +106,8 @@ partial class MainForm1
#endregion #endregion
private PictureBoxWithInterpolationMode pictureBox1; private PictureBoxWithInterpolationMode pictureBox;
private Label label1; private Label debugLabel;
private TrackBar scalingSlider;
private Button buttonRepopulate;
} }

View file

@ -1,3 +1,4 @@
using System.ComponentModel;
using System.Drawing.Drawing2D; using System.Drawing.Drawing2D;
using System.Windows.Forms; using System.Windows.Forms;
using System.Windows.Forms.VisualStyles; using System.Windows.Forms.VisualStyles;
@ -8,48 +9,82 @@ public partial class MainForm1 : Form
{ {
private int scalingFactor = 10; private int scalingFactor = 10;
Pixelmap pixelmap; Pixelmap pixelmap;
private int height;
private int width;
public MainForm1() public MainForm1()
{ {
InitializeComponent(); InitializeComponent();
int height = 1000; height = 1800;
int width = 2000; width = 3200;
pixelmap = new Pixelmap(height, width, scalingFactor); pixelmap = new Pixelmap(height, width, scalingFactor);
Random rand = new Random(); 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)); 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) private void pictureBox1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{ {
int height = pictureBox1.Size.Height; int height = pictureBox.Size.Height;
int width = pictureBox1.Size.Width; 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; Graphics graphics = e.Graphics;
pictureBox1.SizeMode = PictureBoxSizeMode.Zoom; pictureBox.SizeMode = PictureBoxSizeMode.AutoSize;
pictureBox1.InterpolationMode = InterpolationMode.NearestNeighbor; pictureBox.InterpolationMode = InterpolationMode.NearestNeighbor;
pixelmap.paintOnGraphics(graphics); pixelmap.paintOnGraphics(graphics, width, height);
// Thread.Sleep(1000); // Thread.Sleep(1000);
} }
private void pictureBox1_MouseWheel(object sender, MouseEventArgs e) private void pictureBox1_MouseWheel(object sender, MouseEventArgs e)
{ {
scalingFactor += e.Delta/64; scalingFactor += e.Delta/16;
if (scalingFactor < 10) if (scalingFactor < 10)
{ {
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();
} }
}
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();
}
}

View file

@ -6,7 +6,9 @@ public class Pixelmap
private int width; private int width;
private Color[,] map; private Color[,] map;
private int scalingFactor; private int scalingFactor;
private int[] midPoint; private int[] midPoint;
private int viewWidth;
private int viewHeight;
public Pixelmap(int height, int width, int scalingFactor) public Pixelmap(int height, int width, int scalingFactor)
{ {
@ -25,6 +27,23 @@ public class Pixelmap
this.midPoint = new int[] { (width / 2), (height / 2) }; 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) public void setPixel(int x, int y, int val)
{ {
Color col = Color.Black;; Color col = Color.Black;;
@ -38,6 +57,56 @@ public class Pixelmap
map[x, y] = col; 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() public Bitmap ToBitmap()
{ {
Bitmap b = new Bitmap(width, height); Bitmap b = new Bitmap(width, height);
@ -53,30 +122,61 @@ public class Pixelmap
return b; return b;
} }
public Graphics paintOnGraphics(Graphics g) public Graphics paintOnGraphics(Graphics g, int viewWidth, int viewHeight)
{ {
Color penColor = Color.FromArgb(125, Color.Beige); Color penColor = Color.FromArgb(125, Color.Beige);
Pen linePen = new Pen(penColor, 1); 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) if (leftBound < 0)
{ {
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) if (rightBound > width)
{ {
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) if (upperBound < 0)
{ {
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) if (lowerBound > height)
{ {
lowerBound = height; lowerBound = height;
@ -84,16 +184,14 @@ public class Pixelmap
for (int w = leftBound; w < rightBound; w++) for (int w = leftBound; w < rightBound; w++)
{ {
// g.DrawLine(linePen, w * scalingFactor, 0, w * scalingFactor, height * scalingFactor);
for (int h = upperBound; h < lowerBound; h++) for (int h = upperBound; h < lowerBound; h++)
{ {
// g.DrawLine(linePen, 0, h * scalingFactor, width * scalingFactor, h * scalingFactor);
Color pixelColor = map[w, h]; Color pixelColor = map[w, h];
if (!pixelColor.Equals(Color.Black)) if (!pixelColor.Equals(Color.Black))
{ {
Brush pixelBrush = new SolidBrush(pixelColor); 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));
} }
} }
} }