這可能是更多的邏輯問題比語法,所以我不知道是否有人願意幫助,但我有一個類型的矩形列表,我需要循環回來和轉發。C#重繪矩形邏輯
該列表包含從左到右隨機寬度和高度的隨機範圍的矩形,例如,
___________
____ l l
l l l l
l l ________ l l
l l___l l l l
l l l l___l l
l__l___l______l___l_________l
我需要旋轉矩形,使它們看起來像下面
___________ ____ l l l l l l l l ________ l l l__l___l______l l l l_____________l___l_________l l___________________________l
因此,我認爲這樣做最簡單的方法是採取第一個矩形,並把它比作下一個沿。然而,當我試圖讓下一個長方形的值一起我卡住,因爲我要出去的可能的索引範圍
這是繪製其作品的原始矩形的代碼,因爲它是爲了
Graphics RectangleGraphics = DrawingSurface;
for (int x = 0; x < userInput; ++x)
{
int Height = myRectangleClass.genHeight();
int Width = myRectangleClass.genWidth();
RectangleGraphics.DrawRectangle(Pen, myRectangleClass.MyRectangle(startPositionX, (450 - Height), Width, Height));
ReadWrite.writeOutput(startPositionX, (450 - Height), Width, Height);
startPositionX = startPositionX + Width;
}
我嘗試使用下面的代碼,同時保持相同的形狀旋轉矩形,但我顯然得到超出範圍錯誤
Graphics RectangleGraphics = DrawingSurface;
int previousX = 0;
int width = 0;
int height = 0;
int xCoordinate = 0;
int yCoordinate = 0;
for (int i = 0; i < Rectangles.Count; ++i)
{
if ((Rectangles[i].X < Rectangles[i + 1].X) && (Rectangles[i].Height > Rectangles[i + 1].Height))
{
width = Rectangles[i].Width;
height = Rectangles[i].Height - Rectangles[i + 1].Height;
xCoordinate = Rectangles[i].X;
yCoordinate = 250 - height;
Rectangle DrawRec = myRectangleClass.MyRectangle(xCoordinate, yCoordinate, width, height);
RectangleGraphics.DrawRectangle(Pen, DrawRec);
}
}
我需要看看目前的矩形,如果未來矩形更高,然後我知道widt h需要增加,但當它到達一個低於當前高度的矩形時,它需要停止。一旦到達該矩形,它就需要向後移動而不是該系列中的第一個矩形。
如果您有更好的方法來重繪矩形的任何建議或想法,請讓我知道。