c# 二维矩形码垛算法代码
时间: 2023-10-04 07:07:42 浏览: 82
C#矩形代码
以下是一个简单的C#二维矩形码垛算法示例:
```csharp
using System;
using System.Collections.Generic;
namespace RectanglePacking
{
class Program
{
static void Main(string[] args)
{
List<Rectangle> rectangles = new List<Rectangle>
{
new Rectangle(1, 2),
new Rectangle(3, 4),
new Rectangle(2, 3),
new Rectangle(4, 5),
new Rectangle(2, 2),
new Rectangle(3, 3),
new Rectangle(1, 1),
new Rectangle(2, 2)
};
List<Rectangle> packedRectangles = PackRectangles(rectangles, 10, 10);
Console.WriteLine("Packed rectangles:");
foreach (Rectangle rectangle in packedRectangles)
{
Console.WriteLine(rectangle);
}
}
static List<Rectangle> PackRectangles(List<Rectangle> rectangles, int maxWidth, int maxHeight)
{
List<Rectangle> packedRectangles = new List<Rectangle>();
// Sort the rectangles by area in descending order
rectangles.Sort((r1, r2) => r2.Area.CompareTo(r1.Area));
while (rectangles.Count > 0)
{
// Try to fit the largest remaining rectangle at the top left corner
Rectangle currentRectangle = rectangles[0];
currentRectangle.Position.X = 0;
currentRectangle.Position.Y = 0;
// Check if the rectangle fits horizontally
if (currentRectangle.Width <= maxWidth)
{
// Check if the rectangle fits vertically
if (currentRectangle.Height <= maxHeight)
{
// Add the rectangle to the list of packed rectangles
packedRectangles.Add(currentRectangle);
// Remove the rectangle from the list of remaining rectangles
rectangles.Remove(currentRectangle);
// Update the maximum height
maxHeight -= currentRectangle.Height;
}
else
{
// The rectangle doesn't fit vertically, try to fit it in the next row
int currentY = 0;
int currentX = currentRectangle.Width;
while (currentY <= maxHeight && rectangles.Contains(currentRectangle))
{
// Check if the rectangle fits vertically
if (currentRectangle.Height <= maxHeight - currentY)
{
// Add the rectangle to the list of packed rectangles
currentRectangle.Position.X = 0;
currentRectangle.Position.Y = currentY;
packedRectangles.Add(currentRectangle);
// Remove the rectangle from the list of remaining rectangles
rectangles.Remove(currentRectangle);
// Update the current Y position and the maximum height
currentY += currentRectangle.Height;
maxHeight -= currentRectangle.Height;
}
else
{
// The rectangle doesn't fit in the current row, move to the next row
currentY += 1;
currentX += currentRectangle.Width;
}
// Update the current rectangle position
currentRectangle.Position.X = currentX;
currentRectangle.Position.Y = currentY;
}
}
}
else
{
// The rectangle doesn't fit horizontally, try to fit it in the next column
int currentX = 0;
int currentY = currentRectangle.Height;
while (currentX <= maxWidth && rectangles.Contains(currentRectangle))
{
// Check if the rectangle fits horizontally
if (currentRectangle.Width <= maxWidth - currentX)
{
// Add the rectangle to the list of packed rectangles
currentRectangle.Position.X = currentX;
currentRectangle.Position.Y = 0;
packedRectangles.Add(currentRectangle);
// Remove the rectangle from the list of remaining rectangles
rectangles.Remove(currentRectangle);
// Update the current X position and the maximum width
currentX += currentRectangle.Width;
maxWidth -= currentRectangle.Width;
}
else
{
// The rectangle doesn't fit in the current column, move to the next column
currentX += 1;
currentY += currentRectangle.Height;
}
// Update the current rectangle position
currentRectangle.Position.X = currentX;
currentRectangle.Position.Y = currentY;
}
}
}
return packedRectangles;
}
}
class Rectangle
{
public int Width { get; set; }
public int Height { get; set; }
public Vector2 Position { get; set; }
public int Area { get { return Width * Height; } }
public Rectangle(int width, int height)
{
Width = width;
Height = height;
Position = new Vector2();
}
public override string ToString()
{
return string.Format("({0}, {1}), ({2}, {3})", Position.X, Position.Y, Width, Height);
}
}
class Vector2
{
public int X { get; set; }
public int Y { get; set; }
public Vector2()
{
X = 0;
Y = 0;
}
public Vector2(int x, int y)
{
X = x;
Y = y;
}
}
}
```
使用上面的代码,你可以将一个矩形列表放入一个给定的矩形区域内,以最小化未使用的空间。
阅读全文