@TMH97/

# 2d random test

## My attempt at created a two-dimensional noise grid.

Files
• Main.java
• Main.class
• Main\$coord.class
• nohup.out
Main.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
```
```import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Font;
import java.util.Random;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class Main {
private static final int SEED = -1; // -1 for random seed

private Random r;
private double[][] noise;

public Main(int seed) {
if(seed == -1) {
r = new Random();
seed = r.nextInt(99999);
System.out.println("Generating noise with seed: "+seed);
}
r = new Random(seed);

generateNoise();
}

public double getNoise(int x, int y) {

if(x > noise.length) {
x %= noise.length;
}
if(y > noise.length) {
y %= noise.length;
}

return noise[(int)x][(int)y];
}

private void generateNoise() {
noise = new double[100][100];

coord[] points = new coord[noise.length / 10 + 4];
for(int i = 0; i < points.length - 4; i++) {
points[i] = new coord(r.nextInt(100), r.nextInt(100));
noise[points[i].x][points[i].y] = r.nextDouble();
System.out.println(String.format("Random point '%s,%s' has a value of '%.4f'", points[i].x, points[i].y, noise[points[i].x][points[i].y]));
}

points[points.length-4] = new coord(0,0);
noise[0][0] = r.nextDouble();
points[points.length-3] = new coord(0,99);
noise[0][99] = r.nextDouble();
points[points.length-2] = new coord(99,0);
noise[99][0] = r.nextDouble();
points[points.length-1] = new coord(99,99);
noise[99][99] = r.nextDouble();

for(int x = 0;  x < noise.length; x++) {
for(int y = 0; y < noise[x].length; y++) {
coord here = new coord(x,y);
if(!contains(points,here)) {
double[] distances = new double[points.length];

for(int i = 0; i < points.length; i++) {
distances[i] = findDistance(points[i],here);
}

double[] closest = new double[3];
int[]closestPos = new int[3];
for(int j = 0; j<closest.length; j++) {
int close = -1;
closest[j] = 99999;
for(int k = 0; k < distances.length; k++) {
if(closest[j] > distances[k]) {
closest[j] = distances[k];
close = k;
closestPos[j] = k;
}
}
distances[close] = 99999;
}

double d1 = closest[0];
double d2 = closest[1];
double d3 = closest[2];

double r1 = 1/d1;
double r2 = 1/d2;
double r3 = 1/d3;

double w1 = noise[points[closestPos[0]].x][points[closestPos[0]].y];
double w2 = noise[points[closestPos[1]].x][points[closestPos[1]].y];
double w3 = noise[points[closestPos[2]].x][points[closestPos[2]].y];

double w = (w1*r1 + w2*r2 + w3*r3)/r1+r2+r3;

noise[here.x][here.y] = w;
}
}
}
}

public static void main(String[] args) {
Main test = new Main(SEED);

JFrame frame = new JFrame("RTest");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800, 600);
frame.setLocationRelativeTo(null);

JPanel panel = new JPanel();
panel.setLayout(new GridLayout(100,100));

for(int y = 0; y < 100; y++) {
for(int x = 0; x < 100; x++) {

int mult = 80;

Font font = new Font("Serif",Font.PLAIN,10);

//Should return a double between 0-1, does not.
double n = test.getNoise(x, y);

Color disp = new Color((int)(255-n*mult),(int)(n*mult),20);
JLabel lbl = new JLabel();
lbl.setBackground(disp);
lbl.setFont(font);
lbl.setOpaque(true);
lbl.setText(String.format("<html>(%s,%s)<br>%.3f</html>",x,y,n));
lbl.setVisible(true);
}
}

JScrollPane pane = new JScrollPane(panel);

frame.setVisible(true);
}

public static double findDistance(coord a, coord b) {
double distance = Math.sqrt(Math.pow((a.x - b.x), 2) + Math.pow((a.y - b.y),2));
return distance;
}

private static boolean contains (Object[] array, Object item) {
for(Object i : array) {
if(i.equals(item)) {
return true;
}
}
return false;
}

private class coord{
public int x;
public int y;

public coord(int x, int y) {
this.x = x;
this.y = y;
}

@Override
public boolean equals(Object other) {
if(other.getClass().equals(this.getClass())) {
coord o = (coord)other;

if(x == o.x && y == o.y) {
return true;
}
}
return false;
}
}
}```