File: MapPosition.java

package info (click to toggle)
gpsprune 17-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 3,984 kB
  • ctags: 5,218
  • sloc: java: 39,403; sh: 25; makefile: 17; python: 15
file content (280 lines) | stat: -rw-r--r-- 7,160 bytes parent folder | download | duplicates (4)
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package tim.prune.gui.map;

/**
 * Class to hold the current position of the map
 */
public class MapPosition
{
	/** Width and height of each tile of map */
	private static final int MAP_TILE_SIZE = 256;

	/** x position (scale depends on zoom) */
	private int _xPosition = 0;
	/** y position (scale depends on zoom) */
	private int _yPosition = 0;

	/** Zoom level, from 2 to max */
	private int _zoom = 12;
	/** Factor to zoom by, 2 to the power of zoom */
	private int _zoomFactor = 1 << _zoom;
	/** Maximum zoom level */
	private static final int MAX_ZOOM = 21;


	/**
	 * Zoom and pan to show the selected area
	 * @param inMinX minimum transformed X
	 * @param inMaxX maximum transformed X
	 * @param inMinY minimum transformed Y
	 * @param inMaxY maximum transformed Y
	 * @param inWidth width of display
	 * @param inHeight height of display
	 */
	public void zoomToXY(double inMinX, double inMaxX, double inMinY, double inMaxY, int inWidth, int inHeight)
	{
		// System.out.println("Zooming to " + inMinX + ", " + inMaxX + ", " + inMinY + ", " + inMaxY + "; width=" + inWidth + ", height=" + inHeight);
		double diffX = Math.abs(inMaxX - inMinX);
		double diffY = Math.abs(inMaxY - inMinY);
		// Find out what zoom level to go to
		int requiredZoom = -1;
		for (int currZoom = MAX_ZOOM; currZoom >= 2; currZoom--)
		{
			if (transformToPixels(diffX, currZoom) < inWidth
				&& transformToPixels(diffY, currZoom) < inHeight)
			{
				requiredZoom = currZoom;
				break;
			}
		}
		if (requiredZoom < 2) requiredZoom = 2;
		// Set position
		setZoom(requiredZoom);
		_xPosition = transformToPixels((inMinX + inMaxX) / 2.0);
		_yPosition = transformToPixels((inMinY + inMaxY) / 2.0);
	}

	/**
	 * Ensure that zoom and zoomFactor remain in sync
	 * @param inZoom zoom level to set
	 */
	private void setZoom(int inZoom)
	{
		_zoom = inZoom;
		_zoomFactor = 1 << _zoom;
	}

	/**
	 * Zoom and pan to show the selected area
	 * @param inMinX minimum pixels X
	 * @param inMaxX maximum pixels X
	 * @param inMinY minimum pixels Y
	 * @param inMaxY maximum pixels Y
	 * @param inWidth width of display
	 * @param inHeight height of display
	 */
	public void zoomToPixels(int inMinX, int inMaxX, int inMinY, int inMaxY, int inWidth, int inHeight)
	{
		// System.out.println("Current position is " + _xPosition + ", " + _yPosition);
		int diffX = Math.abs(inMaxX - inMinX);
		int diffY = Math.abs(inMaxY - inMinY);
		// Find out what zoom level to go to
		int requiredZoom = -1;
		int multFactor = 0;
		for (int currZoom = MAX_ZOOM; currZoom >= _zoom; currZoom--)
		{
			multFactor = 1 << (currZoom - _zoom);
			if ((diffX * multFactor) < inWidth && (diffY * multFactor) < inHeight)
			{
				requiredZoom = currZoom;
				break;
			}
		}
		setZoom(requiredZoom);
		// Set position
		_xPosition = (_xPosition - inWidth/2 + (inMinX + inMaxX) / 2) * multFactor;
		_yPosition = (_yPosition - inHeight/2 + (inMinY + inMaxY) / 2) * multFactor;
	}

	/**
	 * Transform a given coordinate into pixels using the current zoom value
	 * @param inValue value to transform
	 * @return pixels
	 */
	private int transformToPixels(double inValue)
	{
		return transformToPixels(inValue, _zoom);
	}

	/**
	 * Transform a given coordinate into pixels using the specified zoom value
	 * @param inValue value to transform
	 * @param inZoom zoom value to use
	 * @return pixels
	 */
	private static int transformToPixels(double inValue, int inZoom)
	{
		return (int) (inValue * MAP_TILE_SIZE * (1 << inZoom));
	}

