public int minYDistance(int y) {
    if (type == TYPE_POLY) {
        int p=10000;
        for (int i=0; i<poly.npoints; i++) {
            int np=poly.ypoints[i];
            if (Math.abs(y-np) < Math.abs(y-p))
                p=np;
        }
        return p;
    } else if (type == TYPE_RECT) {
        int[] points = {rect.y, rect.y+rect.height};
        int p=10000;
        for (int i=0; i<points.length; i++)
            if (Math.abs(y-points[i]) < Math.abs(y-p))
                p=points[i];
        return p;
    } else {  // type == TYPE_CIRCLE
        int[] points = {circle_center.y, circle_center.y+circle_r, circle_center.y-circle_r};
        int p=10000;
        for (int i=0; i<points.length; i++)
            if (Math.abs(y-points[i]) < Math.abs(y-p))
                p=points[i];
        return p;
    }
  }