Example #1
0
        private Corners MakeCorner(VoronoiPoint cornerPnt)
        {
            if (cornerPnt == null)
            {
                return(null);
            }
            for (var bck = (int)cornerPnt.X - 1; bck <= (int)cornerPnt.X + 1; bck++)
            {
                if (_cornerLookup.ContainsKey(bck))
                {
                    foreach (Corners q in _cornerLookup[bck])
                    {
                        var dx = cornerPnt.X - q.location.X;
                        var dy = cornerPnt.Y - q.location.Y;
                        if (Math.Pow(dx, 2) + Math.Pow(dy, 2) < Math.Pow(10, -5))
                        {
                            return(q);
                        }
                    }
                }
            }
            var bucket = (int)cornerPnt.X;

            if (!_cornerLookup.ContainsKey(bucket))
            {
                _cornerLookup[bucket] = new List <Corners>();
            }
            var newcorner = new Corners(_cornerLookup.Count, cornerPnt);

            _cornerLookup[bucket].Add(newcorner);
            cornerlist.Add(newcorner);
            return(newcorner);
        }
Example #2
0
        public void CenterEquality_SameCenters_HashTheSameasLocation()
        {
            var mainlocation = new VoronoiPoint(5, 7);
            var sut          = new Centers(2, mainlocation);

            Assert.IsTrue(mainlocation.GetHashCode() == sut.GetHashCode());
        }
Example #3
0
        private int DistanceSquared(VoronoiPoint a, VoronoiPoint b)
        {
            var x = b.X - a.X;
            var y = b.Y - a.Y;

            return(x * x + y * y);
        }
Example #4
0
        public void CenterEquality_SameCenters_SameLocObjectsEqual_ReturnsTrue()
        {
            var mainlocation = new VoronoiPoint(5, 7);
            var sut          = new Centers(2, mainlocation);
            var test         = new Centers(7, mainlocation);

            Assert.IsTrue(sut.Equals(test));
        }
Example #5
0
        public void CenterEquality_SameCenters_DiffLocObjectsNotEqual_ReturnsFalse()
        {
            var mainlocation = new VoronoiPoint(5, 7);
            var sut          = new Centers(2, mainlocation);
            var test         = new Centers(7, new VoronoiPoint(7, 5));

            Assert.IsFalse(sut.Equals(test));
        }
Example #6
0
 public Centers(int indx, VoronoiPoint polycenter)
 {
     index    = indx;
     center   = polycenter;
     neigbors = new List <Centers>();
     borders  = new List <Edges>();
     corners  = new List <Corners>();
     mapData  = new CenterMapData(this);
 }
Example #7
0
 public Corners(int ind, VoronoiPoint pointloc)
 {
     index     = ind;
     location  = pointloc;
     border    = (pointloc.X <= 0 || pointloc.X >= 10 || pointloc.Y <= 0 || pointloc.Y > 10);
     touches   = new List <Centers>();
     protrudes = new List <Edges>();
     adjacents = new List <Corners>();
     mapdata   = new CornerMapData();
 }
Example #8
0
 public MapTile(int region, float height, float ground, float feature, float moisture, float temperature, VoronoiPoint position)
 {
     this.region      = region;
     this.height      = height;
     this.ground      = ground;
     this.feature     = feature;
     this.moisture    = feature;
     this.temperature = temperature;
     this.pos         = position;
 }
Example #9
0
 public Edges(int index, Centers leftCenter, Centers rightCenter, Corners startCorner, Corners endCorner, VoronoiPoint mdpt)
 {
     Index           = index;
     delaunayCenter1 = leftCenter;
     delaunayCenter2 = rightCenter;
     voronoiCorner1  = startCorner;
     voronoiCorner2  = endCorner;
     midpoint        = mdpt;
     BordersAndProtrudes();
     mapdata = new EdgeMapData();
 }
