diff --git a/src/main/java/com/romraider/maps/Table.java b/src/main/java/com/romraider/maps/Table.java index c117ef5c..a3d6162c 100644 --- a/src/main/java/com/romraider/maps/Table.java +++ b/src/main/java/com/romraider/maps/Table.java @@ -1143,34 +1143,18 @@ public abstract class Table extends JPanel implements Serializable { } public void verticalInterpolate() { - horizontalInterpolate(); } public void horizontalInterpolate() { - int[] coords = { getDataSize(), 0}; - DataCell[] tableData = getData(); - - int y; - for (y = 0; y < getDataSize(); y++) { - if (tableData[y].isSelected()) { - if (y < coords[0]) - coords[0] = y; - if (y > coords[1]) - coords[1] = y; - } - } - if (coords[1] - coords[0] > 1) { - double diff = (tableData[coords[0]].getRealValue() - tableData[coords[1]].getRealValue()) / (coords[1] - coords[0]); - if (Math.abs(diff) > 0) { - for (y = coords[0] + 1; y < coords[1]; y++) - data[y].setRealValue(String.valueOf(tableData[y - 1].getRealValue() - diff)); - } - } } public void interpolate() { horizontalInterpolate(); } + + public double linearInterpolation(double x, double x1, double x2, double y1, double y2) { + return (x1 == x2) ? 0.0 : (y1 + (x - x1) * (y2 - y1) / (x2 - x1)); + } public void validateScaling() { if (type != Settings.TABLE_SWITCH) { diff --git a/src/main/java/com/romraider/maps/Table2D.java b/src/main/java/com/romraider/maps/Table2D.java index 7b91b89f..aa8ae97b 100644 --- a/src/main/java/com/romraider/maps/Table2D.java +++ b/src/main/java/com/romraider/maps/Table2D.java @@ -317,7 +317,30 @@ public class Table2D extends Table { @Override public void horizontalInterpolate() { - super.horizontalInterpolate(); + int[] coords = { getDataSize(), 0}; + DataCell[] tableData = getData(); + DataCell[] axisData = getAxis().getData(); + + for (int i = 0; i < getDataSize(); ++i) { + if (tableData[i].isSelected()) { + if (i < coords[0]) + coords[0] = i; + if (i > coords[1]) + coords[1] = i; + } + } + if (coords[1] - coords[0] > 1) { + double x, x1, x2, y1, y2; + x1 = axisData[coords[0]].getValue(); + y1 = tableData[coords[0]].getValue(); + x2 = axisData[coords[1]].getValue(); + y2 = tableData[coords[1]].getValue(); + for (int i = coords[0] + 1; i < coords[1]; ++i) { + x = axisData[i].getValue(); + data[i].setRealValue(String.valueOf(linearInterpolation(x, x1, x2, y1, y2))); + } + } + // Interpolate x axis in case the x axis in selected. this.getAxis().horizontalInterpolate(); } diff --git a/src/main/java/com/romraider/maps/Table3D.java b/src/main/java/com/romraider/maps/Table3D.java index 25e02550..0390cd5b 100644 --- a/src/main/java/com/romraider/maps/Table3D.java +++ b/src/main/java/com/romraider/maps/Table3D.java @@ -784,29 +784,32 @@ public class Table3D extends Table { public void verticalInterpolate() { int[] coords = { getSizeX(), getSizeY(), 0, 0}; DataCell[][] tableData = get3dData(); - - int x, y; - for (x = 0; x < getSizeX(); x++) { - for (y = 0; y < getSizeY(); y++) { - if (tableData[x][y].isSelected()) { - if (x < coords[0]) - coords[0] = x; - if (x > coords[2]) - coords[2] = x; - if (y < coords[1]) - coords[1] = y; - if (y > coords[3]) - coords[3] = y; + DataCell[] axisData = getYAxis().getData(); + int i, j; + for (i = 0; i < getSizeX(); ++i) { + for (j = 0; j < getSizeY(); ++j) { + if (tableData[i][j].isSelected()) { + if (i < coords[0]) + coords[0] = i; + if (i > coords[2]) + coords[2] = i; + if (j < coords[1]) + coords[1] = j; + if (j > coords[3]) + coords[3] = j; } } } if (coords[3] - coords[1] > 1) { - double diff; - for (y = coords[0]; y <= coords[2]; y++) { - diff = (tableData[y][coords[1]].getRealValue() - tableData[y][coords[3]].getRealValue()) / (coords[3] - coords[1]); - if (Math.abs(diff) > 0) { - for (x = coords[1] + 1; x < coords[3]; x++) - tableData[y][x].setRealValue(String.valueOf(tableData[y][x - 1].getRealValue() - diff)); + double x, x1, x2, y1, y2; + x1 = axisData[coords[1]].getValue(); + x2 = axisData[coords[3]].getValue(); + for (i = coords[0]; i <= coords[2]; ++i) { + y1 = tableData[i][coords[1]].getValue(); + y2 = tableData[i][coords[3]].getValue(); + for (j = coords[1] + 1; j < coords[3]; ++j) { + x = axisData[j].getValue(); + tableData[i][j].setRealValue(String.valueOf(linearInterpolation(x, x1, x2, y1, y2))); } } } @@ -818,30 +821,33 @@ public class Table3D extends Table { public void horizontalInterpolate() { int[] coords = { getSizeX(), getSizeY(), 0, 0 }; DataCell[][] tableData = get3dData(); - - int x, y; - for (x = 0; x < getSizeX(); x++) { - for (y = 0; y < getSizeY(); y++) { - if (tableData[x][y].isSelected()) { - if (x < coords[0]) - coords[0] = x; - if (x > coords[2]) - coords[2] = x; - if (y < coords[1]) - coords[1] = y; - if (y > coords[3]) - coords[3] = y; + DataCell[] axisData = getXAxis().getData(); + int i, j; + for (i = 0; i < getSizeX(); ++i) { + for (j = 0; j < getSizeY(); ++j) { + if (tableData[i][j].isSelected()) { + if (i < coords[0]) + coords[0] = i; + if (i > coords[2]) + coords[2] = i; + if (j < coords[1]) + coords[1] = j; + if (j > coords[3]) + coords[3] = j; } } } if (coords[2] - coords[0] > 1) { - double diff; - for (x = coords[1]; x <= coords[3]; x++) { - diff = (tableData[coords[0]][x].getRealValue() - tableData[coords[2]][x].getRealValue()) / (coords[2] - coords[0]); - if (Math.abs(diff) > 0) { - for (y = coords[0] + 1; y < coords[2]; y++) - tableData[y][x].setRealValue(String.valueOf(tableData[y - 1][x].getRealValue() - diff)); - } + double x, x1, x2, y1, y2; + x1 = axisData[coords[0]].getValue(); + x2 = axisData[coords[2]].getValue(); + for (i = coords[1]; i <= coords[3]; ++i) { + y1 = tableData[coords[0]][i].getValue(); + y2 = tableData[coords[2]][i].getValue(); + for (j = coords[0] + 1; j < coords[2]; ++j) { + x = axisData[i].getValue(); + tableData[j][i].setRealValue(String.valueOf(linearInterpolation(x, x1, x2, y1, y2))); + } } } // Interpolate x axis in case the x axis in selected.