找到差异程序和非程序生成算法的好地方

时间:2012-05-17 11:53:32

标签: algorithm random code-snippets pseudocode

有没有人知道找到生成世界,宇宙,地图等不同算法的好地方?

我搜索了几个地方,但大多数时候这些页面只会告诉你这些算法背后的理论。我真正想要的是代码片段或伪代码示例,但大多数时候你(或至少我)都找不到这些。

代码片段的语言并不重要,因为它们中的大多数都很容易移植,更喜欢伪代码或java tho。

我目前正在寻找的是:

生成星系。 (特别是要获得螺旋臂式星系,而不仅仅是随机放置或聚类)。

生成恒星系统(基于种子的随机平面和轨道)。

横向和纹理生成(我已经尝试了基于种子的pe​​rlin噪声并得到了相当不错的结果,只是想知道是否还有其他好的代码示例)。

基于景观和其他因素生成资源和其他元数据。

编辑: 对于螺旋臂星系我目前有这个:

import java.util.ArrayList;
import java.util.Random;

public class SpiralUniverse {

    private ArrayList<Position2f> Galaxies = new ArrayList<Position2f>();
    private int NumberOfStars;
    //# logarithmic spiral constants 
    //# http://en.wikipedia.org/wiki/Logarithmic_spiral
    private float A = 1.0f;
    private float B = 0.20f;
    private float Windings; // 12.4f
    private float MaxAngle;
    //# How far stars may be away from spiral arm centers.
    private float Drift = 0.3f;
    private Random Rand;

    public SpiralUniverse(int numberofstarts, int seed, float windings) {
        NumberOfStars = numberofstarts;
        Windings = windings;
        Rand = new Random(seed);
        MaxAngle = (float) (2.0 * Math.PI * Windings);
        generateGalaxies();
    }

    private void generateGalaxies() {
        for (int i = 0; i < NumberOfStars; i++) {
            float angle = MaxAngle * Rand.nextFloat();
            float x = (float) (A * Math.exp(B * angle) * Math.cos(angle));
            x = x + (Drift * x * Rand.nextFloat()) - (Drift * x * Rand.nextFloat());
            float y = (float) (A * Math.exp(B * angle) * Math.sin(angle));
            y = y + (Drift * y * Rand.nextFloat()) - (Drift * y * Rand.nextFloat());

            // 2 Spiral Arms
            if (!Rand.nextBoolean()) {
                x = -x;
                y = -y;
            }
            Galaxies.add(new Position2f(x, y));
        }
    }

    public ArrayList<Position2f> getGalaxies() {
        return Galaxies;
    }

    /**
     * Tests the code.
     */
    public static void main(String args[]) {
        SpiralUniverse su = new SpiralUniverse(2000, 1234, 12.4f);
        for (Position2f p : su.getGalaxies()) {
            System.out.println("x: " + p.getX() + "; y: " + p.getY());
        }
    }

    public class Position2f {

        private float x;
        private float y;

        public Position2f(float x, float y) {
            this.x = x;
            this.y = y;
        }

        public float getX() {
            return x;
        }

        public void setX(float x) {
            this.x = x;
        }

        public float getY() {
            return y;
        }

        public void setY(float y) {
            this.y = y;
        }
    }
}

它得到相当不错的结果,但是必须根据我正在创建的星系的大小来缩放坐标,并且星星倾向于将中心聚集在一起。 对此更好的解决方案吗?

0 个答案:

没有答案