Other Posts in Image Editing

  1. Perlin Noise
  2. Fault Formation
  3. Cellular Textures
  4. Resizing an Image in C#
  5. Box Blur and Gaussian Blur... Sort of...
  6. Thermal Erosion
  7. Using Mid Point Displacement to Create Cracks
  8. Fluvial Erosion
  9. Creating Marble Like Textures Procedurally
  10. Procedural Textures and Dilation
  11. Converting Image to Black and White in C#
  12. Getting an HTML Based Color Palette from an Image in C#
  13. Adding Noise/Jitter to an Image in C#
  14. Creating Pixelated Images in C#
  15. Edge detection in C#
  16. Using Sin to Get What You Want... In C#...
  17. Noise Reduction of an Image in C# using Median Filters
  18. Image Dilation in C#
  19. Sepia Tone in C#
  20. Kuwahara Filter in C#
  21. Matrix Convolution Filters in C#
  22. Symmetric Nearest Neighbor in C#
  23. Bump Map Creation Using C#
  24. Normal Map Creation Using C#
  25. Creating Negative Images using C#
  26. Red, Blue, and Green Filters in C#
  27. Converting an Image to ASCII Art in C#
  28. Adjusting Brightness of an Image in C#
  29. Adding Noise to an Image in C#
  30. Adjusting the Gamma of an Image Using C#
  31. Adjusting Contrast of an Image in C#
  32. Drawing a Box With Rounded Corners in C#
  33. Anding Two Images Together Using C#
  34. Motion Detection in C#
  35. Creating Thermometer Chart in C#
  36. Colorizing a Black and White Image in C#
  37. Extracting an Icon From a File
  38. Setting the Pixel Format and Image Format of an Image in .Net
  39. Using Unsafe Code for Faster Image Manipulation
  40. Sobel Edge Detection and Laplace Edge Detection in C#

Fault Formation

3/22/2008

In case you're curious, I'm not talking about seg faults. I'm referring to faults on a landscape where two tectonic plates meet. Previously I talked about Perlin Noise a bit and unlike that algorithm, this one is more of a brute force type algorithm. All you do is pick two points along the edge of the height map, draw a line, and pick one side of that line and increase or decrease the values by a given amount. You just continue to do this for a couple thousand iterations and you have some terrain.

