#region License Information
/* HeuristicLab
* Copyright (C) 2002-2015 Joseph Helm and Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System.Collections.Generic;
using System.Linq;
using SharpDX;
namespace PackingPlanVisualizations {
public class CenteredContainer2D {
private Vector2 containerSize;
private float xScaling;
private float yScaling;
private readonly List packingItems = new List();
private struct PackingItem {
internal readonly Vector2 position;
internal readonly Vector2 size;
internal readonly string label;
public PackingItem(Vector2 size, Vector2 position, string label) {
this.size = size;
this.position = position;
this.label = label;
}
}
public CenteredContainer2D(Vector2 controlSize, Vector2 containerSize) {
this.containerSize = containerSize;
UpdateContainer(controlSize);
}
public CenteredContainer2D(float controlWidth, float controlHeight, float containerWidth, float containerHeight)
: this(new Vector2(controlWidth, controlHeight), new Vector2(containerWidth, containerHeight)) {
}
public void AddItem(Vector2 size, Vector2 position, string label) {
packingItems.Add(new PackingItem(size, position, label));
}
public void AddItem(float width, float height, float x, float y, string label) {
AddItem(new Vector2(width, height), new Vector2(x, y), label);
}
public void UpdateContainer(Vector2 controlSize) {
xScaling = (controlSize.X - 4) / (containerSize.X); // leave 2 pixel space on each side
yScaling = (controlSize.Y - 4) / (containerSize.Y);
}
public void UpdateContainer(float controlWidth, float controlHeight) {
UpdateContainer(new Vector2(controlWidth, controlHeight));
}
public RectangleF GetContainerData() {
//Compute centered rectangle
return new RectangleF(2, 2, containerSize.X * xScaling, containerSize.Y * yScaling);
}
public struct LabeledRectangle {
public string label;
public RectangleF rectangle;
public LabeledRectangle(string label, RectangleF rectangle) {
this.label = label;
this.rectangle = rectangle;
}
}
public List GetItemRectangles() {
return packingItems.Select(item =>
new LabeledRectangle(item.label,
new RectangleF(item.position.X * xScaling + 2, item.position.Y * yScaling + 2, item.size.X * xScaling, item.size.Y * yScaling)))
.ToList();
}
}
}