-
Notifications
You must be signed in to change notification settings - Fork 0
/
VoronoiGraph.cpp
68 lines (54 loc) · 1.44 KB
/
VoronoiGraph.cpp
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
#include "StdAfx.h"
#include "VoronoiGraph.h"
Vector3 * FPlist;
VoronoiGraph::VoronoiGraph(int fP)
{
featurePoints = fP;
init(); //Initialize memory for the height map
}
void VoronoiGraph::create()
{
FPlist = (Vector3 *) malloc ((featurePoints+1) * sizeof(Vector3));
for(int i = 0; i < featurePoints; i++)
{
int x = getRandomAsI(MAX_WORLD_SIZE-1);
int z = getRandomAsI(MAX_WORLD_SIZE-1);
FPlist[i].x = x;
FPlist[i].y = z;
if(choose(.3))
FPlist[i].z = 0; //Cell will be drawn
else
FPlist[i].z = -1; //Cell will not be drawn
}
for(int z = 0; z < MAX_WORLD_SIZE; z++)
for(int x = 0; x < MAX_WORLD_SIZE; x++)
{
//Starting values
double minDist = pow(FPlist[0].x-x, 2.0) + pow(FPlist[0].y-z, 2.0);
double minDist2 = pow(FPlist[1].x-x, 2.0) + pow(FPlist[1].y-z, 2.0);
int p = 0, p2 = 1; //Keep track of the indicies that correspond to the minimum distance values
//Find the two closest feature points to the current (x,z) location
for(int i = 2; i < featurePoints; i++)
{
double newDist = pow(FPlist[i].x-x, 2.0) + pow(FPlist[i].y-z, 2.0);
if(newDist < minDist)
{
p2 = p;
minDist2 = minDist;
p = i;
minDist = newDist;
}
else if(newDist < minDist2)
{
p2 = i;
minDist2 = newDist;
}
}
//Only draw the cells that are designated to be drawn
if(FPlist[p].z == 0)
*getMap(x, z) = (minDist2-minDist)/100;
}
}
VoronoiGraph::~VoronoiGraph(void)
{
}