EPPlus detect image aspect ratio - epplus

I am working with EPPlus and try to place a image into B1 cell. I found a nice post which show how to detect aspect ratio and set size for image accordingly.
I saw his code but still few things are not clear. here is code taken from https://stackoverflow.com/a/54941008/15940620
public static void CreatePicture(ExcelWorksheet worksheet, string name, Image image, int firstColumn, int lastColumn, int firstRow, int lastRow, int defaultOffsetPixels)
{
int columnWidth = GetWidthInPixels(worksheet.Cells[firstRow, firstColumn]);
int rowHeight = GetHeightInPixels(worksheet.Cells[firstRow, firstColumn]);
int totalColumnWidth = columnWidth * (lastColumn - firstColumn + 1);
int totalRowHeight = rowHeight * (lastRow - firstRow + 1);
double cellAspectRatio = Convert.ToDouble(totalColumnWidth) / Convert.ToDouble(totalRowHeight);
int imageWidth = image.Width;
int imageHeight = image.Height;
double imageAspectRatio = Convert.ToDouble(imageWidth) / Convert.ToDouble(imageHeight);
int pixelWidth;
int pixelHeight;
if (imageAspectRatio > cellAspectRatio)
{
pixelWidth = totalColumnWidth - defaultOffsetPixels * 2;
pixelHeight = pixelWidth * imageHeight / imageWidth;
}
else
{
pixelHeight = totalRowHeight - defaultOffsetPixels * 2;
pixelWidth = pixelHeight * imageWidth / imageHeight;
}
int rowOffsetPixels = (totalRowHeight - pixelHeight) / 2;
int columnOffsetPixels = (totalColumnWidth - pixelWidth) / 2;
int rowOffsetCount = 0;
int columnOffsetCount = 0;
if (rowOffsetPixels > rowHeight)
{
rowOffsetCount = (int)Math.Floor(Convert.ToDouble(rowOffsetPixels) / Convert.ToDouble(rowHeight));
rowOffsetPixels -= rowHeight * rowOffsetCount;
}
if (columnOffsetPixels > columnWidth)
{
columnOffsetCount = (int)Math.Floor(Convert.ToDouble(columnOffsetPixels) / Convert.ToDouble(columnWidth));
columnOffsetPixels -= columnWidth * columnOffsetCount;
}
int row = firstRow + rowOffsetCount - 1;
int column = firstColumn + columnOffsetCount - 1;
ExcelPicture pic = worksheet.Drawings.AddPicture(name, image);
pic.SetPosition(row, rowOffsetPixels, column, columnOffsetPixels);
pic.SetSize(pixelWidth, pixelHeight);
}
public static int GetHeightInPixels(ExcelRange cell)
{
using (Graphics graphics = Graphics.FromHwnd(IntPtr.Zero))
{
float dpiY = graphics.DpiY;
return (int)(cell.Worksheet.Row(cell.Start.Row).Height * (1 / 72.0) * dpiY);
}
}
public static float MeasureString(string s, Font font)
{
using (var g = Graphics.FromHwnd(IntPtr.Zero))
{
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
return g.MeasureString(s, font, int.MaxValue, StringFormat.GenericTypographic).Width;
}
}
public static int GetWidthInPixels(ExcelRange cell)
{
double columnWidth = cell.Worksheet.Column(cell.Start.Column).Width;
Font font = new Font(cell.Style.Font.Name, cell.Style.Font.Size, FontStyle.Regular);
double pxBaseline = Math.Round(MeasureString("1234567890", font) / 10);
return (int)(columnWidth * pxBaseline);
}
int firstColumn, int lastColumn, int firstRow, int lastRow, int defaultOffsetPixels
I like to know what will be the value for firstColumn
I like to know what will be the value for lastColumn
I like to know what will be the value for firstRow
I like to know what will be the value for lastRow
I like to know what will be the value for defaultOffsetPixels
Anyone can guide me how to calculate values for above points.
Regarding the points 1 to 4 can i use the below code to get first, last row and first column and last column ?
var start = workSheet.Dimension.Start;
var end = workSheet.Dimension.End;
int startrow = start.Row;
int endrow = end.Row;
int startcol = start.Column
int endcol = end.Column
OR should i use these code to get first, last row and first and last column ? please suggest.
int numRow = worksheet.Dimension.Rows;
int numCol = worksheet.Dimension.Columns;
string lastAddress = worksheet.Dimension.Address.Last().ToString();
How to calculate defaultOffsetPixels ?
Thanks

