patterncsharpMinor
Basic height map generator
Viewed 0 times
mapgeneratorbasicheight
Problem
I've made this relatively basic height map generator on my own just to see if I could pull it off because I'm still a newbie when it comes to programming. So my question is: "Is there a fairly obvious way I can optimize it?". I put in effort to make the code as clean as I can write it so any input from a quick review (if you have nothing better to do of course) would be greatly appreciated. Here is the ORIGINAL code:
```
using System;
using System.Diagnostics;
namespace height_map_generator
{
class Program
{
private static readonly int bufferWidth = 237;
private static readonly int bufferHeight = 90;
private static readonly double nodeProbability = 0.007; //Initial node spawn probability in each point of buffer array. (The higher this number is the more peaks on the map.)
private static readonly double intervalFraction = 0.03; //Number must be between 0 and 1. (The lower this number - the higher the variability and sharpness of terrain.)
private static readonly double maxDistanceFromCentre = Math.Sqrt(Math.Pow(bufferWidth / 2, 2) + Math.Pow(bufferHeight / 2, 2));
private static readonly double distanceInterval = maxDistanceFromCentre * intervalFraction;
private static readonly Random rand = new Random();
private static int[,] mapBuffer = new int[bufferWidth, bufferHeight];
private static int[,] nodePositionValue = new int[bufferWidth, bufferHeight];
private static int[] nodeXposition = new int[bufferWidth * bufferHeight];
private static int[] nodeYposition = new int[bufferHeight * bufferWidth];
private static int nodeCount = 0; //Currently not in use.
static void Main(string[] args)
{
Console.SetBufferSize(bufferWidth, bufferHeight);
Console.ForegroundColor = ConsoleColor.Green;
string elapsedTime = null;
Stopwatch sw = new Stopwatch();
sw.Start();
constructNodes(no
```
using System;
using System.Diagnostics;
namespace height_map_generator
{
class Program
{
private static readonly int bufferWidth = 237;
private static readonly int bufferHeight = 90;
private static readonly double nodeProbability = 0.007; //Initial node spawn probability in each point of buffer array. (The higher this number is the more peaks on the map.)
private static readonly double intervalFraction = 0.03; //Number must be between 0 and 1. (The lower this number - the higher the variability and sharpness of terrain.)
private static readonly double maxDistanceFromCentre = Math.Sqrt(Math.Pow(bufferWidth / 2, 2) + Math.Pow(bufferHeight / 2, 2));
private static readonly double distanceInterval = maxDistanceFromCentre * intervalFraction;
private static readonly Random rand = new Random();
private static int[,] mapBuffer = new int[bufferWidth, bufferHeight];
private static int[,] nodePositionValue = new int[bufferWidth, bufferHeight];
private static int[] nodeXposition = new int[bufferWidth * bufferHeight];
private static int[] nodeYposition = new int[bufferHeight * bufferWidth];
private static int nodeCount = 0; //Currently not in use.
static void Main(string[] args)
{
Console.SetBufferSize(bufferWidth, bufferHeight);
Console.ForegroundColor = ConsoleColor.Green;
string elapsedTime = null;
Stopwatch sw = new Stopwatch();
sw.Start();
constructNodes(no
Solution
A couple quick things:
If you are using while loops for iterations, just use for loops.
In probability result, you can drop the if and the new variable. Just do it like this
You can apply the same logic to several of your other functions. No need to save data to temp var if you're going to return it immediately
As far as actual performance goes, instead of using a multidimensional array ([,]), use a jagged array([][]). Due to compiler optimizations jagged arrays often perform better.
If you are using while loops for iterations, just use for loops.
//I have to look in three places to understand how the loop works
int x = 0;
while(x < 10){x++;}
//Everything I need is right here
for(int x = 0; x < 10; x++){}In probability result, you can drop the if and the new variable. Just do it like this
private static bool probabilityResult(double percent)
{
if (percent 1)
throw new ArgumentOutOfRangeException("Probability must be between 0 and 1.");
return rand.NextDouble() <= percent;
}You can apply the same logic to several of your other functions. No need to save data to temp var if you're going to return it immediately
As far as actual performance goes, instead of using a multidimensional array ([,]), use a jagged array([][]). Due to compiler optimizations jagged arrays often perform better.
Code Snippets
//I have to look in three places to understand how the loop works
int x = 0;
while(x < 10){x++;}
//Everything I need is right here
for(int x = 0; x < 10; x++){}private static bool probabilityResult(double percent)
{
if (percent <= 0 || percent > 1)
throw new ArgumentOutOfRangeException("Probability must be between 0 and 1.");
return rand.NextDouble() <= percent;
}Context
StackExchange Code Review Q#161236, answer score: 5
Revisions (0)
No revisions yet.