Is trying to find the nearest point that is generated from a 3D array with 12 elements of "coordinate [x,y]s":
public int[] nearSquare (int xPos, int yPos) {
int len = 40;
int[][][] loc = new int[12][12][2];
int[] output = new int[2];
int boundary = len/2;
for(int size = 0,i = len/2; size < 12; size ,i = len) {
for(int size2 = 0,j = len/2; size2 < 12; size2 ,j = len) {
loc[size][size2][0] = j;
loc[size][size2][1] = i;
}
}
for (int row = 0; row < loc.length; row ) {
for (int col = 0; col < loc[row].length; col ) {
if (loc[row][col][0] - boundary < xPos && loc[row][col][0] boundary > xPos && loc[row][col][1] - boundary < yPos && loc[row][col][1] boundary > yPos) {
output[0] = loc[row][col][0];
output[1] = loc[row][col][1];
} else {
continue;
}
}
}
return output;
}
The method supposed to generate 3D array of center coordinates, then compare them with the input value xPos
and yPos
to find the nearest point and output it as an array. The supposed concept of the method shown in the image:
Red point as the input xPos and yPos. Trying to find the nearest center-point(generated in the 3D array)(green) then output its coordinate
The issue is, when trying to run a Java @test (nearSquare method is within the class called "shapeTest"):
public class Test
{
shapeTest st;
public Test() {}
@BeforeEach
public void setUp()
{
st = new shapeTest();
}
// Trying to test here...
@Test
public void testnearSquare() {
int[] test1 = new int[2];
int[] test1val = st.nearSquare(0,0);
assertEquals(test1[0], test1val[0]);
assertEquals(test1[1], test1val[1]);
int[] test2 = new int[2];
test2[1] = 7;
int[] test2val = st.nearSquare(0,300);
assertEquals(test2[0], test2val[0]);
assertEquals(test2[1], test2val[1]);
}
}
The test will fail stating that expected: <7> but was: <0>
and org.opentest4j.AssertionFailedError: expected: <7> but was: <0>
If it's due to "nearSquare" method did not filter the nearest coordinate correctly, what are the correct ways to output the nearest point to the input coordinate?
CodePudding user response:
As far as I understand it (and I might be wrong), you have a 2D problem and not a 3D problem as the point you enter into the method has no z-coordinate.
Given point (x,y), determine nearest point (x',y') with
x' in {20, 60, 100, 140, ..., 460} or n * 40 20 with n in [0,...,11]
and
y' in {20, 60, 100, 140, ..., 460} or m * 40 20 with m in [0,...,11]
But your tests and the name of the method indicate that you want to determine n and m:
nearSquare(0,0) -> (0,0)
nearSquare(0,300) -> (0,7)
So let's code this:
import java.util.*;
public class NearSquare {
static final int squareLength = 40;
static final int numberOfSquares = 12; // in one dimension
public static int[] nearSquare(int xPos, int yPos) {
int n, m;
// determine n
if (xPos <= 0) {
n = 0; // min index
} else if (xPos >= squareLength * numberOfSquares) {
n = numberOfSquares - 1; // max index
} else {
n = xPos / squareLength;
}
// determine m
if (yPos <= 0) {
m = 0; // min index
} else if (yPos >= squareLength * numberOfSquares) {
m = numberOfSquares - 1; // max index
} else {
m = (yPos - 20) / squareLength;
}
return new int[] {n, m};
}
public static void main(String[] args)
{
int square1[] = nearSquare(0, 0);
System.out.println("Test 1: nearSquare(0,0) -> (0,0): " (square1[0] == 0 && square1[1] == 0 ? "passed" : "failed"));
System.out.println("Result: " Arrays.toString(square1));
System.out.println("Nearest center point: ("
(square1[0]*squareLength squareLength/2)
", "
(square1[1]*squareLength squareLength/2)
")"
);
int square2[] = nearSquare(0, 300);
System.out.println("Test 2: nearSquare(0,0) -> (0,7): " (square2[0] == 0 && square2[1] == 7 ? "passed" : "failed"));
System.out.println("Result: " Arrays.toString(square2));
System.out.println("Nearest center point: ("
(square2[0]*squareLength squareLength/2)
", "
(square2[1]*squareLength squareLength/2)
")"
);
}
}
And show what it brings to the table:
$ java NearSquare.java
Test 1: nearSquare(0,0) -> (0,0): passed
Result: [0, 0]
Nearest center point: (20, 20)
Test 2: nearSquare(0,0) -> (0,7): passed
Result: [0, 7]
Nearest center point: (20, 300)
$