Well,firstColumn,lastColumn,firstRow,lastRow,defaultOffsetPixels is incoming Parameter。I have an example of how it works,I hope it helps
Image image = Image.FromStream(new MemoryStream(drs[0]["Image"] as byte[]));
EPPlusPicture.CreatePicture(worksheet, Guid.NewGuid().ToString(), image, col, col, firstRow, lastRow, 5);

Related

Vulkan: Loading floating point cubemap textures distorted

I am using vulkan-tutorial codes and i made modify for cubemap.
when i use VK_FORMAT_R8G8B8A8_UNORM is working with this code:
unsigned char* pixelsArray[6];
for (int i = 0; i < 6; ++i)
{
pixelsArray[i] = stbi_load(imageFileArray[i].c_str(), &texWidth, &texHeight, &texChannels, STBI_rgb_alpha);
}
VkDeviceSize allSize = texWidth * texHeight * 4 * 6;
VkDeviceSize size = texWidth * texHeight * 4 ;
VkBufferCreateInfo bufferInfo{};
...
bufferInfo.size = allSize ;
vkMapMemory(device, stagingBufferMemory, 0, AllSize, 0, &data);
for(int i = 0; i < 6; ++i)
{
memcpy( (char*) data + (size*i) , pixelsArray[i], static_cast<size_t>(size));
}
vkUnmapMemory(device, stagingBufferMemory);
VkImageCreateInfo imageInfo{};
...
imageInfo.arrayLayers = 6;
imageInfo.format = VK_FORMAT_R8G8B8A8_UNORM;
imageInfo.flags = VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT;
VkImageViewCreateInfo viewInfo{};
...
viewInfo.viewType = VK_IMAGE_VIEW_TYPE_CUBE;
viewInfo.format = VK_FORMAT_R8G8B8A8_UNORM;
viewInfo.subresourceRange.layerCount = 6;
but when i try VK_FORMAT_R16G16B16A16_SFLOAT is giving distorted display and no validation error with this code:
float* pixelsArray[6];
for (int i = 0; i < 6; ++i)
{
pixelsArray[i] = stbi_loadf(imageFileArray[i].c_str(), &texWidth, &texHeight, &texChannels, STBI_rgb_alpha);
}
VkDeviceSize allSize = texWidth * texHeight * 4 * 6 * 2;// I added *2
VkDeviceSize size = texWidth * texHeight * 4 * 2;// I added *2
VkBufferCreateInfo bufferInfo{};
...
bufferInfo.size = allSize ;
vkMapMemory(device, stagingBufferMemory, 0, AllSize, 0, &data);
for(int i = 0; i < 6; ++i)
{
memcpy( (char*) data + (size*i) , pixelsArray[i], static_cast<size_t>(size));
}
vkUnmapMemory(device, stagingBufferMemory);
VkImageCreateInfo imageInfo{};
...
imageInfo.arrayLayers = 6;
imageInfo.format = VK_FORMAT_R16G16B16A16_SFLOAT;
imageInfo.flags = VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT;
VkImageViewCreateInfo viewInfo{};
...
viewInfo.viewType = VK_IMAGE_VIEW_TYPE_CUBE;
viewInfo.format = VK_FORMAT_R16G16B16A16_SFLOAT;
viewInfo.subresourceRange.layerCount = 6;
when VK_FORMAT_R8G8B8A8_UNORM :
when VK_FORMAT_R16G16B16A16_SFLOAT :
i fixed the problem. problem was that i want to use half float but i was sending float to memcpy function.i searched how can i use half float and i found a solution without using extra library.
what i did add helper functions :
typedef unsigned int uint;
typedef unsigned short ushort;
uint as_uint(const float x)
{
return *(uint*)&x;
}
ushort float_to_half(const float x)
{
// IEEE-754 16-bit floating-point format (without infinity): 1-5-10, exp-15, +-131008.0, +-6.1035156E-5, +-5.9604645E-8, 3.311 digits
const uint b = as_uint(x)+0x00001000; // round-to-nearest-even: add last bit after truncated mantissa
const uint e = (b&0x7F800000)>>23; // exponent
const uint m = b&0x007FFFFF; // mantissa; in line below: 0x007FF000 = 0x00800000-0x00001000 = decimal indicator flag - initial rounding
return (b&0x80000000)>>16 | (e>112)*((((e-112)<<10)&0x7C00)|m>>13) | ((e<113)&(e>101))*((((0x007FF000+m)>>(125-e))+1)>>1) | (e>143)*0x7FFF; // sign : normalized : denormalized : saturate
}
and fix problem with this helper functions :
VkDeviceSize size_2 = texWidth * texHeight * 4;// different from the above variables in question : allSize or size
//create half float for cubemap
void* half_pixelsArray[6];
half_pixelsArray[0] = new ushort[size_2];
half_pixelsArray[1] = new ushort[size_2];
half_pixelsArray[2] = new ushort[size_2];
half_pixelsArray[3] = new ushort[size_2];
half_pixelsArray[4] = new ushort[size_2];
half_pixelsArray[5] = new ushort[size_2];
//copy from float to half float
for (int i = 0; i < 6; ++i)
{
for (int j = 0; j < size_2; ++j)
{
((ushort*)half_pixelsArray[i])[j] = float_to_half( pixelsArray[i][j] );
}
}
// and change float to half flaot in memcpy
memcpy( (char*) data + (layerSize*i) , half_pixelsArray[i], static_cast<size_t>(layerSize));

