Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.BinPacking/PackingPlanVisualizations/2D/CenteredContainer2D.cs @ 13497

Last change on this file since 13497 was 13497, checked in by gkronber, 8 years ago

#1966: fixed various problems: bugs in cloning, bugs in persistence, method names, various minor improvements of source code for readability.

File size: 3.3 KB
RevLine 
[13032]1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 Joseph Helm and Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
[13028]22using System.Collections.Generic;
[13477]23using System.Linq;
[13028]24using SharpDX;
25
[13032]26
[13028]27namespace PackingPlanVisualizations {
28  public class CenteredContainer2D {
29    private Vector2 containerSize;
[13468]30    private float xScaling;
31    private float yScaling;
32    private readonly List<PackingItem> packingItems = new List<PackingItem>();
[13028]33
34    private struct PackingItem {
[13477]35      internal readonly Vector2 position;
36      internal readonly Vector2 size;
37      internal readonly string label;
[13468]38      public PackingItem(Vector2 size, Vector2 position, string label) {
[13028]39        this.size = size;
40        this.position = position;
41        this.label = label;
42      }
43    }
44
[13468]45    public CenteredContainer2D(Vector2 controlSize, Vector2 containerSize) {
[13028]46      this.containerSize = containerSize;
47      UpdateContainer(controlSize);
48    }
[13468]49    public CenteredContainer2D(float controlWidth, float controlHeight, float containerWidth, float containerHeight)
50      : this(new Vector2(controlWidth, controlHeight), new Vector2(containerWidth, containerHeight)) {
[13028]51    }
52
53    public void AddItem(Vector2 size, Vector2 position, string label) {
[13468]54      packingItems.Add(new PackingItem(size, position, label));
[13028]55    }
56    public void AddItem(float width, float height, float x, float y, string label) {
[13468]57      AddItem(new Vector2(width, height), new Vector2(x, y), label);
[13028]58    }
[13468]59
[13028]60    public void UpdateContainer(Vector2 controlSize) {
[13477]61      xScaling = (controlSize.X - 4) / (containerSize.X); // leave 2 pixel space on each side
62      yScaling = (controlSize.Y - 4) / (containerSize.Y);
[13028]63    }
64    public void UpdateContainer(float controlWidth, float controlHeight) {
[13468]65      UpdateContainer(new Vector2(controlWidth, controlHeight));
[13028]66    }
67
68
69    public RectangleF GetContainerData() {
70      //Compute centered rectangle
[13477]71      return new RectangleF(2, 2, containerSize.X * xScaling, containerSize.Y * yScaling);
[13028]72    }
73
74    public struct LabeledRectangle {
75      public string label;
76      public RectangleF rectangle;
77
78      public LabeledRectangle(string label, RectangleF rectangle) {
79        this.label = label;
80        this.rectangle = rectangle;
81      }
82    }
[13468]83
[13028]84    public List<LabeledRectangle> GetItemRectangles() {
[13477]85      return packingItems.Select(item =>
86        new LabeledRectangle(item.label,
87          new RectangleF(item.position.X * xScaling + 2, item.position.Y * yScaling + 2, item.size.X * xScaling, item.size.Y * yScaling)))
88      .ToList();
[13028]89    }
90  }
91}
Note: See TracBrowser for help on using the repository browser.