二维装箱

    //节点
    public class Node
    {
        public int X { get; set; }
        public int Y { get; set; }
        public int W { get; set; }
        public int H { get; set; }
        public bool IsUsed { get; set; }
        public Node Right { get; set; }
        public Node Down { get; set; }
        public DataModel Data { get; set; }
    }
    //数据源
    public class DataModel
    {
        public int W { get; set; }
        public int H { get; set; }
        public int Area { get; set; }
        public bool IsUsed { get; set; }
    }
    public class Core
    {
        private Node m_Root;
        private int m_TemplateWidth;
        private int m_TemplateHeight;
        public List<Model.DataModel> Data;
        public Core(List<DataModel> Datas,int TemplateWidth,int TemplateHeight)
        {
            this.Data = Datas;
            this.m_TemplateWidth = TemplateWidth;
            this.m_TemplateHeight = TemplateHeight;
        }

        public Node Paking()
        {
            this.m_Root = new Node() { W = this.m_TemplateWidth, H = this.m_TemplateHeight };
            foreach (var item in this.Data)
            {
                if (!item.IsUsed)//该数据已经包装
                {
                    var CurrentNode = this.FindNode(this.m_Root, item);

                    if (CurrentNode != null)
                    {
                        this.SplitNode(CurrentNode, item);
                        item.IsUsed = true;
                    }
                }
            }

            return this.m_Root;
        }

        public Node FindNode(Node root, Model.DataModel model)
        {
            if (root.IsUsed)
            {
                //往右摆放是否有空间
                var node = FindNode(root.Right, model);
                if (node == null)
                {
                    //往下摆放是否有空间
                    node = FindNode(root.Down, model);
                }
                return node;
            }
			//无需旋转
            else if (model.W <= root.W && model.H <= root.H)
            {
				return root;
            }
			//宽高交换
            else if (model.W <= root.H && model.H <= root.W)
            {
                int Temp = model.W;
                model.W = model.H;
                model.H = Temp;
                model.IsRotate = true;
                return root;
            }
            else
            {
                return null;
            }
        }

        public Node SplitPlate(Node node, Model.DataModel model)
        {
            node.IsUsed = true;
            node.Data = model;
            node.Down = new Node { W = node.W, H = node.H - model.H, X = node.X, Y = node.Y + model.H };
            node.Right = new Node { W = node.W - model.W, H = model.H, X = node.X + model.W, Y = node.Y };

            return node;
        }
    }
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            List<Model.DataModel> Data;//绑定数据源
            int TemplateWidth = 1024,int TemplateHeight = 762;//容器
            Core core =new Core(Data.OrderBy(u=>u.Area).ToList(),TemplateWidth, TemplateHeight);
            Node root = core.Paking();
            //递归输出数据
        }
    }

1.对数据源倒序

2.通过摆放最大的体积

3.将最大体积摆放后的下和上拆分成两个块,进行递归摆放。

评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值