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.Drawing;
24: using System.Drawing.Imaging;
25: #endregion
26:
27: namespace Utilities.Media.Image
28: { 29: /// <summary>
30: /// Cellular texture helper
31: /// </summary>
32: public static class CellularTexture
33: { 34: #region Functions
35:
36: /// <summary>
37: /// Generates a cellular texture image
38: /// </summary>
39: /// <param name="Width">Width</param>
40: /// <param name="Height">Height</param>
41: /// <param name="NumberOfPoints">Number of points</param>
42: /// <param name="Seed">Random seed</param>
43: /// <returns>Returns an image of a cellular texture</returns>
44: public static Bitmap Generate(int Width, int Height, int NumberOfPoints, int Seed)
45: { 46: float[,] DistanceBuffer = new float[Width, Height];
47: Points[] PointArray = new Points[NumberOfPoints];
48: float MinimumDistance = float.MaxValue;
49: float MaxDistance = float.MinValue;
50: System.Random Generator = new System.Random(Seed);
51: for (int x = 0; x < NumberOfPoints; ++x)
52: { 53: PointArray[x].X = Generator.Next(0, Width);
54: PointArray[x].Y = Generator.Next(0, Height);
55: }
56: for (int y = 0; y < Height; ++y)
57: { 58: for (int x = 0; x < Width; ++x)
59: { 60: DistanceBuffer[x, y] = DistanceNearestPoint(x, y, PointArray);
61: if (DistanceBuffer[x, y] > MaxDistance)
62: MaxDistance = DistanceBuffer[x, y];
63: else if (DistanceBuffer[x, y] < MinimumDistance)
64: MinimumDistance = DistanceBuffer[x, y];
65: }
66: }
67: Bitmap ReturnValue = new Bitmap(Width, Height);
68: BitmapData ImageData = Image.LockImage(ReturnValue);
69: int ImagePixelSize = Image.GetPixelSize(ImageData);
70: for (int x = 0; x < Width; ++x)
71: { 72: for (int y = 0; y < Height; ++y)
73: { 74: float Value = GetHeight(x, y, DistanceBuffer, MinimumDistance, MaxDistance);
75: Value *= 255;
76: int RGBValue = Math.MathHelper.Clamp((int)Value, 255, 0);
77: Image.SetPixel(ImageData, x, y, Color.FromArgb(RGBValue, RGBValue, RGBValue), ImagePixelSize);
78: }
79: }
80: Image.UnlockImage(ReturnValue, ImageData);
81: return ReturnValue;
82: }
83:
84: private static float GetHeight(float X, float Y, float[,] DistanceBuffer,
85: float MinimumDistance,float MaxDistance)
86: { 87: return (DistanceBuffer[(int)X, (int)Y] - MinimumDistance) / (MaxDistance - MinimumDistance);
88: }
89:
90: private static float DistanceNearestPoint(int x, int y,Points[] PointArray)
91: { 92: float Lowest = float.MaxValue;
93: for (int z = 0; z < PointArray.Length; ++z)
94: { 95: float Distance = (float)System.Math.Sqrt(((PointArray[z].X - x) * (PointArray[z].X - x)) + ((PointArray[z].Y - y) * (PointArray[z].Y - y)));
96: if (Distance < Lowest)
97: { 98: Lowest = Distance;
99: }
100: }
101: return Lowest;
102: }
103:
104: #endregion
105: }
106:
107: #region Structs
108:
109: internal struct Points
110: { 111: public float X;
112: public float Y;
113: }
114:
115: #endregion
116: }