Example #10
0
 public void addPoint(VoronoiPoint point_)
 {
     foreach (VoronoiPoint point in points)
       {
      if (point.Position == point_.Position)
      {
         return;
      }
       }
       point_.Parent = this;
       points.Add(point_);
 }
Example #11
0
        private VoronoiPoint[,] GenerateNoiseGradient(int v)
        {
            var gradient = new VoronoiPoint[v, v];

            for (int i = 0; i < v; i++)
            {
                for (int f = 0; f < v; f++)
                {
                    var angle = random.NextDouble() * 2 * Math.PI;
                    gradient[i, f] = new VoronoiPoint(Math.Cos(angle), Math.Sin(angle));
                }
            }
            return(gradient);
        }
Example #12
0
 private Centers CenterWarehouse(VoronoiPoint centerPoint)
 {
     if (_centerLookup.ContainsKey(centerPoint.guid))
     {
         return(_centerLookup[centerPoint.guid]);
     }
     else
     {
         var newcenter = new Centers(_centerLookup.Count, centerPoint);
         _centerLookup[newcenter.GetGuid()] = newcenter;
         centerlist.Add(newcenter);
         return(newcenter);
     }
 }
Example #13
0
 private bool VoronoiPointAlreadyExists(VoronoiPoint voronoiPoint)
 {
     foreach (VoronoiPoint p in voronoiPoints)
     {
         if (p == null)
         {
             return(false);
         }
         if (p.X == voronoiPoint.X && p.Y == voronoiPoint.Y)
         {
             return(true);
         }
     }
     return(false);
 }
Example #14
0
        private int MinDistance(VoronoiPoint basePoint, VoronoiPoint[] points)
        {
            int min     = int.MaxValue;
            int current = 0;

            foreach (var point in points)
            {
                current = DistanceSquared(basePoint, point);
                if (current < min)
                {
                    min = current;
                }
            }

            return(min);
        }
Example #15
0
        public static bool ContainsItem(this List <VoronoiPoint> a, VoronoiPoint b)
        {
            if (a.Count == 0)
            {
                return(false);
            }

            foreach (var e in a)
            {
                if (b.x == e.x && b.y == e.y)
                {
                    return(true);
                }
            }

            return(false);
        }
Example #16
0
    private byte[,] GenerateByteMap(int mapWidth, int mapHeight)
    {
        byte[,] map = new byte[mapWidth, mapHeight];

        for (int x = 0; x < mapWidth; x++)
        {
            for (int y = 0; y < mapHeight; y++)
            {
                VoronoiPoint nearestVP = NearestVoronoiPoint(x, y);
                map[x, y] = nearestVP.TileIndex;
                if (!regions.ContainsKey(nearestVP))
                {
                    regions.Add(nearestVP, new List <Vector2>());
                }

                regions[nearestVP].Add(new Vector2(x, y));
            }
        }

        return(map);
    }
Example #17
0
        public static bool ContainsItem(this VoronoiPoint[] a, VoronoiPoint b)
        {
            if (a[0] == null)
            {
                return(false);
            }

            foreach (var e in a)
            {
                if (e == null)
                {
                    continue;
                }
                if (b.x == e.x && b.y == e.y)
                {
                    return(true);
                }
            }

            return(false);
        }
Example #18
0
    private VoronoiPoint NearestVoronoiPoint(int x, int y)
    {
        float        smallestDistance = float.MaxValue;
        VoronoiPoint nearestPoint     = null;

        foreach (VoronoiPoint point in voronoiPoints)
        {
            if (point.X == x && point.Y == y)
            {
                return(point);
            }

            float distance = Mathf.Sqrt(Mathf.Pow(point.X - x, 2) + Mathf.Pow(point.Y - y, 2));
            if (distance < smallestDistance)
            {
                smallestDistance = distance;
                nearestPoint     = point;
            }
        }

        return(nearestPoint);
    }
