package sparseviewer.interp; public class Grid2D { private final int width; private final int height; private final double xmin; private final double xmax; private final double ymin; private final double ymax; public Grid2D(int width, int height, double xmin, double xmax, double ymin, double ymax) { this.width = width; this.height = height; this.xmin = xmin; this.xmax = xmax; this.ymin = ymin; this.ymax = ymax; } public int getWidth() { return width; } public int getHeight() { return height; } public double getXMin() { return xmin; } public double getXMax() { return xmax; } public double getYMin() { return ymin; } public double getYMax() { return ymax; } public double xAt(int ix) { if (width <= 1) { return xmin; } return xmin + ix * (xmax - xmin) / (double) (width - 1); } public double yAt(int iy) { if (height <= 1) { return ymin; } return ymin + iy * (ymax - ymin) / (double) (height - 1); } public int xToIndex(double x) { if (xmax == xmin) { return 0; } double t = (x - xmin) / (xmax - xmin); int ix = (int) Math.round(t * (width - 1)); if (ix < 0) { ix = 0; } if (ix >= width) { ix = width - 1; } return ix; } public int yToIndex(double y) { if (ymax == ymin) { return 0; } double t = (y - ymin) / (ymax - ymin); int iy = (int) Math.round(t * (height - 1)); if (iy < 0) { iy = 0; } if (iy >= height) { iy = height - 1; } return iy; } }