The final image doesn't look quite as good as the Perlin Noise code. At the same time it has a tendency to take an extremely long time to create an item due to the number of iterations involved. However if we put the height map through a Gaussian Blur or something along those lines, it usually fixes the "look" issue to some extent. It does however usually create something that looks more like rolling hills when compared to Perlin Noise.  Anyway, here is the code to accomplish it:

   1: /*
   2: Copyright (c) 2010 <a href="http://www.gutgames.com">James Craig</a>
   3: 
   4: Permission is hereby granted, free of charge, to any person obtaining a copy
   5: of this software and associated documentation files (the "Software"), to deal
   6: in the Software without restriction, including without limitation the rights
   7: to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
   8: copies of the Software, and to permit persons to whom the Software is
   9: furnished to do so, subject to the following conditions:
  10: 
  11: The above copyright notice and this permission notice shall be included in
  12: all copies or substantial portions of the Software.
  13: 
  14: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15: IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16: FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17: AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18: LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19: OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20: THE SOFTWARE.*/
  21:  
  22: #region Usings
  23: using System;
  24: using System.Collections.Generic;
  25: using System.Linq;
  26: using System.Text;
  27: using System.Drawing;
  28: using System.Drawing.Imaging;
  29: #endregion
  30:  
  31: namespace Utilities.Media.Image
  32: {
  33:     /// <summary>
  34:     /// Helper class for doing fault formations
  35:     /// </summary>
  36:     public static class FaultFormation
  37:     {
  38:         /// <summary>
  39:         /// Generates a number of faults, returning an image
  40:         /// </summary>
  41:         /// <param name="Width">Width of the resulting image</param>
  42:         /// <param name="Height">Height of the resulting image</param>
  43:         /// <param name="NumberFaults">Number of faults</param>
  44:         /// <param name="Seed">Random seed</param>
  45:         /// <returns>An image from the resulting faults</returns>
  46:         public static Bitmap Generate(int Width,int Height,int NumberFaults,int Seed)
  47:         {
  48:             float[,] Heights = new float[Width, Height];
  49:             float IncreaseVal = 0.1f;
  50:             System.Random Generator = new System.Random(Seed);
  51:             for (int x = 0; x < NumberFaults; ++x)
  52:             {
  53:                 IncreaseVal = GenerateFault(Width, Height, NumberFaults, Heights, IncreaseVal, Generator);
  54:             }
  55:             Bitmap ReturnValue = new Bitmap(Width, Height);
  56:             BitmapData ImageData = Image.LockImage(ReturnValue);
  57:             int ImagePixelSize = Image.GetPixelSize(ImageData);
  58:             for (int x = 0; x < Width; ++x)
  59:             {
  60:                 for (int y = 0; y < Height; ++y)
  61:                 {
  62:                     float Value = Heights[x, y];
  63:                     Value = (Value * 0.5f) + 0.5f;
  64:                     Value *= 255;
  65:                     int RGBValue = Math.MathHelper.Clamp((int)Value, 255, 0);
  66:                     Image.SetPixel(ImageData, x, y, Color.FromArgb(RGBValue, RGBValue, RGBValue), ImagePixelSize);
  67:                 }
  68:             }
  69:             Image.UnlockImage(ReturnValue, ImageData);
  70:             return ReturnValue;
  71:         }
  72:  
  73:         private static float GenerateFault(int Width, int Height, int NumberFaults, float[,] Heights, float IncreaseVal, System.Random Generator)
  74:         {
  75:             int Wall = 0;
  76:             int Wall2 = 0;
  77:             while (Wall == Wall2)
  78:             {
  79:                 Wall = Generator.Next(4);
  80:                 Wall2 = Generator.Next(4);
  81:             }
  82:             int X1 = 0;
  83:             int Y1 = 0;
  84:             int X2 = 0;
  85:             int Y2 = 0;
  86:             while (X1 == X2 || Y1 == Y2)
  87:             {
  88:                 if (Wall == 0)
  89:                 {
  90:                     X1 = Generator.Next(Width);
  91:                     Y1 = 0;
  92:                 }
  93:                 else if (Wall == 1)
  94:                 {
  95:                     Y1 = Generator.Next(Height);
  96:                     X1 = Width;
  97:                 }
  98:                 else if (Wall == 2)
  99:                 {
 100:                     X1 = Generator.Next(Width);
 101:                     Y1 = Height;
 102:                 }
 103:                 else
 104:                 {
 105:                     X1 = 0;
 106:                     Y1 = Generator.Next(Height);
 107:                 }
 108:  
 109:                 if (Wall2 == 0)
 110:                 {
 111:                     X2 = Generator.Next(Width);
 112:                     Y2 = 0;
 113:                 }
 114:                 else if (Wall2 == 1)
 115:                 {
 116:                     Y2 = Generator.Next(Height);
 117:                     X2 = Width;
 118:                 }
 119:                 else if (Wall2 == 2)
 120:                 {
 121:                     X2 = Generator.Next(Width);
 122:                     Y2 = Height;
 123:                 }
 124:                 else
 125:                 {
 126:                     X2 = 0;
 127:                     Y2 = Generator.Next(Height);
 128:                 }
 129:             }
 130:             int M = (Y1 - Y2) / (X1 - X2);
 131:             int B = Y1 - (M * X1);
 132:             int Side = Generator.Next(2);
 133:             int Direction = 0;
 134:             while (Direction == 0)
 135:                 Direction = Generator.Next(-1, 2);
 136:             float TempIncreaseVal = (float)Generator.NextDouble() * IncreaseVal * (float)Direction;
 137:             if (Side == 0)
 138:             {
 139:                 for (int y = 0; y < Width; ++y)
 140:                 {
 141:                     int LastY = (M * y) + B;
 142:                     for (int z = 0; z < LastY; ++z)
 143:                     {
 144:                         if (z < Height)
 145:                         {
 146:                             Heights[y, z] += TempIncreaseVal;
 147:                             if (Heights[y, z] > 1.0f)
 148:                                 Heights[y, z] = 1.0f;
 149:                             else if (Heights[y, z] < -1.0f)
 150:                                 Heights[y, z] = -1.0f;
 151:                         }
 152:                     }
 153:                 }
 154:             }
 155:             else
 156:             {
 157:                 for (int y = 0; y < Width; ++y)
 158:                 {
 159:                     int LastY = (M * y) + B;
 160:                     if (LastY < 0)
 161:                         LastY = 0;
 162:                     for (int z = LastY; z < Height; ++z)
 163:                     {
 164:                         Heights[y, z] += TempIncreaseVal;
 165:                         if (Heights[y, z] > 1.0f)
 166:                             Heights[y, z] = 1.0f;
 167:                         else if (Heights[y, z] < -1.0f)
 168:                             Heights[y, z] = -1.0f;
 169:                     }
 170:                 }
 171:             }
 172:             IncreaseVal -= (0.1f / (float)NumberFaults);
 173:             return IncreaseVal;
 174:         }
 175:     }
 176: }

So the basic call to Generate will create a bitmap, using the variables you send in. The width, height, and seed are probably rather simple to grasp, but the number of faults may take some testing. For something decent looking you need a large number. I'm talking about 10000 and that will take a little while to run. Also note that this uses some code from my utility library to lock/unlock the bitmap to speed it up, you can find that code here. Anyway, play around with it and definitely see if you can improve my implementation as it has some flaws (namely speed) and feedback is always appreciated.



Comments