Example #19
0
    private void GenerateVoronoiPoints(int mapWidth, int mapHeight, int pointCount)
    {
        if (mapWidth * mapHeight < pointCount)
        {
            throw new System.Exception("Es können nicht mehr VoronoiPoints exisitieren als die gesamt Anzahl an Tiles");
        }

        voronoiPoints = new VoronoiPoint[pointCount];

        for (int i = 0; i < pointCount; i++)
        {
            VoronoiPoint newPoint = new VoronoiPoint();
            do
            {
                newPoint.X = Random.Range(0, mapWidth);
                newPoint.Y = Random.Range(0, mapHeight);
            } while (VoronoiPointAlreadyExists(newPoint));

            newPoint.TileIndex = (byte)(lottery.Draw().Index + 1);//+1 weil der Index 0 für die Border reserviert ist
            voronoiPoints[i]   = newPoint;
        }
    }
Example #20
0
 private static float heuristic(VoronoiPoint a, VoronoiPoint b)
 {
     return((float)Math.Sqrt(Math.Pow((b.X - a.X), 2) + Math.Pow((b.Y - b.Y), 2)));
 }
Example #21
0
 public VoronoiPolygon(Voronoi parentDiagram_, VoronoiPoint original_, Color color_)
 {
     parentDiagram = parentDiagram_;
       originalPoint = original_;
       color = color_;
 }
