+/* return the nearest waypoint (any type) */
+int8_t xycoord_to_ijcoord(int16_t *xp, int16_t *yp, uint8_t *ip, uint8_t *jp)
+{
+ int16_t x, y;
+ uint8_t i, j;
+
+ x = *xp;
+ y = *yp;
+
+ x -= OFFSET_CORN_X;
+ x += (STEP_CORN_X/2);
+ i = x / STEP_CORN_X;
+
+ y = COLOR_Y(y); /* Y depends on color */
+ y -= OFFSET_CORN_Y;
+ y += STEP_CORN_Y/2;
+
+ if ((i & 1) == 1)
+ y -= STEP_CORN_Y/2;
+ j = y / STEP_CORN_Y;
+
+ if (ijcoord_to_xycoord(i, j, &x, &y) < 0)
+ return -1;
+
+ *xp = x;
+ *yp = y;
+ *ip = i;
+ *jp = j;
+
+ return 0;
+}
+