	/**
	 * Convert pixels back into x coordinates
	 * @param inPixelX x coordinate on screen
	 * @param inWidth current width of window
	 * @return x coordinate
	 */
	public double getXFromPixels(int inPixelX, int inWidth)
	{
		return ((inPixelX - inWidth/2) + _xPosition) * 1.0 / MAP_TILE_SIZE / _zoomFactor;
	}

	/**
	 * Convert pixels back into y coordinates
	 * @param inPixelY y coordinate on screen
	 * @param inHeight current height of window
	 * @return y coordinate
	 */
	public double getYFromPixels(int inPixelY, int inHeight)
	{
		return ((inPixelY - inHeight/2) + _yPosition) * 1.0 / MAP_TILE_SIZE / _zoomFactor;
	}

	/**
	 * Get the horizontal offset from the centre
	 * @param inValue value to transform
	 * @return number of pixels right (+ve) or left (-ve) from the centre
	 */
	public int getXFromCentre(double inValue)
	{
		return transformToPixels(inValue) - _xPosition;
	}

	/**
	 * Get the vertical offset from the centre
	 * @param inValue value to transform
	 * @return number of pixels up (+ve) or down (-ve) from the centre
	 */
	public int getYFromCentre(double inValue)
	{
		return transformToPixels(inValue) - _yPosition;
	}

	/**
	 * Convert a pixel value into a bounds value for sensitivity
	 * @param inPixels number of pixels
	 * @return bounds value to use for x,y checking
	 */
	public double getBoundsFromPixels(int inPixels) {
		return (inPixels * 1.0 / MAP_TILE_SIZE / _zoomFactor);
	}

	/**
	 * Get the leftmost, rightmost, upper and lower index boundaries for the tiles to display
	 * @param inWidth width of window
	 * @param inHeight height of window
	 * @return tile indices as array left, right, up, down
	 */
	public int[] getTileIndices(int inWidth, int inHeight)
	{
		int[] result = new int[4];
		result[0] = getTileIndex(_xPosition - inWidth/2);
		result[1] = getTileIndex(_xPosition + inWidth/2);
		result[2] = getTileIndex(_yPosition - inHeight/2);
		result[3] = getTileIndex(_yPosition + inHeight/2);
		return result;
	}

	/**
	 * Get the pixel offsets for the display
	 * @param inWidth width of window
	 * @param inHeight height of window
	 * @return offsets as x, y
	 */
	public int[] getDisplayOffsets(int inWidth, int inHeight)
	{
		int[] result = new int[2];
		result[0] = getDisplayOffset(_xPosition - inWidth/2);
		result[1] = getDisplayOffset(_yPosition - inHeight/2);
		return result;
	}

	/**
	 * @return x index of the centre tile
	 */
	public int getCentreTileX()
	{
		return getTileIndex(_xPosition);
	}

	/**
	 * @return y index of the centre tile
	 */
	public int getCentreTileY()
	{
		return getTileIndex(_yPosition);
	}

	/**
	 * @param inPosition position of point
	 * @return tile index for that point
	 */
	private int getTileIndex(int inPosition)
	{
		return inPosition / MAP_TILE_SIZE;
	}

	/**
	 * @param inPosition position of point
	 * @return pixel offset for that point
	 */
	private int getDisplayOffset(int inPosition)
	{
		return inPosition % MAP_TILE_SIZE;
		// I thought that &255 would be slightly faster, but it gives the wrong result
	}

	/**
	 * Zoom in one level
	 */
	public void zoomIn()
	{
		if (_zoom < MAX_ZOOM)
		{
			setZoom(_zoom + 1);
			_xPosition *= 2;
			_yPosition *= 2;
		}
	}

	/**
	 * Zoom out one level
	 */
	public void zoomOut()
	{
		if (_zoom >= 3)
		{
			setZoom(_zoom - 1);
			_xPosition /= 2;
			_yPosition /= 2;
		}
	}

	/**
	 * @return current zoom level
	 */
	public int getZoom()
	{
		return _zoom;
	}

	/**
	 * Pan map by the specified amount
	 * @param inDeltaX amount to pan right
	 * @param inDeltaY amount to pan down
	 */
	public void pan(int inDeltaX, int inDeltaY)
	{
		// TODO: Check bounds?
		_xPosition += inDeltaX;
		_yPosition += inDeltaY;
	}
}