Example #22
0
        /// <summary>
        /// Stained Glass Color Filter
        /// </summary>
        /// <param name="sourceBitmap">Set source Bitmap</param>
        /// <param name="blockSize">Set block size</param>
        /// <param name="blockFactor">Set block factor</param>
        /// <param name="distanceType">Set distance type</param>
        /// <param name="highlightEdges">Highlight Edges</param>
        /// <param name="edgeThreshold">Set edge threshold</param>
        /// <param name="edgeColor">Set edge color</param>
        /// <returns></returns>
        public static System.Drawing.Bitmap StainedGlassColorFilter(this System.Drawing.Bitmap sourceBitmap,
                                                                    int blockSize, double blockFactor,
                                                                    DistanceFormulaType distanceType,
                                                                    bool highlightEdges,
                                                                    byte edgeThreshold, Color edgeColor)
        {
            BitmapData sourceData =
                sourceBitmap.LockBits(new Rectangle(0, 0,
                                                    sourceBitmap.Width, sourceBitmap.Height),
                                      ImageLockMode.ReadOnly,
                                      PixelFormat.Format32bppArgb);

            byte[] pixelBuffer = new byte[sourceData.Stride *
                                          sourceData.Height];

            byte[] resultBuffer = new byte[sourceData.Stride *
                                           sourceData.Height];

            Marshal.Copy(sourceData.Scan0, pixelBuffer, 0,
                         pixelBuffer.Length);

            sourceBitmap.UnlockBits(sourceData);

            int neighbourHoodTotal   = 0;
            int sourceOffset         = 0;
            int resultOffset         = 0;
            int currentPixelDistance = 0;
            int nearestPixelDistance = 0;
            int nearesttPointIndex   = 0;

            Random randomizer = new Random();

            List <VoronoiPoint> randomPointList = new List <VoronoiPoint>();

            for (int row = 0; row < sourceBitmap.Height - blockSize; row += blockSize)
            {
                for (int col = 0; col < sourceBitmap.Width - blockSize; col += blockSize)
                {
                    sourceOffset = row * sourceData.Stride + col * 4;

                    neighbourHoodTotal = 0;

                    for (int y = 0; y < blockSize; y++)
                    {
                        for (int x = 0; x < blockSize; x++)
                        {
                            resultOffset        = sourceOffset + y * sourceData.Stride + x * 4;
                            neighbourHoodTotal += pixelBuffer[resultOffset];
                            neighbourHoodTotal += pixelBuffer[resultOffset + 1];
                            neighbourHoodTotal += pixelBuffer[resultOffset + 2];
                        }
                    }

                    randomizer = new Random(neighbourHoodTotal);

                    VoronoiPoint randomPoint = new VoronoiPoint();
                    randomPoint.XOffset = randomizer.Next(0, blockSize) + col;
                    randomPoint.YOffset = randomizer.Next(0, blockSize) + row;

                    randomPointList.Add(randomPoint);
                }
            }

            int rowOffset = 0;
            int colOffset = 0;

            for (int bufferOffset = 0; bufferOffset < pixelBuffer.Length - 4; bufferOffset += 4)
            {
                rowOffset = bufferOffset / sourceData.Stride;
                colOffset = (bufferOffset % sourceData.Stride) / 4;

                currentPixelDistance = 0;
                nearestPixelDistance = blockSize * 4;
                nearesttPointIndex   = 0;

                List <VoronoiPoint> pointSubset = new List <VoronoiPoint>();

                pointSubset.AddRange(from t in randomPointList
                                     where
                                     rowOffset >= t.YOffset - blockSize * 2 &&
                                     rowOffset <= t.YOffset + blockSize * 2
                                     select t);

                for (int k = 0; k < pointSubset.Count; k++)
                {
                    if (distanceType == DistanceFormulaType.Euclidean)
                    {
                        currentPixelDistance =
                            CalculateDistanceEuclidean(pointSubset[k].XOffset,
                                                       colOffset, pointSubset[k].YOffset, rowOffset);
                    }
                    else if (distanceType == DistanceFormulaType.Manhattan)
                    {
                        currentPixelDistance =
                            CalculateDistanceManhattan(pointSubset[k].XOffset,
                                                       colOffset, pointSubset[k].YOffset, rowOffset);
                    }
                    else if (distanceType == DistanceFormulaType.Chebyshev)
                    {
                        currentPixelDistance =
                            CalculateDistanceChebyshev(pointSubset[k].XOffset,
                                                       colOffset, pointSubset[k].YOffset, rowOffset);
                    }

                    if (currentPixelDistance <= nearestPixelDistance)
                    {
                        nearestPixelDistance = currentPixelDistance;
                        nearesttPointIndex   = k;

                        if (nearestPixelDistance <= blockSize / blockFactor)
                        {
                            break;
                        }
                    }
                }

                Pixel tmpPixel = new Pixel();
                tmpPixel.XOffset = colOffset;
                tmpPixel.YOffset = rowOffset;
                tmpPixel.Blue    = pixelBuffer[bufferOffset];
                tmpPixel.Green   = pixelBuffer[bufferOffset + 1];
                tmpPixel.Red     = pixelBuffer[bufferOffset + 2];

                pointSubset[nearesttPointIndex].AddPixel(tmpPixel);
            }

            for (int k = 0; k < randomPointList.Count; k++)
            {
                randomPointList[k].CalculateAverages();

                for (int i = 0; i < randomPointList[k].PixelCollection.Count; i++)
                {
                    resultOffset = randomPointList[k].PixelCollection[i].YOffset *
                                   sourceData.Stride +
                                   randomPointList[k].PixelCollection[i].XOffset * 4;

                    resultBuffer[resultOffset]     = (byte)randomPointList[k].BlueAverage;
                    resultBuffer[resultOffset + 1] = (byte)randomPointList[k].GreenAverage;
                    resultBuffer[resultOffset + 2] = (byte)randomPointList[k].RedAverage;

                    resultBuffer[resultOffset + 3] = 255;
                }
            }

            System.Drawing.Bitmap resultBitmap = new System.Drawing.Bitmap(sourceBitmap.Width,
                                                                           sourceBitmap.Height);

            BitmapData resultData =
                resultBitmap.LockBits(new Rectangle(0, 0,
                                                    resultBitmap.Width, resultBitmap.Height),
                                      ImageLockMode.WriteOnly,
                                      PixelFormat.Format32bppArgb);

            Marshal.Copy(resultBuffer, 0, resultData.Scan0,
                         resultBuffer.Length);

            resultBitmap.UnlockBits(resultData);

            if (highlightEdges == true)
            {
                resultBitmap =
                    resultBitmap.GradientBasedEdgeDetectionFilter(edgeColor, edgeThreshold);
            }

            return(resultBitmap);
        }
