public override void Apply(Heightmap targetHeightmap, Bitmap targetBitmap, int x, int y, double size, double speed, double deltaTime) { double radius = size; //Make a bounding box around the circle int startX = (int)((double)x - size); int endX = (int)((double)x + size); int startY = (int)((double)y - size); int endY = (int)((double)y + size); //Make sure the corners are in bounds startX = Utils.CapBounds(startX, 0, targetHeightmap.width); endX = Utils.CapBounds(endX, 0, targetHeightmap.width); startY = Utils.CapBounds(startY, 0, targetHeightmap.height); endY = Utils.CapBounds(endY, 0, targetHeightmap.height); //Loop through the bounding box, skipping any pixels that fall outside the circle int centerX = x; int centerY = y; //Save x and y, since we're using those variable names in the loop for (x = startX; x < endX; x++) { for (y = startY; y < endY; y++) { //Skip this pixel if it's not within the radius double squaredDist = (x - centerX) * (x - centerX) + (y - centerY) * (y - centerY); if (squaredDist > radius * radius) { continue; } //Make it so the speed decreases as it gets further from the center double distPercent = squaredDist / (radius * radius); double scaledSpeed = Utils.Lerp(speed, 0, distPercent); //Change the height at this position double val = targetHeightmap.GetValue(x, y); val += scaledSpeed * deltaTime; //Ensure the value stays within the limits if (Math.Abs(val) > Heightmap.MAX_HEIGHT) { val = Math.Sign(val) * Heightmap.MAX_HEIGHT; } //Apply the height change targetHeightmap.SetValue(x, y, val); targetBitmap.SetPixel(x, y, Heightmap.ValueToColor(val)); } } //End of double for loop } //End of function
private void generateButton_Click(object sender, EventArgs e) { //Get the data from the textboxes int width; int height; int seed; int octave; double persistence; double frequency; double lacunarity; //Catch any errors with the textboxes try { width = int.Parse(widthBox.Text); height = int.Parse(heightBox.Text); seed = int.Parse(seedBox.Text); octave = int.Parse(OctBox.Text); Double.TryParse(PersBox.Text, out persistence); Double.TryParse(LacBox.Text, out lacunarity); Double.TryParse(FreqBox.Text, out frequency); } catch (FormatException err) { MessageBox.Show("ERROR: width, height, seed, and octave count must all be integers.\r\nPersistence, lacunarity, and frequency are doubles"); return; } catch (OverflowException err) { MessageBox.Show("ERROR: One of your values is too long."); return; } //Generate the preview map previewMap = new Heightmap(width, height); PerlinNoise noiseGen = new PerlinNoise(seed, octave, frequency, lacunarity, persistence); for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { double value = noiseGen.GetValue((double)x, (double)y) * Heightmap.MAX_HEIGHT; previewMap.SetValue(x, y, value); } } //Update the preview picture previewBox.Image = previewMap.ToBitmap(); //Enable the Okay button OKButton.Enabled = true; }
public override void Apply(Heightmap targetHeightmap, Bitmap targetBitmap, int x, int y, double size, double speed, double deltaTime) { //Make a square around the coordinates int startX = (int)((double)x - size / 2); int endX = (int)((double)x + size / 2); int startY = (int)((double)y - size / 2); int endY = (int)((double)y + size / 2); //Make sure the corners of the square are in bounds startX = Utils.CapBounds(startX, 0, targetHeightmap.width); endX = Utils.CapBounds(endX, 0, targetHeightmap.width); startY = Utils.CapBounds(startY, 0, targetHeightmap.height); endY = Utils.CapBounds(endY, 0, targetHeightmap.height); //Iterate through the square, applying the logic to it. for (x = startX; x < endX; x++) { for (y = startY; y < endY; y++) { //Change the height at this position double val = targetHeightmap.GetValue(x, y); val += speed * deltaTime; //Ensure the value stays within the limits if (Math.Abs(val) > Heightmap.MAX_HEIGHT) { val = Math.Sign(val) * Heightmap.MAX_HEIGHT; } //Apply the height change targetHeightmap.SetValue(x, y, val); targetBitmap.SetPixel(x, y, Heightmap.ValueToColor(val)); } } }