public ArraySubblock ToArraySubblock(ReduceSurfaceAreaByReordering sorter) { var translaterow = Enumerable.Range(this.Row, this.RowHeight).Select(s => sorter.Rows[s]); var translatecol = Enumerable.Range(this.Col, this.ColWidth).Select(s => sorter.Columns[s]); return(new ArraySubblock(translaterow, translatecol) { Row = this.Row, Col = this.Col }); }
public BiggestContiguousBlock(ReduceSurfaceAreaByReordering reordering) { this.Sorter = reordering; this.Area = reordering.Area; }
private void run() { Thread.Sleep(1000); for (int j = 1; j < 30; j++) { Bitmap bmp = new Bitmap(250, 200, System.Drawing.Imaging.PixelFormat.Format32bppArgb); Bitmap clone = null; this.Invoke(new MethodInvoker(delegate(){ clone = Clone(bmp); this.Text = "Making a new request. Image is all black. User thinks all distances need to be asked from Google."; this.pictureBox1.Image = clone; })); bool[,] area = new bool[200, 250]; Random rnd = new Random(Environment.TickCount); for (int i = 1; i < j; i++) { var x1 = rnd.Next(250); var x2 = rnd.Next(250); var y1 = rnd.Next(200); var y2 = rnd.Next(200); Rectangle rect = new Rectangle(Math.Min(x1, x2), Math.Min(y1, y2), Math.Abs(x1 - x2), Math.Abs(y1 - y2)); for (int y = rect.Y; y < rect.Bottom; y++) { for (int x = rect.X; x < rect.Right; x++) { area[y, x] = true; bmp.SetPixel(x, y, Color.Green); } } var color = Color.Green; /* * using (var g = Graphics.FromImage(bmp)) * { * g.CompositingMode = CompositingMode.SourceOver; * g.FillRectangle(new SolidBrush(color), rect); * g.DrawRectangle(Pens.DarkGreen, rect); * }*/ //var bmp2 = new Bitmap(200, 200, System.Drawing.Imaging.PixelFormat.Format32bppArgb); //bmp2.SetPixel(100,100, Color.Red); this.Invoke(new MethodInvoker(delegate() { this.Text = "But the cache has found a part of data exists (green) " + i; if (this.pictureBox1.Image != null) { var iimg = this.pictureBox1.Image; this.pictureBox1.Image = Clone(bmp); iimg.Dispose(); } else { this.pictureBox1.Image = Clone(bmp); } })); Thread.Sleep(500); } clone = null; ReduceSurfaceAreaByReordering waterdrop = new ReduceSurfaceAreaByReordering() { Area = area }; this.Invoke(new MethodInvoker(delegate() { this.Text = "Algorithm looks at what the user requested(right) and what is missing(black)"; var fg = waterdrop.ToBitmap(); if (this.pictureBox2.Image != null) { var iimg = this.pictureBox2.Image; this.pictureBox2.Image = fg; iimg.Dispose(); } else { this.pictureBox2.Image = fg; } })); waterdrop.SortPass += delegate(object sender, EventArgs e) { var fg = waterdrop.ToBitmap(); this.Invoke(new MethodInvoker(delegate() { this.Text = "Algorithm sorts the filled items(green), to find the unfilled items(black)"; if (this.pictureBox2.Image != null) { var iimg = this.pictureBox2.Image; this.pictureBox2.Image = fg; iimg.Dispose(); } else { this.pictureBox2.Image = fg; } })); Thread.Sleep(100); }; waterdrop.Sort(); int counter1 = 0; var bg = waterdrop.ToBitmap(); BiggestContiguousBlock skimmer = new BiggestContiguousBlock(waterdrop); foreach (var item in skimmer.Chunkify(false)) { if (this.pictureBox2.Image != bg) { bg = waterdrop.ToBitmap(); } item.DrawTo(bg, Color.Blue); this.Invoke(new MethodInvoker(delegate() { this.Text = "Finds the most convenient large block(blue) to resend request " + counter1++; if (this.pictureBox2.Image != null) { var iimg = this.pictureBox2.Image; this.pictureBox2.Image = bg; this.pictureBox2.Invalidate(); if (iimg != bg) { iimg.Dispose(); } } else { this.pictureBox2.Image = bg; } })); foreach (var y in item.RowIndices) { foreach (var x in item.ColIndices) { if (area[y, x]) { MessageBox.Show("This is already true (" + x + "," + y + ")"); } } } //else //area[y, x] = true; item.SetAllTrue(); Thread.Sleep(500); int counter2 = 0; foreach (var subitem in item.Chunkify(25)) { subitem.DrawTo(bg, Color.Red); this.Invoke(new MethodInvoker(delegate() { this.Text = "There are limits to how big a request can be, so it divides the request into smaller ones(red) " + counter2++; if (this.pictureBox2.Image != null) { var iimg = this.pictureBox2.Image; this.pictureBox2.Image = bg; this.pictureBox2.Invalidate(); if (iimg != bg) { iimg.Dispose(); } } else { this.pictureBox2.Image = bg; } })); Thread.Sleep(100); } } for (int y = 0; y < 200; y++) { for (int x = 0; x < 250; x++) { if (!area[y, x]) { MessageBox.Show("Not every cell is true (" + x + "," + y + ")"); break; } } } } this.Invoke(new MethodInvoker(delegate() { this.Text = "done!"; })); }