Horizontal scroll in appium

How can i horizontally scroll in the appium
like for Vertical scroll we do
driver.findElementByAndroidUIAutomator("new UiScrollable(new UiSelector()).scrollIntoView("")");
is there like that???
You can set the UiSelector to scrollable and set direction of scrolling
driver().findElementByAndroidUIAutomator("new UiScrollable(new UiSelector().scrollable(true)."
+ "resourceId(\"<id of scrollable control>\"))"
+ ".setAsHorizontalList().scrollIntoView(new UiSelector().textContains(\"<text to search for>\"))");
Link gives details about UiScrollable
You can Use this way this will work horizontal and also by changing scroll start and scroll end you can use same code for vertical scroll also.
public void scrollHorizontal(double value1,double value2) {
Dimension dimension =driver.manage().window().getSize();
Double scrollheight = dimension.getHeight()*value1;
int scrollStart = scrollheight.intValue();
Double scrollheightEnd = dimension.getHeight()*value2;
int scrollEnd = scrollheightEnd.intValue();
new TouchAction((PerformsTouchActions)driver)
.press(PointOption.point(scrollStart,0))
.waitAction(WaitOptions.waitOptions(Duration.ofSeconds(2)))
.moveTo(PointOption.point(scrollEnd , 0))
.release()
.perform(); }
Use below method for horizontally scrolling:
public static void swipeHorizontal(AppiumDriver driver, double startPercentage, double finalPercentage, double anchorPercentage, int duration) throws Exception {
Dimension size = driver.manage().window().getSize();
int anchor = (int) (size.height * anchorPercentage);
int startPoint = (int) (size.width * startPercentage);
int endPoint = (int) (size.width * finalPercentage);
new TouchAction(driver).press(startPoint, anchor).waitAction(Duration.ofMillis(duration)).moveTo(endPoint, anchor).release().perform();
}
Call above method by:
swipeHorizontal((AppiumDriver) driver,0.9,0.01,0.5,2000);
This applies in all directions:
enum:
public enum DIRECTION {
DOWN, UP, LEFT, RIGHT;
}
actual code:
public static void swipe(MobileDriver driver, DIRECTION direction, long duration) {
Dimension size = driver.manage().window().getSize();
int startX = 0;
int endX = 0;
int startY = 0;
int endY = 0;
switch (direction) {
case RIGHT:
startY = (int) (size.height / 2);
startX = (int) (size.width * 0.90);
endX = (int) (size.width * 0.05);
new TouchAction(driver)
.press(startX, startY)
.waitAction(Duration.ofMillis(duration))
.moveTo(endX, startY)
.release()
.perform();
break;
case LEFT:
startY = (int) (size.height / 2);
startX = (int) (size.width * 0.05);
endX = (int) (size.width * 0.90);
new TouchAction(driver)
.press(startX, startY)
.waitAction(Duration.ofMillis(duration))
.moveTo(endX, startY)
.release()
.perform();
break;
case UP:
endY = (int) (size.height * 0.70);
startY = (int) (size.height * 0.30);
startX = (size.width / 2);
new TouchAction(driver)
.press(startX, startY)
.waitAction(Duration.ofMillis(duration))
.moveTo(endX, startY)
.release()
.perform();
break;
case DOWN:
startY = (int) (size.height * 0.70);
endY = (int) (size.height * 0.30);
startX = (size.width / 2);
new TouchAction(driver)
.press(startX, startY)
.waitAction(Duration.ofMillis(duration))
.moveTo(startX, endY)
.release()
.perform();
break;
}
}
usage:
swipe(driver, Util.DIRECTION.RIGHT);
Hope this helps,

