我输入正确完成我的所有矩形垂直堆叠,每次绘制一个新的矩形时有不同的颜色,但我努力让输出看起来像底部的图片。请任何人帮助我走上正确的道路。获取矩形垂直堆叠到水平,但保持形状
//List to strore all randomly generated rectangles
List<Rectangle> rectangleCollection = new List<Rectangle>();
//Counts the amount of time a rectangle needs to be drawn
int count = 0;
public static Random ran = new Random();
void CreateRectangle()
{
int TallestRectangle = 0; ;
int PrevRecY = 0;
Graphics graphic = pictureBox1.CreateGraphics();
SolidBrush brush;
foreach (Rectangle rect in rectangleCollection)
{
if (rect.Height > TallestRectangle)
TallestRectangle = rect.Height;
}
foreach (Rectangle rect in rectangleCollection)
{
graphic.FillRectangle(brush = new SolidBrush(Color.FromArgb(ran.Next(1, 255), ran.Next(1, 255), ran.Next(1, 255))),
new Rectangle(rect.X + PrevRecY, (TallestRectangle - rect.Height), rect.Width, rect.Height));
PrevRecY += rect.Width;
}
}
private void button1_Click(object sender, EventArgs e)
{
count = int.Parse(textBox1.Text);
for (int i = 0; i < count; i++)
{
GetRandomRectangle();
}
CreateRectangle();
}
void GetRandomRectangle()
{
Graphics graph = this.CreateGraphics();
int x = 0;
int y = 0;
int width = ran.Next(20, 100);
int height = ran.Next(30, 150);
Rectangle rec1 = new Rectangle(x, y, width, height);
rectangleCollection.Add(rec1);
}
你试过了什么?你的代码只是产生问题 – 2013-04-25 10:37:03
这个问题不是很清楚,可以使用改进,很难理解你的具体问题是什么 - 你说你正在挣扎,但你不认同什么。 – Amicable 2013-04-25 10:41:49
在同一基线上从左至右彼此相邻放置的一组随机矩形。输出:输出表示与输入矩形所描述的相同面积和结果形状的垂直堆叠矩形的最小数量。 – TheJunior 2013-04-25 11:07:13