我正在尝试编写一个自定义函数来循环遍历数组列表并比较四边形的四个边。每边只需要一对一比较,但我似乎无法找出循环它们的正确方法。我画了一张附在下面的图表,但我似乎无法实现正确的循环来做我想做的事情。我制作了 Bounds 类的简化版本,仅用于测试,它使用 getSide(1-4) 方法来获取边 1:North,2:East,3:South,4:West。现在我只想打印侧面以进行测试。即 (1 -> 11, 1 -> 11, 2 -> 8, 2 -> 12... 等)
编辑:我忘了提到最小的数字是我试图让我的算法通过的顺序。也可能这棵树不像我希望的那样清晰,线条基本上是向下比较四边形的每一边和它的对面。所以第一个序列是:矩形 1:1 -> 7,然后是 1 -> 11,然后移动到东边并将其与西边进行比较,依此类推,直到它通过所有边。
/* Program I'm using to test my algorithm */
public static void test()
{
BoundTest a,b,c,d,e,f;
a = new BoundTest("Rectangle 1","A","B","C","D");
b = new BoundTest("Rectangle 2","E","F","G","H");
c = new BoundTest("Rectangle 3","I","J","K","L");
ArrayList<BoundTest> x = new ArrayList();
x.add(0,a);
x.add(1,b);
x.add(2,c);
System.out.println(x.size());
for(int i = 0; i <= (x.size() - 2); i++)
{
System.out.println(x.get(i).getName());
}
}
/* Bounding Class */
public class BoundTest
{
private String north,east,south,west,name;
public BoundTest(String name,String a,String b,String c,String d)
{
this.name = "Name:" + name;
this.north = "North:" + a;
this.east = "East: " + b;
this.south = "South:" + c;
this.west = "West: " + d;
}
/* Get side */
public String getSide(int a)
{
String returnName;
switch(a)
{
case 1:
returnName = this.north;
break;
case 2:
returnName = this.east;
break;
case 3:
returnName = this.south;
break;
case 4:
returnName = this.west;
break;
default:
returnName = this.north;
break;
}
return returnName;
}
}