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: }