Example #23
0
    VoronoiPolygon getClosestPolygonToPoint(VoronoiPoint point_)
    {
        VoronoiPolygon returnee = null;
          float currentDistance = -1;
          foreach (VoronoiPolygon polygon in polygons)
          {
         float tempDistance = Vector3.Distance(point_.Position, polygon.OriginalPoint.Position);
         if (currentDistance == -1 || currentDistance > tempDistance)
         {
            returnee = polygon;
            currentDistance = tempDistance;
         }
          }

          return returnee;
    }
Example #24
0
    void generateRegions(int width_, int height_, List<Vector2> points_)
    {
        polygons.Clear();

          map = new VoronoiPoint[width, height];

          for (int i = 0; i < points_.Count; ++i)
          {
         VoronoiPolygon polygon = new VoronoiPolygon(this, new VoronoiPoint(points_[i]), new Color(Random.Range(0.0f, 1.0f), Random.Range(0.0f, 1.0f), Random.Range(0.0f, 1.0f)));
         polygons.Add(polygon);
          }

          for (int i = 0; i < height; ++i)
          {
         for (int j = 0; j < width; ++j)
         {
            map[j, i] = new VoronoiPoint(new Vector2(j, i));
            VoronoiPolygon closestPoly = getClosestPolygonToPoint(map[j, i]);

            closestPoly.addPoint(map[j, i]);
         }
          }

          foreach (VoronoiPolygon polygon in polygons)
          {
         polygon.generateEdges();
          }
    }
Example #25
0
 public Line(VoronoiPoint leftpoint, VoronoiPoint rightpoint)
 {
     startleft = leftpoint;
     endright  = rightpoint;
 }
Example #26
0
 public Point(VoronoiPoint vrnPoint)
 {
     X = vrnPoint.X;
     Y = vrnPoint.Y;
 }
