-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMountain.java
133 lines (114 loc) · 3.66 KB
/
Mountain.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Random;
/**
* Created by Rezenter on 11/16/15.
*/
public class Mountain {
public Tree cut;
public float pos;
public float h;
public final double FINESS = 0.001;
public Mountain(float x) {
Random random = new Random();
float l = (float) (x - 0.5);
float r = (float) (x + 0.5);
pos = (float) (l + 0.15 + Math.abs(random.nextFloat() - 0.7));
cut = new Tree(new Tuple(
new Vector2f(l, 0), new Vector2f(new Vector2f(r, 0))));
generate(cut.getRoot());
}
public void generate(Node t) {
if (t != null) {
boolean flag = true;
Random random = new Random();
if (t.equals(cut.getRoot())) {
flag = false;
h = random.nextFloat() * 2;
Vector2f tmp = new Vector2f(pos, h);
cut.getRoot().addL(new Tuple(cut.getRoot().val.a, tmp));
cut.getRoot().addR(new Tuple(tmp, cut.getRoot().val.b));
}
float length = ((t.val.b.get()[0] - t.val.a.get()[0]));
if (length > FINESS && length != Float.POSITIVE_INFINITY) {
if (flag) {
float x = (length / 2) + t.val.a.get()[0];
float y = (float) Math.abs(((random.nextFloat() - 0.5) * 2 * length)
+ (t.val.b.get()[1] + t.val.a.get()[1]) / 2);
Vector2f point = new Vector2f(x, y);
t.addL(new Tuple(t.val.a, point));
t.addR(new Tuple(point, t.val.b));
}
generate(t.l);
generate(t.r);
}
}
}
public ArrayList<Triangle> triangulate(Vector3f col, float far) {
ArrayList<Triangle> res = new ArrayList<>();
for (Node tmp : lists) {
Tuple cur = tmp.val;
Vector3f c = col;
res.add(new Triangle(
new Vector3f(cur.a.get()[0], cur.a.get()[1], far),
new Vector3f(cur.b.get()[0], cur.b.get()[1], far),
new Vector3f(cur.a.get()[0], 0, far),
c));
res.add(new Triangle(
new Vector3f(cur.b.get()[0], 0, far),
new Vector3f(cur.b.get()[0], cur.b.get()[1], far),
new Vector3f(cur.a.get()[0], 0, far),
c));
}
return res;
}
public class Tuple {
public Vector2f a;
public Vector2f b;
public String toString() {
return a + "_" + b;
}
public Tuple(Vector2f x, Vector2f y) {
a = new Vector2f(x);
b = new Vector2f(y);
}
}
private class Node {
public Tuple val;
public Node l;
public Node r;
public Node(Tuple v) {
val = v;
}
public String toString() {
return val + "";
}
public void addL(Tuple v) {
lists.remove(this);
l = new Node(v);
lists.add(l);
}
public void addR(Tuple v) {
//lists.remove(this);
r = new Node(v);
lists.add(r);
}
public void setVal(Tuple v) {
this.val = v;
}
}
public HashSet<Node> lists = new HashSet<>();
private class Tree {
private Node root;
public Tree(Tuple v) {
root = new Node(v);
lists.add(root);
}
public Node getRoot() {
return root;
}
public void setRoot(Tuple v) {
root.setVal(v);
}
}
}