How do I add a checkbox column to a DataGrid in Compact Framework 3.5? [duplicate]

how to put checkboxes in datagrid in windows mobile 6 using c#?
dataset dsAgent=table;
DataTable dataTable = dsAgent.Tables[0];
DataGridTableStyle tableStyle = new DataGridTableStyle();
tableStyle.MappingName = dataTable.TableName;
GridColumnStylesCollection columnStyles = tableStyle.GridColumnStyles;
DataGridTextBoxColumn columnStyle = new DataGridTextBoxColumn();
columnStyle.MappingName = "FirstName";
columnStyle.HeaderText = "Name";
columnStyle.Width = 80;
columnStyles.Add(columnStyle);
//columnStyle = new DataGridTextBoxColumn();
//columnStyle.MappingName = "EmailAddress";
//columnStyle.HeaderText = "EmailID";
//columnStyle.Width = 150;
//columnStyles.Add(columnStyle);
columnStyle = new DataGridTextBoxColumn();
columnStyle.MappingName = "WorkPhone";
columnStyle.HeaderText = "PhoneNo";
columnStyle.Width = 150;
columnStyles.Add(columnStyle);
GridTableStylesCollection tableStyles = DataGrid.TableStyles;
tableStyles.Add(tableStyle);
DataGrid.PreferredRowHeight = 16;
DataGrid.RowHeadersVisible = false;
DataGrid.DataSource = dataTable;
Here's some code from and old blog by Eric Hartwell (pulled into SO using the wayback machine):
private void SetupTableStyles()
{
Color alternatingColor = SystemColors.ControlDark;
DataTable vehicle = dataSource.Tables[1];
// ID Column
DataGridCustomTextBoxColumn dataGridCustomColumn0 = new DataGridCustomTextBoxColumn();
dataGridCustomColumn0.Owner = this.dataGrid1;
dataGridCustomColumn0.Format = "0##";
dataGridCustomColumn0.FormatInfo = null;
dataGridCustomColumn0.HeaderText = vehicle.Columns[0].ColumnName;
dataGridCustomColumn0.MappingName = vehicle.Columns[0].ColumnName;
dataGridCustomColumn0.Width = dataGrid1.Width * 10 / 100; // 10% of grid size
dataGridCustomColumn0.AlternatingBackColor = alternatingColor;
dataGridCustomColumn0.ReadOnly = true;
dataGridTableStyle1.GridColumnStyles.Add(dataGridCustomColumn0);
// Make column
DataGridCustomTextBoxColumn dataGridCustomColumn1 = new DataGridCustomTextBoxColumn();
dataGridCustomColumn1.Owner = this.dataGrid1;
dataGridCustomColumn1.HeaderText = vehicle.Columns[1].ColumnName;
dataGridCustomColumn1.MappingName = vehicle.Columns[1].ColumnName;
dataGridCustomColumn1.NullText = "-Probably Ford-";
dataGridCustomColumn1.Width = dataGrid1.Width * 40 / 100; // 40% of grid size
dataGridCustomColumn1.Alignment = HorizontalAlignment.Right;
dataGridCustomColumn1.AlternatingBackColor = alternatingColor;
dataGridTableStyle1.GridColumnStyles.Add(dataGridCustomColumn1);
// Mileage column
DataGridCustomUpDownColumn dataGridCustomColumn2 = new DataGridCustomUpDownColumn();
dataGridCustomColumn2.Owner = this.dataGrid1;
dataGridCustomColumn2.HeaderText = vehicle.Columns[2].ColumnName;
dataGridCustomColumn2.MappingName = vehicle.Columns[2].ColumnName;
dataGridCustomColumn2.NullText = "-Unknown-";
dataGridCustomColumn2.Width = dataGrid1.Width * 20 / 100; // 20% of grid size
dataGridCustomColumn2.Alignment = HorizontalAlignment.Left;
dataGridCustomColumn2.AlternatingBackColor = alternatingColor;
dataGridTableStyle1.GridColumnStyles.Add(dataGridCustomColumn2);
// Availability column
DataGridCustomCheckBoxColumn dataGridCustomColumn3 = new DataGridCustomCheckBoxColumn();
dataGridCustomColumn3.Owner = this.dataGrid1;
dataGridCustomColumn3.HeaderText = vehicle.Columns[3].ColumnName;
dataGridCustomColumn3.MappingName = vehicle.Columns[3].ColumnName;
dataGridCustomColumn3.NullText = "-Unknown-";
dataGridCustomColumn3.Width = dataGrid1.Width * 10 / 100; // 10% of grid size
dataGridCustomColumn3.Alignment = HorizontalAlignment.Left;
dataGridCustomColumn3.AlternatingBackColor = alternatingColor;
dataGridTableStyle1.GridColumnStyles.Add(dataGridCustomColumn3);
// Fuel Level column
DataGridCustomComboBoxColumn dataGridCustomColumn4 = new DataGridCustomComboBoxColumn();
dataGridCustomColumn4.Owner = this.dataGrid1;
dataGridCustomColumn4.HeaderText = vehicle.Columns[4].ColumnName;
dataGridCustomColumn4.MappingName = vehicle.Columns[4].ColumnName;
dataGridCustomColumn4.NullText = "-Unknown-";
dataGridCustomColumn4.Width = dataGrid1.Width * 30 / 100; // 30% of grid size
dataGridCustomColumn4.Alignment = HorizontalAlignment.Left;
dataGridCustomColumn4.AlternatingBackColor = alternatingColor;
dataGridTableStyle1.GridColumnStyles.Add(dataGridCustomColumn4);
// Last Used column
DataGridCustomDateTimePickerColumn dataGridCustomColumn5 = new DataGridCustomDateTimePickerColumn();
dataGridCustomColumn5.Owner = this.dataGrid1;
dataGridCustomColumn5.HeaderText = vehicle.Columns[5].ColumnName;
dataGridCustomColumn5.MappingName = vehicle.Columns[5].ColumnName;
dataGridCustomColumn5.NullText = "-Unknown-";
dataGridCustomColumn5.Width = dataGrid1.Width * 30 / 100; // 30% of grid size
dataGridCustomColumn5.Alignment = HorizontalAlignment.Left;
dataGridCustomColumn5.AlternatingBackColor = alternatingColor;
dataGridTableStyle1.GridColumnStyles.Add(dataGridCustomColumn5);
// Grid, mapping
dataGridTableStyle1.MappingName = vehicle.TableName; // Setup table mapping name
dataGrid1.DataSource = vehicle;
// Setup grid's data source
ComboBox cb = (ComboBox)dataGridCustomColumn4.HostedControl;
DataTable fuel = dataSource.Tables[0]; // Set up data source
cb.DataSource = fuel;
// For combo box column
cb.DisplayMember = fuel.Columns[0].ColumnName;
cb.ValueMember = fuel.Columns[0].ColumnName;
dataGrid1.CurrentRowIndex = 50; // Move to the middle of the table
}
For better checkbox UI look and feel, rather than big cross
private void DrawCheckBox(Graphics g, Rectangle bounds, CheckState state)
{
int size;
int boxTop;
size = bounds.Size.Height < bounds.Size.Width ? bounds.Size.Height : bounds.Size.Width;
size = size > ((int)g.DpiX / 7) ? ((int)g.DpiX / 7) : size;
boxTop = bounds.Y + (bounds.Height - size) / 2;
size = 12; // 13, so I made it 12
boxTop = boxTop - 1;
using (Pen p = new Pen(this.Owner.ForeColor))
{
g.DrawRectangle(p, bounds.X, boxTop, size, size);
}
if (state != CheckState.Unchecked)
{
using (Pen p = new Pen(state == CheckState.Indeterminate ? SystemColors.GrayText : SystemColors.ControlText))
{
p.Width = 2;
int offset = 2;
int edgeOffset = 2;
g.DrawLine(p, bounds.X + offset, boxTop + offset + 2, bounds.X + (size / 2) - edgeOffset, boxTop + (size / 2) + edgeOffset);
g.DrawLine(p, bounds.X + (size / 2) - edgeOffset, boxTop + (size / 2) + edgeOffset, bounds.X + size - offset, boxTop + offset);
}
}
}

