@@ -45,9 +45,10 @@ function equalPts(pt1, pt2, xtol, ytol) {
45
45
Math . abs ( pt1 [ 1 ] - pt2 [ 1 ] ) < ytol ;
46
46
}
47
47
48
+ // distance in index units - uses the 3rd and 4th items in points
48
49
function ptDist ( pt1 , pt2 ) {
49
- var dx = pt1 [ 0 ] - pt2 [ 0 ] ,
50
- dy = pt1 [ 1 ] - pt2 [ 1 ] ;
50
+ var dx = pt1 [ 2 ] - pt2 [ 2 ] ,
51
+ dy = pt1 [ 3 ] - pt2 [ 3 ] ;
51
52
return Math . sqrt ( dx * dx + dy * dy ) ;
52
53
}
53
54
@@ -114,9 +115,13 @@ function makePath(pi, loc, edgeflag, xtol, ytol) {
114
115
ptavg ,
115
116
thisdist ;
116
117
117
- // check for points that are too close together (<1/5 the average dist,
118
- // less if less smoothed) and just take the center (or avg of center 2)
119
- // this cuts down on funny behavior when a point is very close to a contour level
118
+ /*
119
+ * Check for points that are too close together (<1/5 the average dist
120
+ * *in grid index units* (important for log axes and nonuniform grids),
121
+ * less if less smoothed) and just take the center (or avg of center 2).
122
+ * This cuts down on funny behavior when a point is very close to a
123
+ * contour level.
124
+ */
120
125
for ( cnt = 1 ; cnt < pts . length ; cnt ++ ) {
121
126
thisdist = ptDist ( pts [ cnt ] , pts [ cnt - 1 ] ) ;
122
127
totaldist += thisdist ;
@@ -174,6 +179,10 @@ function makePath(pi, loc, edgeflag, xtol, ytol) {
174
179
}
175
180
pts . splice ( 0 , cropstart ) ;
176
181
182
+ // done with the index parts - remove them so path generation works right
183
+ // because it depends on only having [xpx, ypx]
184
+ for ( cnt = 0 ; cnt < pts . length ; cnt ++ ) pts [ cnt ] . length = 2 ;
185
+
177
186
// don't return single-point paths (ie all points were the same
178
187
// so they got deleted?)
179
188
if ( pts . length < 2 ) return ;
@@ -252,6 +261,21 @@ function startStep(mi, edgeflag, loc) {
252
261
return [ dx , dy ] ;
253
262
}
254
263
264
+ /*
265
+ * Find the pixel coordinates of a particular crossing
266
+ *
267
+ * @param {object } pi: the pathinfo object at this level
268
+ * @param {array } loc: the grid index [x, y] of the crossing
269
+ * @param {array } step: the direction [dx, dy] we're moving on the grid
270
+ *
271
+ * @return {array } [xpx, ypx, xi, yi]: the first two are the pixel location,
272
+ * the next two are the interpolated grid indices, which we use for
273
+ * distance calculations to delete points that are too close together.
274
+ * This is important when the grid is nonuniform (and most dramatically when
275
+ * we're on log axes and include invalid (0 or negative) values.
276
+ * It's crucial to delete these extra two before turning an array of these
277
+ * points into a path, because those routines require length-2 points.
278
+ */
255
279
function getInterpPx ( pi , loc , step ) {
256
280
var locx = loc [ 0 ] + Math . max ( step [ 0 ] , 0 ) ,
257
281
locy = loc [ 1 ] + Math . max ( step [ 1 ] , 0 ) ,
@@ -263,11 +287,13 @@ function getInterpPx(pi, loc, step) {
263
287
var dx = ( pi . level - zxy ) / ( pi . z [ locy ] [ locx + 1 ] - zxy ) ;
264
288
265
289
return [ xa . c2p ( ( 1 - dx ) * pi . x [ locx ] + dx * pi . x [ locx + 1 ] , true ) ,
266
- ya . c2p ( pi . y [ locy ] , true ) ] ;
290
+ ya . c2p ( pi . y [ locy ] , true ) ,
291
+ locx + dx , locy ] ;
267
292
}
268
293
else {
269
294
var dy = ( pi . level - zxy ) / ( pi . z [ locy + 1 ] [ locx ] - zxy ) ;
270
295
return [ xa . c2p ( pi . x [ locx ] , true ) ,
271
- ya . c2p ( ( 1 - dy ) * pi . y [ locy ] + dy * pi . y [ locy + 1 ] , true ) ] ;
296
+ ya . c2p ( ( 1 - dy ) * pi . y [ locy ] + dy * pi . y [ locy + 1 ] , true ) ,
297
+ locx , locy + dy ] ;
272
298
}
273
299
}
0 commit comments