I have a Method that generates random coordinates to position rectangles, no rectangles can intersect with each other. Works but I need to speed up the performance, I am hoping someone could give me an idea.
private void RandomCords()
{
DateTime StartTime = DateTime.Now;
PositionCoords.Clear();
Random R = new Random();
ArrayList rectList = new ArrayList();
for (int i = 0; i < 10; i++)
{
ScreenCoord coord = new ScreenCoord();
coord.X = R.Next(Enviro1.X_Constraint, Enviro1.ScreenWidth - Global.imageWidth);
coord.Y = R.Next(Enviro1.Y_Constraint, Enviro1.ScreenHeight - Global.imageHeight);
System.Drawing.Rectangle r1 = new System.Drawing.Rectangle(coord.X, coord.Y, Global.imageWidth, Global.imageHeight);
int k = 0;
while (k < rectList.Count)
{
if (r1.IntersectsWith(((System.Drawing.Rectangle)rectList[k])))
{
coord.X = R.Next(Enviro1.X_Constraint, Enviro1.ScreenWidth - Global.imageWidth);
coord.Y = R.Next(Enviro1.Y_Constraint, Enviro1.ScreenHeight - Global.imageHeight);
r1.X = coord.X;
r1.Y = coord.Y;
k = 0;
}
else
k++;
}
PositionCoords.Add(coord);
System.Drawing.Rectangle tempRect = new System.Drawing.Rectangle(coord.X, coord.Y, Global.imageWidth, Global.imageHeight);
rectList.Add(tempRect);
}
DateTime EndTime = DateTime.Now;
TimeSpan ts = EndTime - StartTime;
Response.Write("Page Generated In: " + ts.TotalMilliseconds.ToString() + " ms");
private void RandomCords()
{
DateTime StartTime = DateTime.Now;
PositionCoords.Clear();
Random R = new Random();
ArrayList rectList = new ArrayList();
for (int i = 0; i < 10; i++)
{
ScreenCoord coord = new ScreenCoord();
coord.X = R.Next(Enviro1.X_Constraint, Enviro1.ScreenWidth - Global.imageWidth);
coord.Y = R.Next(Enviro1.Y_Constraint, Enviro1.ScreenHeight - Global.imageHeight);
System.Drawing.Rectangle r1 = new System.Drawing.Rectangle(coord.X, coord.Y, Global.imageWidth, Global.imageHeight);
int k = 0;
while (k < rectList.Count)
{
if (r1.IntersectsWith(((System.Drawing.Rectangle)rectList[k])))
{
coord.X = R.Next(Enviro1.X_Constraint, Enviro1.ScreenWidth - Global.imageWidth);
coord.Y = R.Next(Enviro1.Y_Constraint, Enviro1.ScreenHeight - Global.imageHeight);
r1.X = coord.X;
r1.Y = coord.Y;
k = 0;
}
else
k++;
}
PositionCoords.Add(coord);
System.Drawing.Rectangle tempRect = new System.Drawing.Rectangle(coord.X, coord.Y, Global.imageWidth, Global.imageHeight);
rectList.Add(tempRect);
}
DateTime EndTime = DateTime.Now;
TimeSpan ts = EndTime - StartTime;
Response.Write("Page Generated In: " + ts.TotalMilliseconds.ToString() + " ms");