我希望有8对整数,以便它们都具有不同的坐标,并且彼此之间都在5个单位以内(使用欧氏距离)。
为了澄清,我想拥有
(x1, y1)
(x2, y2)
(x3, y3)
(x4, y4)
(x5, y5)
(x6, y6)
(x7, y7)
(x8, y8)
其中xi
和yi
是随机生成的整数(通过nextInt
),因此没有索引i
,j
这样xi = xj
和yi = yj
,对于所有索引i
,j
sqrt( (xi - xj)^2 + (yi - yj)^2 ) <= 5
。
我尝试通过nextInt生成第一对,然后使用7个复杂的while循环来选择其他7对,但计算时间太长:
int r_x = generateur.nextInt(2*n + 1) - n;
int r_y = generateur.nextInt(2*n + 1) - n;
int p_x = generateur.nextInt(2*n + 1) - n;
int p_y = generateur.nextInt(2*n + 1) - n;
int b1_x = generateur.nextInt(2*n + 1) - n;
int b1_y = generateur.nextInt(2*n + 1) - n;
int b2_x = generateur.nextInt(2*n + 1) - n;
int b2_y = generateur.nextInt(2*n + 1) - n;
int g1_x = generateur.nextInt(2*n + 1) - n;
int g1_y = generateur.nextInt(2*n + 1) - n;
int g2_x = generateur.nextInt(2*n + 1) - n;
int g2_y = generateur.nextInt(2*n + 1) - n;
int m_x = generateur.nextInt(2*n + 1) - n;
int m_y = generateur.nextInt(2*n + 1) - n;
int w_x = generateur.nextInt(2*n + 1) - n;
int w_y = generateur.nextInt(2*n + 1) - n;
Point2D pt_r = new Point2D(r_x, r_y);
Point2D pt_p = new Point2D(p_x, p_y);
Point2D pt_b1 = new Point2D(b1_x, b2_y);
Point2D pt_b2 = new Point2D(b2_x, b2_y);
Point2D pt_g1 = new Point2D(g1_x, g1_y);
Point2D pt_g2 = new Point2D(g2_x, g2_y);
Point2D pt_m = new Point2D(m_x, m_y);
Point2D pt_w = new Point2D(w_x, w_y);
boolean r_p = false;
boolean r_p_b1 = false;
boolean r_p_b1_b2 = false;
boolean r_p_b1_b2_g1 = false;
boolean r_p_b1_b2_g1_g2 = false;
boolean r_p_b1_b2_g1_g2_m = false;
boolean r_p_b1_b2_g1_g2_m_w = false;
while(!r_p_b1_b2_g1_g2_m_w){
while(!r_p_b1_b2_g1_g2_m){
while(!r_p_b1_b2_g1_g2){
while(!r_p_b1_b2_g1){
while(!r_p_b1_b2){
while(!r_p_b1){
while(!r_p){
p_x = generateur.nextInt(n + 1) - n/2;
p_y = generateur.nextInt(n + 1) - n/2;
r_p = pt_r.test(p_x, p_y);
}
pt_p = new Point2D(p_x, p_y);
this.peon.setPos(pt_p);
b1_x = generateur.nextInt(n/2 + 1) - n/4;
b1_y = generateur.nextInt(n/2 + 1) - n/4;
r_p_b1 = (pt_r.test(b1_x, b1_y) && pt_p.test(b1_x, b1_y));
}
pt_b1 = new Point2D(b1_x, b1_y);
this.bugs1.setPos(pt_b1);
b2_x = generateur.nextInt(n/4 + 1) - n/8;
b2_y = generateur.nextInt(n/4 + 1) - n/8;
r_p_b1_b2 = (pt_r.test(b2_x, b2_y) && pt_p.test(b2_x, b2_y) && pt_b1.test(b2_x, b2_y));
}
pt_b2 = new Point2D(b2_x, b2_y);
this.bugs2.setPos(pt_b2);
g1_x = generateur.nextInt(n/8 + 1) - n/16;
g1_y = generateur.nextInt(n/8 + 1) - n/16;
r_p_b1_b2_g1 = (pt_r.test(g1_x, g1_y) && pt_p.test(g1_x, g1_y) && pt_b1.test(g1_x, g1_y) && pt_b2.test(g1_x, g1_y));
}
pt_g1 = new Point2D(g1_x, g1_y);
this.guillaumeT.setPos(pt_g1);
g2_x = generateur.nextInt(n/16 + 1) - n/32;
g2_y = generateur.nextInt(n/16 + 1) - n/32;
r_p_b1_b2_g1_g2 = (pt_r.test(g2_x, g2_y) && pt_p.test(g2_x, g2_y) && pt_b1.test(g2_x, g2_y) && pt_b2.test(g2_x, g2_y) && pt_g1.test(g2_x, g2_y));
}
pt_g2 = new Point2D(g2_x, g2_y);
this.grosBill.setPos(pt_g2);
m_x = generateur.nextInt(n/32 + 1) - n/64;
m_y = generateur.nextInt(n/32 + 1) - n/64;
r_p_b1_b2_g1_g2_m = (pt_r.test(m_x, m_y) && pt_p.test(m_x, m_y) && pt_b1.test(m_x, m_y) && pt_b2.test(m_x, m_y) && pt_g1.test(m_x, m_y)
&& pt_g2.test(m_x, m_y));
}
pt_m = new Point2D(m_x, m_y);
this.merlin.setPos(pt_m);
w_x = generateur.nextInt(n/64 + 1) - n/128;
w_y = generateur.nextInt(n/64 + 1) - n/128;
r_p_b1_b2_g1_g2_m_w = (pt_r.test(w_x, w_y) && pt_p.test(w_x, w_y) && pt_b1.test(w_x, w_y) && pt_b2.test(w_x, w_y) && pt_g1.test(w_x, w_y)
&& pt_g2.test(w_x, w_y) && pt_w.test(w_x, w_y));
}
pt_w = new Point2D(w_x, w_y);
this.wolfie.setPos(pt_w);
其中n是2的幂
答案 0 :(得分:0)
从(0,0)到(4,4)有25个坐标对。您可以立即消除其中五个:(0,0),(1,1)等,具有相同的x和y值。剩下20个可能的值。在这20个中,只有两个相差超过5个单位:(0,4)和(4,0)相隔sqrt(32)。如果选择这两个中的一个,则不能选择另一个。除此之外,您还可以选择给定范围内的任何一个。
答案 1 :(得分:0)
尝试错误算法的简单方法是:
位置矢量在以下位置确定:
private void determinePositionVectors() {
positionVector[0] = getFirstPositionVector(); // 1.
for (int i = 1; i < positionVector.length; i++) {
if (positionVector[i] == null) {
positionVector[i] = getRemainingPositionVectors(); // 2.
while(!checkConditions(i)) { // 3.
positionVector[i] = getRemainingPositionVectors();
}
}
}
}
使用
private Point2D getFirstPositionVector() {
int n1 = random.nextInt(2 * MAXIMUM_POSITION_FIRST_POSITIONVECTOR + 1) - MAXIMUM_POSITION_FIRST_POSITIONVECTOR;
int n2 = random.nextInt(2 * MAXIMUM_POSITION_FIRST_POSITIONVECTOR + 1) - MAXIMUM_POSITION_FIRST_POSITIONVECTOR;
return new Point2D(n1, n2);
}
private Point2D getRemainingPositionVectors() {
int n1 = random.nextInt(2 * MAXIMUM_DISTANCE + 1) - MAXIMUM_DISTANCE;
int n2 = random.nextInt(2 * MAXIMUM_DISTANCE + 1) - MAXIMUM_DISTANCE;
Point2D p = positionVector[0].add(n1,n2);
return p;
}
private boolean checkConditions(int i) {
for (int j = 0; j < i; j++) {
double distance = positionVector[i].distance(positionVector[j]);
boolean positionVectorsEquals = positionVector[j].equals(positionVector[i]);
if (positionVectorsEquals || distance > MAXIMUM_DISTANCE) {
return false;
}
}
return true;
}
使用的常量和字段等定义为:
package application;
import java.util.Arrays;
import java.util.Random;
import javafx.geometry.Point2D;
public class RandomVectors {
private final static int NUMBER_OF_POSITIONVECTORS = 8;
private final static int MAXIMUM_DISTANCE = 5;
private final static int MAXIMUM_POSITION_FIRST_POSITIONVECTOR = 10;
private static Random random = new Random();
private Point2D[] positionVector = new Point2D[NUMBER_OF_POSITIONVECTORS];
public static void main(String[] args) {
RandomVectors randomVectors = new RandomVectors();
randomVectors.determinePositionVectors();
Arrays.stream(randomVectors.positionVector).forEach(p->System.out.print("(" + p.getX() + "," + p.getY() + ") "));
}
private void determinePositionVectors() {...}
private Point2D getFirstPositionVector() {...}
private Point2D getRemainingPositionVectors() {...}
private boolean checkConditions(int i) {...}
}
对于某些参数(例如,MAXIMUM_DISTANCE的较小值和NUMBER_OF_POSITIONVECTORS的较大值),由于在太小的空间上有太多的位置矢量,因此该问题无法解决。这导致在确定位置向量中出现无尽的while循环。到目前为止,尚未集成任何机制来防止这种无休止的while循环。一种简单的方法是计算while循环数,并在超过阈值时停止程序。