Java 3D数组找到最近的点?
Java 3D array find the nearest point?
正在尝试查找从具有“坐标 [x,y]s”的 12 个元素的 3D 数组生成的最近点:
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;
}
该方法应该生成中心坐标的 3D 数组,然后将它们与输入值 xPos
和 yPos
进行比较,以找到最近的点并将其作为数组输出。图中所示方法的假定概念:
红点作为输入的xPos和yPos。尝试找到最近的中心点(在 3D 数组中生成)(绿色)然后输出其坐标
问题是,在尝试 运行 一个 Java @test 时(nearSquare 方法在 class 中称为“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]);
}
}
测试将失败说明expected: <7> but was: <0>
和org.opentest4j.AssertionFailedError: expected: <7> but was: <0>
如果是由于“nearSquare”方法没有正确过滤最近的坐标,输出最近点的正确方法是什么输入坐标?
据我了解(我可能是错的),您遇到的是 2D 问题而不是 3D 问题,因为您进入该方法的点没有 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]
但是您的测试和方法名称表明您要确定 n 和 m:
nearSquare(0,0) -> (0,0)
nearSquare(0,300) -> (0,7)
所以让我们编写代码:
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)
+ ")"
);
}
}
并展示它给 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)
$
正在尝试查找从具有“坐标 [x,y]s”的 12 个元素的 3D 数组生成的最近点:
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;
}
该方法应该生成中心坐标的 3D 数组,然后将它们与输入值 xPos
和 yPos
进行比较,以找到最近的点并将其作为数组输出。图中所示方法的假定概念:
红点作为输入的xPos和yPos。尝试找到最近的中心点(在 3D 数组中生成)(绿色)然后输出其坐标
问题是,在尝试 运行 一个 Java @test 时(nearSquare 方法在 class 中称为“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]);
}
}
测试将失败说明expected: <7> but was: <0>
和org.opentest4j.AssertionFailedError: expected: <7> but was: <0>
如果是由于“nearSquare”方法没有正确过滤最近的坐标,输出最近点的正确方法是什么输入坐标?
据我了解(我可能是错的),您遇到的是 2D 问题而不是 3D 问题,因为您进入该方法的点没有 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]
但是您的测试和方法名称表明您要确定 n 和 m:
nearSquare(0,0) -> (0,0)
nearSquare(0,300) -> (0,7)
所以让我们编写代码:
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)
+ ")"
);
}
}
并展示它给 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)
$