Zedgraph - Change X-Axis from points to frequency

i have a working program where i can add an array to a Zedgraph and show this in a form.
Now i only want to change the display of the x-axis from points (0..400) to frequency (9e3..6e9 Hz).
Here are my currently working functions:
public void AddGraph(double[] Values, string LegendName)
{
int i = 0;
PointPairList list = new PointPairList();
for (i = 0; i < Values.Length; i++)
{
list.Add(i, Values[i]);
}
if (i > MaxXAxis)
MaxXAxis = i;
SList.Add(list);
SListColor.Add(Color.Black);
}
SListName.Add(LegendName);
}
public void ShowDiagram(string Title, string XAxisName, string YAxisName,
int Timeout_ms)
{
ZedGraph.ZedGraphControl zgc = new ZedGraphControl();
GraphPane myPane = zgc.GraphPane;
LineItem myCurve = null;
// Set the titles and axis labels
myPane.Title.Text = Title;
myPane.XAxis.Title.Text = XAxisName;
myPane.YAxis.Title.Text = YAxisName;
for (int i = 0; i < SList.Count(); i++)
{
myCurve = myPane.AddCurve(SListName[i], SList[i], SListColor[i],
SymbolType.None);
myCurve.Line.Width = 2;
}
// Add gridlines to the plot, and make them gray
myPane.XAxis.MinorGrid.IsVisible = true;
myPane.YAxis.MinorGrid.IsVisible = true;
myPane.XAxis.MinorGrid.Color = Color.LightGray;
myPane.YAxis.MinorGrid.Color = Color.LightGray;
myPane.XAxis.MinorGrid.DashOff = 0;
myPane.YAxis.MinorGrid.DashOff = 0;
myPane.XAxis.MajorGrid.IsVisible = true;
myPane.YAxis.MajorGrid.IsVisible = true;
myPane.XAxis.MajorGrid.Color = Color.Gray;
myPane.YAxis.MajorGrid.Color = Color.Gray;
myPane.XAxis.MajorGrid.DashOff = 0;
myPane.YAxis.MajorGrid.DashOff = 0;
// Move Legend to bottom
myPane.Legend.Position = LegendPos.Bottom;
zgc.AxisChange();
myPane.XAxis.Scale.Max = MaxXAxis;
zgc.Location = new Point(0, 0);
zgc.Size = new Size(panel_diagramm.ClientRectangle.Width, panel_diagramm.ClientRectangle.Height);
panel_diagramm.Controls.Add(zgc);
}
How can i change the above two functions that they display the frequency in the x-axis?
I already tried to change the AddGraph-function to pass the needed parameters and to calculate the list to have the correct values. But what then...?
public void AddGraph_Frequency(int **Points**, double **StartFrequency**,
double **StopFrequency**, double[] Values, string GraphColor, string LegendName)
{
...
double frequency = StartFrequency; //der erste Punkt
double Intervall = (StopFrequency - StartFrequency) / Points;
for (i = 0; i < Points; i++)
{
list.Add(frequency, Values[i]);
frequency = frequency + Intervall;
}
....
}
Thanks for any help
best regards
Solved.
Missing was:
myPane.XAxis.Scale.Max = Stopfrequency;
myPane.XAxis.Scale.Min = Startfrequency;

