# Average height of any point in a shape

Super quick intro. I am making a terrain generator (With a MC exporter mostly to debug). For the islands I am creating a map with points, and create a Voronoi diagram from it. Each intersection (Where lines of the shapes meet) has a different height, depending on the distance to the coast. The coast itself is elevation=0.

However, whatever I try, I keep getting weird heightmaps. (I can't link more than two images, so I will explain). You can clearly see the difference between each site, yet they are slightly sloped.

In-game this looks like this:

The red blocks that stick out is the height of the intersection. Sites are created by references to the intersections, since they have IDs.

The code I am using at the moment:

Point p = new Point(x, 1, z);

double[] distances = new double[points.length];
double[] percentages = new double[points.length];
double[] elevations = new double[points.length];

int max = 0;
for(int i=0; i<points.length; i++)
if(parent.getIntersections().fromIndex(points[i]).getElevation() > max)
max = (int)parent.getIntersections().fromIndex(points[i]).getElevation();

double totaldist = 0;
for(int i=0; i<points.length; i++) {
distances[i] = parent.getIntersections().fromIndex(points[i]).getLocation().surfaceDist(p);
totaldist+=distances[i];
}

for(int i=0; i<points.length; i++){
percentages[i] = (distances[i] / totaldist);
}

for(int i=0; i<points.length; i++)
elevations[i] = (percentages[i] * (parent.getIntersections().fromIndex(points[i]).getElevation()));

double heightTemp = 0;
for(int i=0; i<elevations.length; i++)
heightTemp += elevations[i];
heightTemp = max-heightTemp+max;

int height = (int) heightTemp - 3 + parent.getConfig().getSeaLevel() ;
height = (height>255 ? 255 : height);


The code's quite weird and unorganized since I pretty much tried anything I could come up with. "points" is an array containing the reference IDs to the correct intersections "parent.getIntersections().fromIndex(int index)" is the way to retrieve an intersection from the index/reference id. "p" is the point to get the altitude/elevation from. "Intersection.getElevation()" returns the elevation (Anything above sea level).

Sorry if this is real vague, I have no clue on how to explain this properly.

• Looking at this makes me really wish I understood voronoi – Shaun Wild May 27 '16 at 10:19
• @ShaunWild It's not that complicated to understand, generating a diagram can also be done using some APIs! I started this project a few weeks ago, and did tons and tons of background reading. Go for it! – Daniël V May 27 '16 at 10:55