public
Created

fills a 1000x1000 grid with 300 randomly shaped terrains based on 14 terrain types, inspired by http://stackoverflow.com/questions/6586338/randomly-and-efficiently-filling-space-with-shapes/6588235#6588235

  • Download Gist
Terrain generation demo
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
// * **********************************************************************************
// * Copyright (c) Clinton Sheppard
// * This source code is subject to terms and conditions of the MIT License.
// * By using this source code in any fashion, you are agreeing to be bound by
// * the terms of the MIT License.
// * You must not remove this notice from this software.
// * **********************************************************************************
 
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
 
using NUnit.Framework;
 
namespace Scratch.FillGrid
{
public class LandType
{
private static readonly List<LandType> _list = new List<LandType>();
public static readonly LandType AlluvialPlain = new LandType(Color.LightGray);
public static readonly LandType CoastalPlain = new LandType(Color.PaleGoldenrod);
public static readonly LandType CultivatedLand = new LandType(Color.IndianRed);
public static readonly LandType FloodPlain = new LandType(Color.Lavender);
public static readonly LandType Forest = new LandType(Color.ForestGreen);
public static readonly LandType ForestSwamp = new LandType(Color.BurlyWood);
public static readonly LandType GrassLand = new LandType(Color.DarkSeaGreen);
public static readonly LandType NeedleForest = new LandType(Color.DarkGreen);
public static readonly LandType Plateau = new LandType(Color.RosyBrown);
public static readonly LandType Sand = new LandType(Color.SandyBrown);
public static readonly LandType Savannah = new LandType(Color.SpringGreen);
public static readonly LandType SemiArid = new LandType(Color.PapayaWhip);
public static readonly LandType Swamp = new LandType(Color.CadetBlue);
public static readonly LandType TidalMarsh = new LandType(Color.CornflowerBlue);
// .. etc, for more land form names see:
// http://makingmaps.net/2008/04/03/map-symbols-landforms-terrain/
 
private LandType(Color color)
{
_list.Add(this);
Color = color;
}
 
public Color Color { get; set; }
 
public static IEnumerable<LandType> GetAll()
{
return _list;
}
}
 
public static class IEnumerableExtensions
{
private static readonly Random _random = new Random();
public static T PickRandom<T>(this IEnumerable<T> items)
{
var list = new List<T>();
foreach (var item in items)
{
if (_random.Next(1000) == 0)
{
return item;
}
list.Add(item);
}
if (list.Count > 0)
{
return list[_random.Next(list.Count)];
}
return default(T);
}
}
 
[TestFixture]
public class Demo
{
public static int Height = 1000;
public static int Width = 1000;
public static int XOffsetEast = 1;
public static int XOffsetWest = -1;
 
public static int YOffsetNorth = -1;
public static int YOffsetSouth = 1;
 
[Test]
public void FillGrid()
{
var bitmap = new Bitmap(Width, Height);
var defaultPixel = bitmap.GetPixel(0, 0);
var hasOpenNeighbors = new HashSet<Land>();
var random = new Random();
var landTypes = LandType.GetAll().ToList();
for (int i = 0; i < 300; i++)
{
int randX = random.Next(Width);
int randY = random.Next(Height);
var point = new Point(randX, randY);
if (bitmap.GetPixel(randX, randY) == defaultPixel)
{
var landType = landTypes[random.Next(landTypes.Count)];
var item = new Land
{
Location = point,
LandType = landType
};
bitmap.SetPixel(randX, randY, landType.Color);
hasOpenNeighbors.Add(item);
}
}
 
while (hasOpenNeighbors.Any())
{
var toRemove = new List<Land>();
var toAdd = new List<Land>();
foreach (var item in hasOpenNeighbors)
{
var neighborLocation = GetNeighbors(item.Location)
.Where(x => bitmap.GetPixel(x.X, x.Y) == defaultPixel)
.PickRandom();
if (neighborLocation == default(Point))
{
toRemove.Add(item);
continue;
}
bitmap.SetPixel(neighborLocation.X, neighborLocation.Y, item.LandType.Color);
toAdd.Add(new Land
{
Location = neighborLocation,
LandType = item.LandType
});
}
foreach (var land in toRemove)
{
hasOpenNeighbors.Remove(land);
}
foreach (var land in toAdd)
{
hasOpenNeighbors.Add(land);
}
}
 
bitmap.Save("result.jpg");
}
 
public static Point CreatePoint(Point point, int xOffset, int yOffset)
{
return new Point(point.X + xOffset, point.Y + yOffset);
}
 
public static IEnumerable<Point> GetNeighbors(Point location)
{
return new Func<Point, Point>[]
{
GoNorth, GoNorthEast, GoEast, GoSouthEast,
GoSouth, GoSouthWest, GoWest, GoNorthWest
}
.Select(direction => direction(location))
.Where(IsOnTheBoard);
}
 
public static Point GoEast(Point point)
{
return CreatePoint(point, XOffsetEast, 0);
}
 
public static Point GoNorth(Point point)
{
return CreatePoint(point, 0, YOffsetNorth);
}
 
public static Point GoNorthEast(Point point)
{
return CreatePoint(point, XOffsetEast, YOffsetNorth);
}
 
public static Point GoNorthWest(Point point)
{
return CreatePoint(point, XOffsetWest, YOffsetNorth);
}
 
public static Point GoSouth(Point point)
{
return CreatePoint(point, 0, YOffsetSouth);
}
 
public static Point GoSouthEast(Point point)
{
return CreatePoint(point, XOffsetEast, YOffsetSouth);
}
 
public static Point GoSouthWest(Point point)
{
return CreatePoint(point, XOffsetWest, YOffsetSouth);
}
 
public static Point GoWest(Point point)
{
return CreatePoint(point, XOffsetWest, 0);
}
 
public static bool IsOnTheBoard(Point point)
{
return point.X >= 0 && point.X < Width && point.Y >= 0 && point.Y < Height;
}
 
public class Land
{
public LandType LandType { get; set; }
public Point Location { get; set; }
}
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.