我想将随机向量添加到一个数组中,该数组有 n elements.HoweverI 想要一个新的随机 Vertor 和数组中的元素之间的距离> 4f
I want to add random vector to an array that has n elements.HoweverI want a distance between a new random Vertor and elements in the array>4f
这是我的代码,显然它不正确?有没有其他方法可以在不重叠的情况下在随机位置创建对象?谢谢你的时间。
public ArrayList getVector3f() {
boolean lessThan = false;
// create first random vector & add it to the list array
Vector3f v1 = randVector();
list.add(v1);
//num=4;
for (int i = 0; i <= num - 2; i++) {
vec = randVector();
for (int j = 0; j <= list.size() - 1 ; j++) {
if (list.get(j).distance(vec) < 4f) {
lessThan = true;
}
}
if (lessThan == true) {
vec = randVector();
for (int j = 0; j <= list.size() - 1; j++) {
if (list.get(j).distance(vec) < 4f) {
lessThan = true;
} else {
lessThan = false;
}
}
}
if (lessThan == false) {
list.add(vec);
System.out.println(vec);
}
}
return list;
}
你似乎有 "unrolled your loop"。试试这个(未经测试):-
public ArrayList getVector3f() {
// create first random vector & add it to the list array
Vector3f vec = randVector();
list.add(vec);
for (int i = 0; i <= num-2; i++) {
boolean lessThan = true;
while (lessThan) {
lessThan = false;
vec = randVector();
for (int j = 0; j < list.size() ; j++) {
if (list.get(j).distance(vec) < 4f) {
lessThan = true;
break;
}
}
}
list.add(vec);
System.out.println(vec);
}
return list;
}
这是我的代码,显然它不正确?有没有其他方法可以在不重叠的情况下在随机位置创建对象?谢谢你的时间。
public ArrayList getVector3f() {
boolean lessThan = false;
// create first random vector & add it to the list array
Vector3f v1 = randVector();
list.add(v1);
//num=4;
for (int i = 0; i <= num - 2; i++) {
vec = randVector();
for (int j = 0; j <= list.size() - 1 ; j++) {
if (list.get(j).distance(vec) < 4f) {
lessThan = true;
}
}
if (lessThan == true) {
vec = randVector();
for (int j = 0; j <= list.size() - 1; j++) {
if (list.get(j).distance(vec) < 4f) {
lessThan = true;
} else {
lessThan = false;
}
}
}
if (lessThan == false) {
list.add(vec);
System.out.println(vec);
}
}
return list;
}
你似乎有 "unrolled your loop"。试试这个(未经测试):-
public ArrayList getVector3f() {
// create first random vector & add it to the list array
Vector3f vec = randVector();
list.add(vec);
for (int i = 0; i <= num-2; i++) {
boolean lessThan = true;
while (lessThan) {
lessThan = false;
vec = randVector();
for (int j = 0; j < list.size() ; j++) {
if (list.get(j).distance(vec) < 4f) {
lessThan = true;
break;
}
}
}
list.add(vec);
System.out.println(vec);
}
return list;
}