gpt4 book ai didi

java - 从中心点分布形状

转载 作者:行者123 更新时间:2023-12-01 11:46:32 24 4
gpt4 key购买 nike

我正在尝试改进从中心点在网格表面上以相同足迹布置数千个形状的位置的方法(它具有更有机的外观)。网格表面意味着邻居之间的间距相等,但我不想通过选择随机的 x 和 y 坐标(或者在本例中为 x 和 z,因为我在 3 维中工作)来创建类似拼凑的图案)。我现在所拥有的可以工作,但是当我开始从 2,000 个对象向上移动时,速度非常慢。有没有更快的方法?正如我所说,我避免了拼凑效果,以及均匀的圆形分布。现在的分发非常城市化且完美,但太慢了。

我已经对该函数进行了评论,因此希望它能够彻底解释所有内容:

ArrayList buildingCoords; // stores the co-ordinates of occupied spaces
int w = 50 // Footprint dimensions. Width and depth.

PVector generateNewBuildingPosition(PVector coord) {
float sW = 30; // gap between shapes
// Starting at a coordinate of 0,0 if this is our 1st go
// (PVector coord initially feeds 0,0)
// or the last coordinate that was already taken
// (this loops with the last coordinate if it fails)
// we check one of the four spaces next to us with
// the roll of a dice (in a way...)
float randomX = random(0,15);
float randomZ = random(0,15);
if (randomX >= 10) {
randomX = w + sW;
} else if (randomX >= 5) {
randomX = (w + sW) * -1;
} else {
randomX = 0;
}
if (randomZ >= 10) {
randomZ = w + sW;
} else if (randomX >= 5) {
randomZ = (w + sW) * -1;
} else {
randomZ = 0;
}

// We've picked our direction.
// We have a PVector that acts as a marker for where we're
// placing. Adding or subtracting the movement of each
// attempt, one at a time, means the shapes spreads out
// more organically from the centre rather than trying
// to distribute each shape as a random patch.
PVector newDirection = new PVector(randomX, 0, randomZ);
coord.add(newDirection);
// Our marker moves to the new spot, we check if it exists.
// If it doesn't, we confirm it as this shape's anchor spot.
// If it does, we loop this function again, feeding it where our
// marker is.
if(buildingCoords.contains(coord)) {
generateNewBuildingPosition(coord);
} else {
// add this PVector to the arrayList
buildingCoords.add(coord);
}
// Return the coordinates that just succeeded.
return coord;
}

最佳答案

这段代码基本上可以立即处理 200,000 个数据。我不得不猜测一些细节,但它应该接近您正在寻找的内容。

public class Test {

static Map<Integer, Vector3> buildingCoords;
public static void main(String[] args) {
buildingCoords = new HashMap();

Vector3 start = new Vector3(0,0,0);

for (int i = 0; i < 200000; i++)
start = generateNewBuildingPosition(start);

System.out.print("Done");

}

static Vector3 generateNewBuildingPosition(Vector3 coord) {
int w = 50; // Footprint dimensions. Width and depth.
float sW = 30; // gap between shapes
// Starting at a coordinate of 0,0 if this is our 1st go
// (PVector coord initially feeds 0,0)
// or the last coordinate that was already taken
// (this loops with the last coordinate if it fails)
// we check one of the four spaces next to us with
// the roll of a dice (in a way...)
float randomX = (float)random() * 15;
float randomZ = (float)random() * 15;
if (randomX >= 10) randomX = w + sW;
else if (randomX >= 5) randomX = (w + sW) * -1;
else randomX = 0;

if (randomZ >= 10) randomZ = w + sW;
else if (randomX >= 5) randomZ = (w + sW) * -1;
else randomZ = 0;


// We've picked our direction.
// We have a PVector that acts as a marker for where we're
// placing. Adding or subtracting the movement of each
// attempt, one at a time, means the shapes spreads out
// more organically from the centre rather than trying
// to distribute each shape as a random patch.
Vector3 newDirection = new Vector3(randomX, 0, randomZ);
coord.add(newDirection);
// Our marker moves to the new spot, we check if it exists.
// If it doesn't, we confirm it as this shape's anchor spot.
// If it does, we loop this function again, feeding it where our
// marker is.
if(buildingCoords.containsKey(coord.hashCode())) {
generateNewBuildingPosition(coord);
} else {
// add this PVector to the arrayList
buildingCoords.put(coord.hashCode(), coord);
}
// Return the coordinates that just succeeded.
return coord;
}
}

class Vector3 {

float x, y, z; // package-private variables; nice encapsulation if you place this in a maths package of something

Vector3(float x, float y, float z) {
this.x = x;
this.y = y;
this.z = z;
}

public Vector3 add(Vector3 vector) {
x += vector.x;
y += vector.y;
z += vector.z;
return this; // method chaining would be very useful
}

@Override
public int hashCode(){
return Float.hashCode(x + y + z);
}

}

编辑:所示的 hashCode 不太健全,可能会导致问题。您应该阅读:Hashing 2D, 3D and nD vectors

关于java - 从中心点分布形状,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29096440/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com