Generating non-overlapping rectangle test sets

Alas this is a university type question, but one im struggling with none the less.
I need to generate large test sets of rectangles, non-overlapping, that can then be used to test an algorithm that finds the adjacencies between them. The test sets will need to probably have upwards of 10000 - 100000 rectangles in them. Ive been searching the net for examples of how to generate test sets like this, but have come up with nothing. Im aware that I could use a purely brute force method, and every time a random rectangle is generated, check whether or not it overlaps with any of the other rectangles, but this seems like generating the test sets will end up taking days if not longer!
Any one know how to go about doing this, or at least where I should start looking?
I found your idea fun and challanging and therefore tried a solution by using a matrix.
Basicly, the idea is (when talking in pixels) to create a matrix of booleans of the same width and height as the square root of MaxWidthOfRectangle * (NumberOfRectangles) (just for simplicity the same width and height).
Next, for each entry in the matrix, generate a random rectangle between min and max bounds. and set all the bools in the matrix for the specific rectangle. Now when generating the next rectangle, you can simply check "around" the desired location to determine how much space you can occupy rather then having to estimate a size and compare with each other rectangle if it conflicts.
My code:
class RectangleGenerator
{
readonly bool[,] _matrix;
readonly int _size;
readonly int _minimalBoxSize;
readonly int _maximumBoxSize;
readonly Random _random = new Random(1);
readonly List<Point> _offsets;
public bool[,] Matrix { get { return _matrix; } }
public RectangleGenerator(int size, int minimalBoxSize, int maximumBoxSize)
{
_matrix = new bool[size, size];
_size = size;
_minimalBoxSize = minimalBoxSize;
_maximumBoxSize = maximumBoxSize;
_offsets = new List<Point>(size * size);
Reset();
}
public IEnumerable<Rectangle> Calculate()
{
while (_offsets.Count > 0)
{
Point currentPoint = _offsets[_offsets.Count - 1];
_offsets.RemoveAt(_offsets.Count - 1);
if (!_matrix[currentPoint.X, currentPoint.Y])
{
Rectangle rectangle;
if (TryCreateNextRectangle(currentPoint.X, currentPoint.Y, out rectangle))
{
// fill the matrix with the rectangle + padding
int startFillX = Math.Max(0, rectangle.Left);
int startFillY = Math.Max(0, rectangle.Top);
int endFillX = Math.Min(_size, rectangle.Right);
int endFillY = Math.Min(_size, rectangle.Bottom);
for (int fillX = startFillX; fillX < endFillX; fillX++)
for (int fillY = startFillY; fillY < endFillY; fillY++)
{
_matrix[fillX, fillY] = true;
}
yield return rectangle;
}
}
}
}
private bool TryCreateNextRectangle(int x, int y, out Rectangle rectangle)
{
int maxWidth = DetermineMaxWidth(x, y, _minimalBoxSize);
int maxHeight = DetermineMaxHeight(y, x, maxWidth);
if (maxWidth < _minimalBoxSize || maxHeight < _minimalBoxSize)
{
rectangle = Rectangle.Empty;
return false;
}
int width = _random.Next(_minimalBoxSize, maxWidth);
int height = _random.Next(_minimalBoxSize, maxHeight);
rectangle = new Rectangle(x, y, width, height);
return true;
}
private int DetermineMaxWidth(int x, int y, int height)
{
int result = Math.Min(_maximumBoxSize, _size - x);
for (int offsetX = 0; offsetX < result; offsetX++)
for (int offsetY = 0; offsetY < height; offsetY++)
{
if (_matrix[x + offsetX, y + offsetY])
{
result = offsetX;
break;
}
}
return result;
}
private int DetermineMaxHeight(int y, int x, int width)
{
int result = Math.Min(_maximumBoxSize, _size - y);
for (int offsetY = 0; offsetY < result; offsetY++)
for (int offsetX = 0; offsetX < width; offsetX++ )
{
if (_matrix[x + offsetX, y + offsetY])
{
result = offsetY;
break;
}
}
return result;
}
public void Reset()
{
// append for padding:
for (int x = 0; x < _size; x++)
for (int y = 0; y < _size; y++)
{
_matrix[x, y] = false;
if (_size - x >= _minimalBoxSize && _size - y >= _minimalBoxSize)
{
_offsets.Add(new Point(x, y));
}
}
_offsets.Sort((x, y) => x == y ? 0 : _random.Next(-1, 1));
}
}