1 package tim.prune.data;
4 * Class to hold information about the mid-points between
5 * adjacent track points. Used by the MapCanvas for creating
8 public class MidpointData
11 private Track _track = null;
13 private double[] _xValues = null;
14 private double[] _yValues = null;
16 private boolean[] _valids = null;
17 // Flag to set data stale
18 private boolean _needRefresh = true;
22 * Flag the data as needing to be updated
23 * @param inTrack track object from which to get the data
25 public void updateData(Track inTrack)
32 * Update the arrays of data from the track
34 private synchronized void updateData()
37 if (_track == null) return;
38 // Make arrays the right size
39 final int numPoints = _track.getNumPoints();
40 if (_xValues == null || _xValues.length != numPoints)
42 _xValues = new double[numPoints];
43 _yValues = new double[numPoints];
44 _valids = new boolean[numPoints];
46 if (numPoints <= 0) return;
49 // Loop over the points in the track
50 for (int i=1; i<numPoints; i++)
52 boolean pointValid = false;
53 DataPoint point = _track.getPoint(i);
54 if (point != null && !point.getSegmentStart() && !point.isWaypoint())
56 _xValues[i] = (_track.getX(i) + _track.getX(i-1)) / 2.0;
57 _yValues[i] = (_track.getY(i) + _track.getY(i-1)) / 2.0;
60 _valids[i] = pointValid;
65 * Find the nearest point to the specified x and y coordinates
66 * or -1 if no point is within the specified max distance
67 * @param inX x coordinate
68 * @param inY y coordinate
69 * @param inMaxDist maximum distance from selected coordinates
70 * @return index of nearest point or -1 if not found
72 public int getNearestPointIndex(double inX, double inY, double inMaxDist)
74 if (_track == null) return -1;
75 if (_needRefresh) updateData();
76 final int numPoints = _track.getNumPoints();
78 double nearestDist = -1.0;
80 for (int i=1; i < numPoints; i++)
84 currDist = Math.abs(_xValues[i] - inX) + Math.abs(_yValues[i] - inY);
85 if (currDist < nearestDist || nearestDist < 0.0)
88 nearestDist = currDist;
92 // Check whether it's within required distance
93 if (nearestDist > inMaxDist && inMaxDist > 0.0) {