Recursivley生成三角形以形成lancdscape

时间:2020-04-29 08:41:05

标签: java recursion stl fractals

我必须通过计算三角形边缘的中点并为该点分配“随机”高度(z)值,以递归方式创建从单个2维三角形开始的风景(通过在stl文件中打印三角形) 。 “随机”是因为,对于没有孔的景观,如果我在递归过程中计算相同点的中点,则必须分配相同的z值。 我尝试通过我的随机生成器的setSeed(“从中计算出中点的两点的参数”)来实现此目的。这似乎可行,创建的景观没有孔且递归大小为2或3时看起来也不是太差。

但是,我必须递归大小为8,当我创建它时,它只是一个非常刺眼的表面。

下面您可以找到我的代码(Point类,Triangle类,Main类“ Aufgabe 3”)。 如果有人知道我能做什么,那将很棒,我想问题可能出在我的种子或我创建的随机数(粗糙度因子)的边界/计算上。

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.Random;

class Point{
    double x, y, z;


    public Point(double dx, double dy, double dw) {
        x = dx; 
        y = dy; 
        z = dw;

    }

    public long seed(Point p1) {    
        return (long) (this.x*p1.x + this.y*p1.y + this.z*p1.z);        
    }

    Point middlePoint(Point p) {
        Point m = new Point(0.0, 0.0, 0.0);
        m.x = (x + p.x) / 2.0;
        m.y = (y + p.y) / 2.0;
        m.z = (z + p.z) /2.0;
        return m;
    }
}

class Triangle{
    Point p1, p2, p3;
    Random random;


    public Triangle(Point p1, Point p2, Point p3) {
        this.p1=p1;
        this.p2=p2;
        this.p3=p3;
        random = new Random();
    }


    public void elevate(int n) {
        int roughness = 9;
        elevateUtil( n, roughness);
    }

    public void elevateUtil(int n, int r) {

        Point p4 = p1.middlePoint(p2);
        random.setSeed(p1.seed(p2));
        p4.z = random.nextInt(r);

        Point p5 = p2.middlePoint(p3);
        random.setSeed(p2.seed(p3));
        p5.z = random.nextInt(r);

        Point p6 = p3.middlePoint(p1);
        random.setSeed(p3.seed(p1));
        p6.z = random.nextInt(r);

        Triangle first = new Triangle (p1, p4, p6);
        Triangle second = new Triangle (p4, p2, p5);
        Triangle third = new Triangle (p6, p5, p3);
        Triangle fourth = new Triangle (p5, p6, p4);

        if (n == 1) {
            first.print();
            second.print();
            third.print();
            fourth.print();

        } else {
            first.elevateUtil(n-1, r);
            second.elevateUtil(n-1, r);
            third.elevateUtil(n-1, r);
            fourth.elevateUtil(n-1, r);
        }

    }

    public void print() {
        System.out.println("    facet normal 0.0 0.0 0.0");
        System.out.println("        outer loop");
        System.out.println("            vertex " + p1.x + " " + p1.y + " " + p1.z);
        System.out.println("            vertex " + p2.x + " " + p2.y + " " + p2.z);
        System.out.println("            vertex " + p3.x + " " + p3.y + " " + p3.z);
        System.out.println("        endloop");
        System.out.println("    endfacet");
    }

}


public class Aufgabe3 {

    public static void main(String[] args) throws FileNotFoundException {

        System.setOut(new PrintStream(new FileOutputStream("Aufgabe3.stl"))); 

        Point p4 = new Point(10, 5, 0);
        Point p5 = new Point(32, 5, 0);
        Point p6 = new Point (21, 26, 0);
        Triangle duplicate = new Triangle (p6, p4, p5);

        System.out.println("solid Aufgabe2");
        //duplicate.print();
        duplicate.elevate(2);
        //original.elevate(2);
        System.out.println("endsolid");


        Point p1 = new Point( 15 * Math.cos(Math.toRadians(90)),  15 * Math.sin(Math.toRadians(90)), 0);
        Point p2 = new Point( 15* Math.cos(Math.toRadians(210)),  15* Math.sin(Math.toRadians(210)), 0);
        Point p3 = new Point( 15* Math.cos(Math.toRadians(330)),  15* Math.sin(Math.toRadians(330)), 0);

        Triangle original = new Triangle(p1,p2,p3);

    }

}


0 个答案:

没有答案