-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiamondsquare.cc
More file actions
115 lines (105 loc) · 3.48 KB
/
diamondsquare.cc
File metadata and controls
115 lines (105 loc) · 3.48 KB
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
// diamondsquare.cpp : diamond square algorithm
//
#include "diamondsquare.h"
#include <cstdlib>
#include <vector>
#include <cmath>
#include <algorithm>
#include <iostream>
using namespace std;
int random(int a, int b)
{
return rand() % b + a;
}
float avg(float a, float b, float c, float d)
{
return (a + b + c + d) / 4;
}
int max(int a, int b, int c, int d)
{
return max(max(max(a, b), c), d);
}
int min(int a, int b, int c, int d)
{
return min(min(min(a, b), c), d);
}
void diamondsquare(vector<vector<float> > &res, float rate, int M, int H, int a, int b, int c, int d)
{
int INC = H;
if (a + 1 < c)
{
int m = (a + c) / 2, n = (b + d) / 2;
float maxN = max(res[a][b], res[a][d], res[c][b], res[c][d]);
float minN = min(res[a][b], res[a][d], res[c][b], res[c][d]);
float av = avg(res[a][b], res[a][d], res[c][b], res[c][d]);
res[m][n] = av + (random(minN, maxN + INC) - minN) * rate;
if(a != 0 && a != M - 1)
{
maxN = max(res[a][b], res[m][n], res[a][d], (3 * a >= 2 * c ? res[3 * a / 2 - c / 2][n] : 0));
minN = min(res[a][b], res[m][n], res[a][d], (3 * a >= 2 * c ? res[3 * a / 2 - c / 2][n] : 0));
av = avg(res[a][b], res[m][n], res[a][d], (3 * a >= 2 * c ? res[3 * a / 2 - c / 2][n] : 0));
int temp = 0;
if(a < 8 || a > M - 8 || n < 8 || n > M - 8)
{
temp = random(0, max(H / 4, 2)) * pow(rate, 3);
}
else
{
temp = (random(minN, maxN + INC) - minN) * rate;
}
res[a][n] = av + temp;
}
if(b != 0 && b != M - 1)
{
maxN = max(res[a][b], res[m][n], res[c][b], (3 * b >= 2 * d ? res[m][3 * b / 2 - d / 2] : 0));
minN = min(res[a][b], res[m][n], res[c][b], (3 * b >= 2 * d ? res[m][3 * b / 2 - d / 2] : 0));
av = avg(res[a][b], res[m][n], res[c][b], (3 * b >= 2 * d ? res[m][3 * b / 2 - d / 2] : 0));
int temp = 0;
if(b < 8 || b > M - 8 || m < 8 || m > M - 8)
{
temp = random(0, max(H / 4, 2)) * pow(rate, 3);
}
else
{
temp = (random(minN, maxN + INC) - minN) * rate;
}
res[m][b] = av + temp;
}
if(c != 0 && c != M - 1)
{
maxN = max(res[c][b], res[m][n], res[c][d], (a + 2 * (int)res.size() > 3 * c ? res[3 * c / 2 - a / 2][n] : 0));
minN = min(res[c][b], res[m][n], res[c][d], (a + 2 * (int)res.size() > 3 * c ? res[3 * c / 2 - a / 2][n] : 0));
av = avg(res[c][b], res[m][n], res[c][d], (a + 2 * (int)res.size() > 3 * c ? res[3 * c / 2 - a / 2][n] : 0));
int temp = 0;
if(c < 8 || c > M - 8 || n < 8 || n > M - 8)
{
temp = random(0, max(H / 4, 2)) * pow(rate, 3);
}
else
{
temp = (random(minN, maxN + INC) - minN) * rate;
}
res[c][n] = av + temp;
}
if(d != 0 && d != M - 1)
{
maxN = max(res[c][b], res[m][n], res[c][d], (b + 2 * (int)res.size() > 3 * d ? res[3 * d / 2 - b / 2][n] : 0));
minN = min(res[c][b], res[m][n], res[c][d], (b + 2 * (int)res.size() > 3 * d ? res[3 * d / 2 - b / 2][n] : 0));
av = avg(res[c][b], res[m][n], res[c][d], (b + 2 * (int)res.size() > 3 * d ? res[3 * d / 2 - b / 2][n] : 0));
int temp = 0;
if(d < 8 || d > M - 8 || m < 8 || m > M - 8)
{
temp = random(0, max(H / 4, 1)) * pow(rate, 3);
}
else
{
temp = (random(minN, maxN + INC) - minN) * rate;
}
res[m][d] = av + temp;
}
diamondsquare(res, rate * rate, M, H, a, b, m, n);
diamondsquare(res, rate * rate, M, H, m, b, c, n);
diamondsquare(res, rate * rate, M, H, a, n, m, d);
diamondsquare(res, rate * rate, M, H, m, n, c, d);
}
}