Row.java 4.49 KB
Newer Older
Florian Grabowski's avatar
Florian Grabowski committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package de._82grfl1bif.KPI_Visualizer.layouts.TreeMap;

import de._82grfl1bif.KPI_Visualizer.structures.Structure;

import java.awt.*;
import java.util.HashMap;
import java.util.Map;

public class Row {

    double broad;
    boolean horizontal;

    public HashMap<Rectangle, Structure> getRectangles() {
        return rectangles;
    }

    HashMap<Rectangle, Structure> rectangles;
    Rectangle space;

    public Row(Rectangle space) {
        this.space = space;
        this.rectangles = new HashMap<>();
        this.broad = 0;
        this.horizontal = (space.getWidth() >= space.getHeight());
    }

    private double getSideRelation() {
        double currentMax = 0;
        for (Map.Entry<Rectangle, Structure> entry : rectangles.entrySet()) {
            Rectangle r = entry.getKey();
            currentMax = Math.max(r.getSideRelation(), currentMax);
        }
        return currentMax;
    }

    public void addRectangle(double area, Structure structure) {
        double currentArea = getCurrentArea() + area;
        if (horizontal) {
            this.broad = currentArea / space.getHeight(); //calculating the new broad
            int currentHeight = this.space.getOrigin().y; //new temp for height of origins
            for (Map.Entry<Rectangle, Structure> entry : rectangles.entrySet()) {
                Rectangle r = entry.getKey();
                r.setWidth(this.broad);//setting new width&Height for all existing rectangles
                r.setOrigin(new Point(this.space.getOrigin().x, currentHeight));//setting new origins for all existing rectangles
                currentHeight += (int) Math.round(r.getHeight());//for next origin
            }
            rectangles.put((new Rectangle(area, this.broad, new Point(this.space.getOrigin().x, currentHeight))), structure);//add new Rectangle
        } else {
            this.broad = currentArea / space.getWidth(); //calculating the new broad
            int currentWidth = this.space.getOrigin().x; //new temp for width of origins
            for (Map.Entry<Rectangle, Structure> entry : rectangles.entrySet()) {
                Rectangle r = entry.getKey();
                r.setHeight(this.broad);//setting new width&Height for all existing rectangles
                r.setOrigin(new Point(currentWidth, space.getOrigin().y));//setting new origins for all existing rectangles
                currentWidth += (int) Math.round(r.getHeight());//for next origin
            }
            rectangles.put(new Rectangle(area, area / this.broad, new Point(currentWidth, space.getOrigin().y)), structure);
        }
    }

    private double getCurrentArea() {
        double currentArea = 0;
        for (Map.Entry<Rectangle, Structure> entry : rectangles.entrySet()) {
            Rectangle r = entry.getKey();
            currentArea += r.getArea();
        }
        return currentArea;
    }

    public boolean checkInsert(double area) {
        if (!rectangles.isEmpty()) {
            double currentArea = getCurrentArea() + area;
            double broadTemp = horizontal ? currentArea / space.getHeight() : currentArea / space.getWidth();
            for (Map.Entry<Rectangle, Structure> entry : rectangles.entrySet()) {
                Rectangle r = entry.getKey();
                if (!r.checkSideRelation(broadTemp))
                    return false;//returns false if one of the existing rectangles gets worse
            }
            return !(new Rectangle(area, broad, new Point(0, 0)).getSideRelation() > getSideRelation()); //returns false if the inserted rectangle is worse, than any other before
        }else {
            return true;
        }
    }

    public Rectangle getLeftover() {
        Point origin;
        double width;
        if (horizontal) {
            origin = new Point(space.getOrigin().x + (int)(broad), space.getOrigin().y);
            width = space.getWidth() - getWidth();
        } else {
            origin = new Point(space.getOrigin().x, space.getOrigin().y + (int)(broad));
            width = getWidth();
        }
        return new Rectangle(this.space.getArea() - getCurrentArea(), width, origin);
    }

    public double getWidth() {
        if (horizontal) {
            return this.broad;
        } else {
            return this.space.getWidth();
        }
    }

    public double getHeight() {
        if (horizontal) {
            return this.space.getHeight();
        } else {
            return this.broad;
        }
    }

    public boolean isFull(){
        return (Math.round(this.space.getArea()) <= Math.round(this.getCurrentArea()));
    }
}