summaryrefslogtreecommitdiff
path: root/src/net/sourceforge/plantuml/geom/SpiderWeb.java
blob: fe4815ff80ab50203b4210bd8547b1387328ee65 (plain)
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/* ========================================================================
 * PlantUML : a free UML diagram generator
 * ========================================================================
 *
 * (C) Copyright 2009-2020, Arnaud Roques
 *
 * Project Info:  http://plantuml.com
 * 
 * If you like this project or if you find it useful, you can support us at:
 * 
 * http://plantuml.com/patreon (only 1$ per month!)
 * http://plantuml.com/paypal
 * 
 * This file is part of PlantUML.
 *
 * PlantUML 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.
 *
 * PlantUML 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 this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
 * USA.
 *
 *
 * Original Author:  Arnaud Roques
 * 
 *
 */
package net.sourceforge.plantuml.geom;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import net.sourceforge.plantuml.Log;

public class SpiderWeb {

	private final int pointsInCircle = 16;
	private int nbRow;
	private int nbCol;

	final private int widthCell;
	final private int heightCell;

	final private int xMargin = 50;
	final private int yMargin = 50;

	private final List<PolylineBreakeable> lines = new ArrayList<PolylineBreakeable>();

	public SpiderWeb(int widthCell, int heightCell) {
		Log.info("widthCell=" + widthCell + " heightCell=" + heightCell);
		this.widthCell = widthCell;
		this.heightCell = heightCell;
	}

	public Point2DInt getMainPoint(int row, int col) {
		return new Point2DInt(col * (widthCell + xMargin), row * (heightCell + yMargin));
	}

	public Collection<Point2DInt> getHangPoints(int row, int col) {
		// final double dist = Math.pow(1.6, -row - 10) + Math.pow(1.5, -col -
		// 10);
		assert pointsInCircle % 4 == 0;
		final List<Point2DInt> result = new ArrayList<Point2DInt>();
		final int dist = (int) Math.round(Math.sqrt(widthCell * widthCell + heightCell * heightCell) / 10);
		for (int i = 0; i < pointsInCircle; i++) {
			final Point2DInt main = getMainPoint(row, col);
			final int x = main.getXint();
			final int y = main.getYint();
			if (i == 0) {
				result.add(new Point2DInt(x + dist, y));
			} else if (i == pointsInCircle / 4) {
				result.add(new Point2DInt(x, y + dist));
			} else if (i == 2 * pointsInCircle / 4) {
				result.add(new Point2DInt(x - dist, y));
			} else if (i == 3 * pointsInCircle / 4) {
				result.add(new Point2DInt(x, y - dist));
			} else {
				final double angle = Math.PI * 2.0 * i / pointsInCircle;
				final double x1 = x + dist * Math.cos(angle);
				final double y1 = y + dist * Math.sin(angle);
				result.add(new Point2DInt((int) Math.round(x1), (int) Math.round(y1)));
			}
		}
		// Log.println("getHangPoints="+result);
		return result;
	}

	public PolylineBreakeable addPolyline(int row1, int col1, int row2, int col2) {
		// Log.println("SpiderWeb : adding " + row1 + "," + col1 + " - "
		// + row2 + "," + col2);
		final PolylineBreakeable result = computePolyline(row1, col1, row2, col2);
		// Log.println("SpiderWeb : adding " + result);
		if (result != null) {
			lines.add(result);
		}
		return result;
	}

	private PolylineBreakeable computePolyline(int row1, int col1, int row2, int col2) {
		if (row1 > nbRow) {
			nbRow = row1;
		}
		if (row2 > nbRow) {
			nbRow = row2;
		}
		if (col1 > nbCol) {
			nbCol = col1;
		}
		if (col2 > nbCol) {
			nbCol = col2;
		}
		if (directLinkPossibleForGeometry(row1, col1, row2, col2)) {
			// Log.println("Geom OK");
			final PolylineBreakeable direct = new PolylineBreakeable(getMainPoint(row1, col1), getMainPoint(row2, col2));
			if (isCompatible(direct)) {
				// Log.println("Direct OK");
				return direct;
			}
		}
		return bestLevel1Line(row1, col1, row2, col2);
	}

	private boolean isCompatible(PolylineBreakeable toTest) {
		for (PolylineBreakeable p : lines) {
			if (p.doesTouch(toTest)) {
				return false;
			}
		}
		return true;
	}

	private PolylineBreakeable bestLevel1Line(int row1, int col1, int row2, int col2) {
		PolylineBreakeable result = null;
		for (int u = 5; u <= 95; u += 5) {
			for (int d = -200; d <= 200; d += 5) {
				final PolylineBreakeable cur = new PolylineBreakeable(getMainPoint(row1, col1),
						getMainPoint(row2, col2));
				cur.insertBetweenPoint(u, d);
				if ((result == null || cur.getLength() < result.getLength()) && isCompatible(cur)) {
					result = cur;
				}

			}
		}
		return result;
	}

	boolean directLinkPossibleForGeometry(int row1, int col1, int row2, int col2) {
		final int rowMin = Math.min(row1, row2);
		final int rowMax = Math.max(row1, row2);
		final int colMin = Math.min(col1, col2);
		final int colMax = Math.max(col1, col2);
		final LineSegmentInt seg = new LineSegmentInt(col1, row1, col2, row2);
		for (int r = rowMin; r <= rowMax; r++) {
			for (int c = colMin; c <= colMax; c++) {
				if (r == row1 && c == col1) {
					continue;
				}
				if (r == row2 && c == col2) {
					continue;
				}
				if (seg.containsPoint(new Point2DInt(c, r))) {
					return false;
				}
			}
		}
		return true;
	}

	final int getPointsInCircle() {
		return pointsInCircle;
	}

}