1 | /**
|
---|
2 | * DimensionExtent.java
|
---|
3 | * Copyright (C) 2010 New Zealand Digital Library, http://expeditee.org
|
---|
4 | *
|
---|
5 | * This program is free software: you can redistribute it and/or modify
|
---|
6 | * it under the terms of the GNU General Public License as published by
|
---|
7 | * the Free Software Foundation, either version 3 of the License, or
|
---|
8 | * (at your option) any later version.
|
---|
9 | *
|
---|
10 | * This program is distributed in the hope that it will be useful,
|
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
13 | * GNU General Public License for more details.
|
---|
14 | *
|
---|
15 | * You should have received a copy of the GNU General Public License
|
---|
16 | * along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
17 | */
|
---|
18 |
|
---|
19 | package org.expeditee.io.flowlayout;
|
---|
20 |
|
---|
21 | import java.awt.Polygon;
|
---|
22 | import java.awt.Rectangle;
|
---|
23 | import java.awt.geom.Area;
|
---|
24 | import java.util.List;
|
---|
25 |
|
---|
26 | import org.expeditee.items.Item;
|
---|
27 |
|
---|
28 | public class DimensionExtent
|
---|
29 | {
|
---|
30 | public int min;
|
---|
31 | public int max;
|
---|
32 |
|
---|
33 | public DimensionExtent(int min, int max)
|
---|
34 | {
|
---|
35 | this.min = min;
|
---|
36 | this.max = max;
|
---|
37 | }
|
---|
38 |
|
---|
39 | public static DimensionExtent calcMinMaxXExtent(List<Item>items)
|
---|
40 | {
|
---|
41 |
|
---|
42 | int min_x = Integer.MAX_VALUE;
|
---|
43 | int max_x = Integer.MIN_VALUE;
|
---|
44 |
|
---|
45 | for (Item item : items) {
|
---|
46 | Area area = item.getArea();
|
---|
47 | Rectangle rect = area.getBounds();
|
---|
48 | int xl = rect.x;
|
---|
49 | int xr = xl + rect.width-1;
|
---|
50 |
|
---|
51 | min_x = Math.min(min_x, xl);
|
---|
52 | max_x = Math.max(max_x, xr);
|
---|
53 |
|
---|
54 | }
|
---|
55 |
|
---|
56 | DimensionExtent extent = new DimensionExtent(min_x,max_x);
|
---|
57 |
|
---|
58 | return extent;
|
---|
59 | }
|
---|
60 |
|
---|
61 | public static DimensionExtent calcMinMaxYExtent(List<Item>items)
|
---|
62 | {
|
---|
63 |
|
---|
64 | int min_y = Integer.MAX_VALUE;
|
---|
65 | int max_y = Integer.MIN_VALUE;
|
---|
66 |
|
---|
67 | for (Item item : items) {
|
---|
68 | Area area = item.getArea();
|
---|
69 | Rectangle rect = area.getBounds();
|
---|
70 | int yt = rect.y;
|
---|
71 | int yb = yt + rect.height-1;
|
---|
72 |
|
---|
73 | min_y = Math.min(min_y, yt);
|
---|
74 | max_y = Math.max(max_y, yb);
|
---|
75 |
|
---|
76 | }
|
---|
77 |
|
---|
78 | DimensionExtent extent = new DimensionExtent(min_y,max_y);
|
---|
79 |
|
---|
80 | return extent;
|
---|
81 | }
|
---|
82 |
|
---|
83 |
|
---|
84 | public static DimensionExtent calcMinMaxXExtent(Polygon polygon)
|
---|
85 | {
|
---|
86 |
|
---|
87 | int min_x = Integer.MAX_VALUE;
|
---|
88 | int max_x = Integer.MIN_VALUE;
|
---|
89 |
|
---|
90 | int npoints = polygon.npoints;
|
---|
91 |
|
---|
92 | for (int i=0; i<npoints; i++) {
|
---|
93 |
|
---|
94 | int x = polygon.xpoints[i];
|
---|
95 |
|
---|
96 | min_x = Math.min(min_x, x);
|
---|
97 | max_x = Math.max(max_x, x);
|
---|
98 | }
|
---|
99 |
|
---|
100 | DimensionExtent extent = new DimensionExtent(min_x,max_x);
|
---|
101 |
|
---|
102 | return extent;
|
---|
103 | }
|
---|
104 |
|
---|
105 | public static DimensionExtent calcMinMaxYExtent(Polygon polygon)
|
---|
106 | {
|
---|
107 |
|
---|
108 | int min_y = Integer.MAX_VALUE;
|
---|
109 | int max_y = Integer.MIN_VALUE;
|
---|
110 |
|
---|
111 | int npoints = polygon.npoints;
|
---|
112 |
|
---|
113 | for (int i=0; i<npoints; i++) {
|
---|
114 |
|
---|
115 | int y = polygon.ypoints[i];
|
---|
116 |
|
---|
117 | min_y = Math.min(min_y, y);
|
---|
118 | max_y = Math.max(max_y, y);
|
---|
119 | }
|
---|
120 |
|
---|
121 | DimensionExtent extent = new DimensionExtent(min_y,max_y);
|
---|
122 |
|
---|
123 | return extent;
|
---|
124 | }
|
---|
125 |
|
---|
126 | public static Polygon boundingBoxPolygon(List<Item> items)
|
---|
127 | {
|
---|
128 | DimensionExtent xextent = DimensionExtent.calcMinMaxXExtent(items);
|
---|
129 | DimensionExtent yextent = DimensionExtent.calcMinMaxYExtent(items);
|
---|
130 |
|
---|
131 | int xl=xextent.min;
|
---|
132 | int xr=xextent.max;
|
---|
133 | int yt=yextent.min;
|
---|
134 | int yb=yextent.max;
|
---|
135 |
|
---|
136 | int[] xpoints = new int[]{xl,xr,xr,xl};
|
---|
137 | int[] ypoints = new int[]{yt,yt,yb,yb};
|
---|
138 |
|
---|
139 |
|
---|
140 | Polygon polygon = new Polygon(xpoints,ypoints,4);
|
---|
141 |
|
---|
142 | return polygon;
|
---|
143 | }
|
---|
144 | }
|
---|