旋转矩形问题 [英] Rotating Rectangle Problem
问题描述
伙计们
我被要求解决一个问题-与此处发布的问题相同;
http://social.msdn.microsoft.com/论坛/da-DK/csharpgeneral/thread/e285e4d9-c43d-4482-a2e7-c8d1861657e0 [
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;
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;
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;
}
函数GetRandomRectagle;
Function GetRandomRectagle;
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
推荐答案
您似乎没有将TallestRectangle设置为任何值-我会尝试在外循环之外将其初始化为负值.
You don''t appear to set TallestRectangle to anything - I would try initializing it outside the outer loop to a negative value.
更新:
我设法按需要绘制了矩形,我现在正在为它们全部实现循环,这样可以随时通知您.
谢谢.
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.
伙计们
跟上这篇文章,我一直在思考实现这一目标的最佳方法;
生成所需数量的矩形并将其写入文件(以人类可读的方式.)
从上述步骤中生成的文件中读取随机生成的输入矩形"
我已经使用bmp/jpg文件完成了第一步(即,我保存了矩形的图像),并且想知道是否有更好的方法可以立即读取此文件(用于下一步),或者我应该保存矩形是否不同?
我用来将矩形保存到文件的代码;
位图bmp =新位图(this.Width-100,this.Height-100,图形);
使用(Graphics g = Graphics.FromImage(bmp))
{
g.Clear(Color.White);
g.DrawRectangles(pen1,squares.ToArray());
graphics.DrawRectangles(pen1,squares.ToArray());
}
bmp.Save(Application.StartupPath +"\\ Rectangle_Image.jpg",System.Drawing.Imaging.ImageFormat.Jpeg);
非常感谢您的帮助.
谢谢
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
这篇关于旋转矩形问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!