我輸入正確完成我的所有矩形垂直堆疊,每次繪製一個新的矩形時有不同的顏色,但我努力讓輸出看起來像底部的圖片。請任何人幫助我走上正確的道路。獲取矩形垂直堆疊到水平,但保持形狀
//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