Click here to Skip to main content
15,896,201 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi Guys

I have been asked to solve an issue - same as the one posted here;
http://social.msdn.microsoft.com/Forums/da-DK/csharpgeneral/thread/e285e4d9-c43d-4482-a2e7-c8d1861657e0[^]

I have managed to get the rectangles up and displaying, the problem i am having is when i try to get them all on one touching and NOT overlapping -
if i manually add 3 RANDOM rectangles i can get 2 of them to look perfect, the last will appear in random and overlapping.

If i manually set the properties of the rectangles, i can accomplish what i am wanting - hence, the confusion;

The below code works;
C#
rectangles = new List<rectangle>();
            Rectangle rect4 = new Rectangle(10, 160, 100, 100);
            Rectangle rect5 = new Rectangle(rect4.X + rect4.Width, 160, 120, 120);
            Rectangle rect6 = new Rectangle(rect5.X + rect5.Width, 160, 100, 140);
            Rectangle rect7 = new Rectangle(rect6.X + rect6.Width, 160, 100, 80);
            rectangles.Add(rect4);
            rectangles.Add(rect5);
            rectangles.Add(rect6);
            rectangles.Add(rect7);
            
            graphics.DrawRectangles(pen1,rectangles.ToArray());

Yet the following loop doesn't;
C#
Rectangle foo = GetRandomRectangle(rng);
            Rectangle bar = GetRandomRectangle(rng);
            Rectangle car = GetRandomRectangle(rng);
            List<rectangle> rectangles = new List<rectangle>();
            rectangles.Add(foo);
            rectangles.Add(bar);
            rectangles.Add(car);

foreach (Rectangle rect in rectangles)
{
if (rect.Height > TallestRectangle) { TallestRectangle = rect.Height; }
}
foreach (Rectangle rect in rectangles)
{
if (PreviousRectangleX + PreviousRectangleWidth == 0)
{
graphics.DrawRectangle(pen1, new Rectangle(rect.X, (TallestRectangle - rect.Height), rect.Width, rect.Height));
}
else
{
graphics.DrawRectangle(pen1, new Rectangle((PreviousRectangleX + PreviousRectangleWidth), (TallestRectangle - rect.Height), rect.Width, rect.Height));
}
PreviousRectangleX = rect.X;
PreviousRectangleWidth = rect.Width;
}


Function GetRandomRectagle;
C#
private Rectangle GetRandomRectangle(Random rnd)
        {
            int x = rnd.Next(4, 110);
            int y = rnd.Next(4, 110);
            int width = rnd.Next(4, 110);
            int height = rnd.Next(4, 110);
            Rectangle rec = new Rectangle(x, y, width, height);
            return rec
        };



Any help would be greatly appreciated?

Regards
Posted
Updated 12-Jul-12 22:00pm
v5

You don't appear to set TallestRectangle to anything - I would try initializing it outside the outer loop to a negative value.
 
Share this answer
 
Comments
hexie 13-Jul-12 3:11am    
I forgot to add, i do initialize [TallestRectangle] above -
int TallestRectangle = 0;
The are staying on the same base / bottom line. The problem is that they are overlapping each other - i would like each rectangle to start from the edge of the previous one.
Update:
I have managed to get the rectangles to draw as wanted, i am now in the process of implementing a loop for them all, will keep you posted.
Thanks.
 
Share this answer
 
Hi Guys

Keeping with this post, i was thinking of the best way to achieve this;
"Generate the requested amount of rectangles and write them to file (in a human readable).
Read the randomly generated input rectangles from the file generated from the above step"

I have done the first step using a bmp / jpg file (i.e. i save the image of the rectangles) and was wondering if there might be a better way to read this file back now (for the next step) or alternatively, should i be saving the rectangles differently?

Code i use to save the rectangles to file;

Bitmap bmp = new Bitmap(this.Width-100, this.Height-100 , graphics);
using (Graphics g = Graphics.FromImage(bmp))
{
g.Clear(Color.White);
g.DrawRectangles(pen1, rectangles.ToArray());
graphics.DrawRectangles(pen1, rectangles.ToArray());
}
bmp.Save(Application.StartupPath + "\\Rectangle_Image.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);


Any help is much appreciated.

Thanks
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900