Change Scaling to window

This commit is contained in:
EinEtw4s 2022-12-02 07:41:20 +01:00
parent 6caf74a154
commit c90a01d9f3
2 changed files with 60 additions and 39 deletions

View file

@ -7,46 +7,35 @@ namespace Game_of_Life;
public partial class MainForm1 : Form
{
private int scalingFactor = 10;
Pixelmap pixelmap;
public MainForm1()
{
InitializeComponent();
label1.Text = "Scaling Factor: " + scalingFactor.ToString() + $"\nDimensions: {pictureBox1.Size.Height.ToString()}x{pictureBox1.Size.Width.ToString()}";
/* this.Shown += (s, e) =>
{
while (true)
{
for (int i = 0; i < 20; i++)
{
pixelmap.setPixel(rand.Next(50), rand.Next(20), rand.Next(3));
}
pictureBox1.Image = pixelmap.ToBitmap();
Thread.Sleep(120);
}
};*/
}
private void pictureBox1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
Pixelmap pixelmap;
int height = pictureBox1.Size.Height;
int width = pictureBox1.Size.Width;
Graphics graphics = e.Graphics;
int height = 1000;
int width = 2000;
pixelmap = new Pixelmap(height, width, scalingFactor);
Random rand = new Random();
for (int i = 0; i < 20; i++)
for (int i = 0; i < 2000; i++)
{
pixelmap.setPixel(rand.Next(width / scalingFactor), rand.Next(height / scalingFactor), 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()}";
}
private void pictureBox1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
int height = pictureBox1.Size.Height;
int width = pictureBox1.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()}";
Graphics graphics = e.Graphics;
pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
pictureBox1.InterpolationMode = InterpolationMode.NearestNeighbor;
pixelmap.paintOnGraphics(graphics);
@ -55,12 +44,12 @@ public partial class MainForm1 : Form
private void pictureBox1_MouseWheel(object sender, MouseEventArgs e)
{
scalingFactor += e.Delta/32;
if (scalingFactor < 1)
scalingFactor += e.Delta/64;
if (scalingFactor < 10)
{
scalingFactor = 1;
scalingFactor = 10;
}
label1.Text = "Scaling Factor: " + scalingFactor.ToString() + $"\nDimensions: {pictureBox1.Size.Height.ToString()}x{pictureBox1.Size.Width.ToString()}";
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();
}
}

View file

@ -6,11 +6,12 @@ public class Pixelmap
private int width;
private Color[,] map;
private int scalingFactor;
private int[] midPoint;
public Pixelmap(int height, int width, int scalingFactor)
{
this.height = height / scalingFactor;
this.width = width / scalingFactor;
this.height = height;
this.width = width;
map = new Color[width, height];
for (int w = 0; w < width; w++)
{
@ -21,6 +22,7 @@ public class Pixelmap
}
this.scalingFactor = scalingFactor;
this.midPoint = new int[] { (width / 2), (height / 2) };
}
public void setPixel(int x, int y, int val)
@ -53,15 +55,45 @@ public class Pixelmap
public Graphics paintOnGraphics(Graphics g)
{
for (int w = 0; w < width; w++)
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));
if (leftBound < 0)
{
for (int h = 0; h < height; h++)
leftBound = 0;
}
int rightBound = (int)Math.Floor(midPoint[0] + ((double)borderWidth / (double)2));
if (rightBound > width)
{
rightBound = width;
}
int upperBound = (int)Math.Floor(midPoint[1] - ((double)borderHeight / (double)2));
if (upperBound < 0)
{
upperBound = 0;
}
int lowerBound = (int)Math.Floor(midPoint[1] + ((double)borderHeight / (double)2));
if (lowerBound > height)
{
lowerBound = height;
}
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((w*scalingFactor), (h * scalingFactor), scalingFactor, scalingFactor));
g.FillRectangle(pixelBrush, new Rectangle(((int)((double)w / ((double)1 / (double)scalingFactor))), ((int)((double)h / ((double)1 / (double)scalingFactor))), scalingFactor, scalingFactor));
}
}
}