Example #27
0
        /// <summary>
        /// Processes the image using a pixel buffer
        /// </summary>
        /// <param name="pixelBuffer">The pixel buffer to use</param>
        /// <param name="sourceWidth">The source image width</param>
        /// <param name="sourceHeight">The source image height</param>
        /// <param name="sourceStride">The source data stride</param>
        /// <returns>The processed pixel buffer</returns>
        protected override byte[] Process(byte[] pixelBuffer, int sourceWidth, int sourceHeight, int sourceStride)
        {
            StainedGlassParameters parameters = this.DynamicParameter;

            byte[] resultBuffer = new byte[sourceStride * sourceHeight];

            int neighbourHoodTotal   = 0;
            int sourceOffset         = 0;
            int resultOffset         = 0;
            int currentPixelDistance = 0;
            int nearestPixelDistance = 0;
            int nearesttPointIndex   = 0;

            Random randomizer = new Random();
            List <VoronoiPoint> randomPointList = new List <VoronoiPoint>();
            int blockSize = parameters.Size;

            for (int row = 0; row < sourceHeight - blockSize; row += blockSize)
            {
                for (int col = 0; col < sourceWidth - blockSize; col += blockSize)
                {
                    sourceOffset       = (row * sourceStride) + (col * 4);
                    neighbourHoodTotal = 0;

                    for (int y = 0; y < blockSize; y++)
                    {
                        for (int x = 0; x < blockSize; x++)
                        {
                            resultOffset        = sourceOffset + (y * sourceStride) + (x * 4);
                            neighbourHoodTotal += pixelBuffer[resultOffset];
                            neighbourHoodTotal += pixelBuffer[resultOffset + 1];
                            neighbourHoodTotal += pixelBuffer[resultOffset + 2];
                        }
                    }

                    randomizer = new Random(neighbourHoodTotal);
                    VoronoiPoint randomPoint = new VoronoiPoint();
                    randomPoint.XOffset = randomizer.Next(0, blockSize) + col;
                    randomPoint.YOffset = randomizer.Next(0, blockSize) + row;
                    randomPointList.Add(randomPoint);
                }
            }

            int rowOffset = 0;
            int colOffset = 0;

            for (int bufferOffset = 0; bufferOffset < pixelBuffer.Length - 4; bufferOffset += 4)
            {
                rowOffset            = bufferOffset / sourceStride;
                colOffset            = (bufferOffset % sourceStride) / 4;
                currentPixelDistance = 0;
                nearestPixelDistance = blockSize * 4;
                nearesttPointIndex   = 0;
                List <VoronoiPoint> pointSubset = new List <VoronoiPoint>();

                pointSubset.AddRange(from t in randomPointList
                                     where
                                     rowOffset >= t.YOffset - (blockSize * 2) &&
                                     rowOffset <= t.YOffset + (blockSize * 2)
                                     select t);

                for (int k = 0; k < pointSubset.Count; k++)
                {
                    if (parameters.DistanceFormula == StainedGlassParameters.Formula.Euclidean)
                    {
                        currentPixelDistance = CalculateDistanceEuclidean(pointSubset[k].XOffset, colOffset, pointSubset[k].YOffset, rowOffset);
                    }
                    else if (parameters.DistanceFormula == StainedGlassParameters.Formula.Manhattan)
                    {
                        currentPixelDistance = CalculateDistanceManhattan(pointSubset[k].XOffset, colOffset, pointSubset[k].YOffset, rowOffset);
                    }
                    else if (parameters.DistanceFormula == StainedGlassParameters.Formula.Chebyshev)
                    {
                        currentPixelDistance = CalculateDistanceChebyshev(pointSubset[k].XOffset, colOffset, pointSubset[k].YOffset, rowOffset);
                    }

                    if (currentPixelDistance <= nearestPixelDistance)
                    {
                        nearestPixelDistance = currentPixelDistance;
                        nearesttPointIndex   = k;

                        if (nearestPixelDistance <= blockSize / parameters.Factor)
                        {
                            break;
                        }
                    }
                }

                Pixel tmpPixel = new Pixel();
                tmpPixel.XOffset = colOffset;
                tmpPixel.YOffset = rowOffset;
                tmpPixel.Blue    = pixelBuffer[bufferOffset];
                tmpPixel.Green   = pixelBuffer[bufferOffset + 1];
                tmpPixel.Red     = pixelBuffer[bufferOffset + 2];
                pointSubset[nearesttPointIndex].AddPixel(tmpPixel);
            }

            for (int k = 0; k < randomPointList.Count; k++)
            {
                randomPointList[k].CalculateAverages();

                for (int i = 0; i < randomPointList[k].PixelCollection.Count; i++)
                {
                    resultOffset = (randomPointList[k].PixelCollection[i].YOffset * sourceStride) + (randomPointList[k].PixelCollection[i].XOffset * 4);
                    resultBuffer[resultOffset]     = (byte)randomPointList[k].BlueAverage;
                    resultBuffer[resultOffset + 1] = (byte)randomPointList[k].GreenAverage;
                    resultBuffer[resultOffset + 2] = (byte)randomPointList[k].RedAverage;
                    resultBuffer[resultOffset + 3] = 255;
                }
            }

            return(resultBuffer);
        }
Example #28
0
 public PlainsTile(int region, float height, float ground, float feature, float moisture, float temperature, VoronoiPoint position) :
     base(region, height, ground, feature, moisture, temperature, position)
 {
     this.region      = region;
     this.height      = height;
     this.ground      = ground;
     this.feature     = feature;
     this.moisture    = feature;
     this.temperature = temperature;
     this.pos         = position;
 }