Esempio n. 1
0
        //public static TerrainOutputImage getVectorFieldImage(Vector[][] vectorField)
        //{
        //    int width = CELL_SIZE * vectorField.Length;
        //    int height = CELL_SIZE * vectorField[0].Length;

        //    Pen linePen = new Pen(Color.Black, 1);
        //    Pen directionPen = new Pen(Color.Red, 1);
        //    Bitmap bitmap = new Bitmap(width, height);
        //    Graphics g = Graphics.FromImage(bitmap);
        //    g.SmoothingMode = SmoothingMode.HighQuality;
        //    g.Clear(Color.White);

        //    for(int x = 0; x < vectorField.Length; x++)
        //    {
        //        for (int y = 0; y < vectorField[x].Length; y++)
        //        {
        //            drawVector(x * CELL_SIZE + CELL_SIZE/2, y * CELL_SIZE + CELL_SIZE/2, vectorField[x][y], g, linePen, directionPen);
        //        }
        //    }

        //    TerrainOutputImage vectorFieldOutputImage = new TerrainOutputImage();
        //    vectorFieldOutputImage.Title = "Vector field";
        //    vectorFieldOutputImage.Key = "vectorField";

        //    vectorFieldOutputImage.ImageData = new int[bitmap.Width][];

        //    for (int i = 0; i < bitmap.Width; i++)
        //    {
        //        vectorFieldOutputImage.ImageData[i] = new int[bitmap.Height];
        //        for (int j = 0; j < bitmap.Height; j++)
        //        {
        //            Color color = bitmap.GetPixel(i, j);
        //            vectorFieldOutputImage.ImageData[i][j] = (color.R << 24) | (color.G << 16) | (color.B << 8) | 0xff;
        //        }
        //    }
        //    return vectorFieldOutputImage;
        //}

        //private static void drawVector(int posX, int posY, Vector vector, Graphics g, Pen linePen, Pen directionPen)
        //{
        //    float amplification = (CELL_SIZE/2)-1;

        //    GraphicsState state = g.Save();

        //    g.TranslateTransform(posX, posY);
        //    g.DrawLine(directionPen, 0, 0, vector.Vx * amplification, vector.Vy * amplification);
        //    g.DrawLine(linePen, 0, 0, vector.Vx * -amplification, vector.Vy * -amplification);

        //    g.Restore(state);
        //}

        private static void getLowestNeighbour(float?[][] heightMap, int x, int y, out int finalX, out int finalY)
        {
            float?currentHeight = 9999;
            int   nX;
            int   nY;

            finalX = x;
            finalY = y;

            float?neighbourHeight = TerrainHelpers.getLeft(heightMap, x, y, out nX, out nY);

            if (neighbourHeight != null && neighbourHeight < currentHeight)
            {
                currentHeight = neighbourHeight;
                finalX        = nX;
                finalY        = nY;
            }

            neighbourHeight = TerrainHelpers.getLeftLeftTop(heightMap, x, y, out nX, out nY);
            if (neighbourHeight != null && neighbourHeight < currentHeight)
            {
                currentHeight = neighbourHeight;
                finalX        = nX;
                finalY        = nY;
            }

            neighbourHeight = TerrainHelpers.getTopLeft(heightMap, x, y, out nX, out nY);
            if (neighbourHeight != null && neighbourHeight < currentHeight)
            {
                currentHeight = neighbourHeight;
                finalX        = nX;
                finalY        = nY;
            }

            neighbourHeight = TerrainHelpers.getTopTopLeft(heightMap, x, y, out nX, out nY);
            if (neighbourHeight != null && neighbourHeight < currentHeight)
            {
                currentHeight = neighbourHeight;
                finalX        = nX;
                finalY        = nY;
            }

            neighbourHeight = TerrainHelpers.getTop(heightMap, x, y, out nX, out nY);
            if (neighbourHeight != null && neighbourHeight < currentHeight)
            {
                currentHeight = neighbourHeight;
                finalX        = nX;
                finalY        = nY;
            }

            neighbourHeight = TerrainHelpers.getTopTopRight(heightMap, x, y, out nX, out nY);
            if (neighbourHeight != null && neighbourHeight < currentHeight)
            {
                currentHeight = neighbourHeight;
                finalX        = nX;
                finalY        = nY;
            }

            neighbourHeight = TerrainHelpers.getTopRight(heightMap, x, y, out nX, out nY);
            if (neighbourHeight != null && neighbourHeight < currentHeight)
            {
                currentHeight = neighbourHeight;
                finalX        = nX;
                finalY        = nY;
            }

            neighbourHeight = TerrainHelpers.getRightRightTop(heightMap, x, y, out nX, out nY);
            if (neighbourHeight != null && neighbourHeight < currentHeight)
            {
                currentHeight = neighbourHeight;
                finalX        = nX;
                finalY        = nY;
            }

            neighbourHeight = TerrainHelpers.getRight(heightMap, x, y, out nX, out nY);
            if (neighbourHeight != null && neighbourHeight < currentHeight)
            {
                currentHeight = neighbourHeight;
                finalX        = nX;
                finalY        = nY;
            }

            neighbourHeight = TerrainHelpers.getRightRightBottom(heightMap, x, y, out nX, out nY);
            if (neighbourHeight != null && neighbourHeight < currentHeight)
            {
                currentHeight = neighbourHeight;
                finalX        = nX;
                finalY        = nY;
            }

            neighbourHeight = TerrainHelpers.getBottomRight(heightMap, x, y, out nX, out nY);
            if (neighbourHeight != null && neighbourHeight < currentHeight)
            {
                currentHeight = neighbourHeight;
                finalX        = nX;
                finalY        = nY;
            }

            neighbourHeight = TerrainHelpers.getBottomBottomRight(heightMap, x, y, out nX, out nY);
            if (neighbourHeight != null && neighbourHeight < currentHeight)
            {
                currentHeight = neighbourHeight;
                finalX        = nX;
                finalY        = nY;
            }

            neighbourHeight = TerrainHelpers.getBottom(heightMap, x, y, out nX, out nY);
            if (neighbourHeight != null && neighbourHeight < currentHeight)
            {
                currentHeight = neighbourHeight;
                finalX        = nX;
                finalY        = nY;
            }

            neighbourHeight = TerrainHelpers.getBottomBottomLeft(heightMap, x, y, out nX, out nY);
            if (neighbourHeight != null && neighbourHeight < currentHeight)
            {
                currentHeight = neighbourHeight;
                finalX        = nX;
                finalY        = nY;
            }

            neighbourHeight = TerrainHelpers.getBottomLeft(heightMap, x, y, out nX, out nY);
            if (neighbourHeight != null && neighbourHeight < currentHeight)
            {
                currentHeight = neighbourHeight;
                finalX        = nX;
                finalY        = nY;
            }

            neighbourHeight = TerrainHelpers.getLeftLeftBottom(heightMap, x, y, out nX, out nY);
            if (neighbourHeight != null && neighbourHeight < currentHeight)
            {
                currentHeight = neighbourHeight;
                finalX        = nX;
                finalY        = nY;
            }
        }
Esempio n. 2
0
        private static Vector getCellVector(float?[][] heightMap, int x, int y)
        {
            float currentVx = 0;
            float currentVy = 0;

            int nX;
            int nY;

            List <Vector> vectors = new List <Vector>();

            float?currentValue = TerrainHelpers.getTopLeft(heightMap, x, y, out nX, out nY);

            if (currentValue != null && currentValue < heightMap[x][y])
            {
                currentValue = heightMap[x][y] - currentValue;
                currentValue = (float)Math.Cos(Math.PI / 4) * currentValue;
                currentVx    = -(float)currentValue;
                currentVy    = -(float)currentValue;
                vectors.Add(new Vector(currentVx, currentVy));
            }

            currentValue = TerrainHelpers.getLeft(heightMap, x, y, out nX, out nY);
            if (currentValue != null && currentValue < heightMap[x][y])
            {
                currentValue = heightMap[x][y] - currentValue;
                currentVx    = -(float)currentValue;
                currentVy    = 0;
                vectors.Add(new Vector(currentVx, currentVy));
            }

            currentValue = TerrainHelpers.getBottomLeft(heightMap, x, y, out nX, out nY);
            if (currentValue != null && currentValue < heightMap[x][y])
            {
                currentValue = heightMap[x][y] - currentValue;
                currentValue = (float)Math.Cos(Math.PI / 4) * currentValue;
                currentVx    = -(float)currentValue;
                currentVy    = (float)currentValue;
                vectors.Add(new Vector(currentVx, currentVy));
            }

            currentValue = TerrainHelpers.getTop(heightMap, x, y, out nX, out nY);
            if (currentValue != null && currentValue < heightMap[x][y])
            {
                currentValue = heightMap[x][y] - currentValue;
                currentVx    = 0;
                currentVy    = -(float)currentValue;
                vectors.Add(new Vector(currentVx, currentVy));
            }

            currentValue = TerrainHelpers.getBottom(heightMap, x, y, out nX, out nY);
            if (currentValue != null && currentValue < heightMap[x][y])
            {
                currentValue = heightMap[x][y] - currentValue;
                currentVx    = 0;
                currentVy    = (float)currentValue;
                vectors.Add(new Vector(currentVx, currentVy));
            }

            currentValue = TerrainHelpers.getTopRight(heightMap, x, y, out nX, out nY);
            if (currentValue != null && currentValue < heightMap[x][y])
            {
                currentValue = heightMap[x][y] - currentValue;
                currentValue = (float)Math.Cos(Math.PI / 4) * currentValue;
                currentVx    = (float)currentValue;
                currentVy    = -(float)currentValue;
                vectors.Add(new Vector(currentVx, currentVy));
            }

            currentValue = TerrainHelpers.getRight(heightMap, x, y, out nX, out nY);
            if (currentValue != null && currentValue < heightMap[x][y])
            {
                currentValue = heightMap[x][y] - currentValue;
                currentVx    = (float)currentValue;
                currentVy    = 0;
                vectors.Add(new Vector(currentVx, currentVy));
            }

            currentValue = TerrainHelpers.getBottomRight(heightMap, x, y, out nX, out nY);
            if (currentValue != null && currentValue < heightMap[x][y])
            {
                currentValue = heightMap[x][y] - currentValue;
                currentValue = (float)Math.Cos(Math.PI / 4) * currentValue;
                currentVx    = (float)currentValue;
                currentVy    = (float)currentValue;
                vectors.Add(new Vector(currentVx, currentVy));
            }



            currentValue = TerrainHelpers.getTopTopLeft(heightMap, x, y, out nX, out nY);
            if (currentValue != null && currentValue < heightMap[x][y])
            {
                currentValue = heightMap[x][y] - currentValue;
                float larger  = (float)(Math.Cos(Math.Atan(0.5)) * currentValue);
                float smaller = (float)(Math.Sin(Math.Atan(0.5)) * currentValue);
                currentVx = -smaller;
                currentVy = -larger;
                vectors.Add(new Vector(currentVx, currentVy));
            }

            currentValue = TerrainHelpers.getLeftLeftTop(heightMap, x, y, out nX, out nY);
            if (currentValue != null && currentValue < heightMap[x][y])
            {
                currentValue = heightMap[x][y] - currentValue;
                float larger  = (float)(Math.Cos(Math.Atan(0.5)) * currentValue);
                float smaller = (float)(Math.Sin(Math.Atan(0.5)) * currentValue);
                currentVx = -larger;
                currentVy = -smaller;
                vectors.Add(new Vector(currentVx, currentVy));
            }

            currentValue = TerrainHelpers.getTopTopRight(heightMap, x, y, out nX, out nY);
            if (currentValue != null && currentValue < heightMap[x][y])
            {
                currentValue = heightMap[x][y] - currentValue;
                float larger  = (float)(Math.Cos(Math.Atan(0.5)) * currentValue);
                float smaller = (float)(Math.Sin(Math.Atan(0.5)) * currentValue);
                currentVx = smaller;
                currentVy = -larger;
                vectors.Add(new Vector(currentVx, currentVy));
            }

            currentValue = TerrainHelpers.getRightRightTop(heightMap, x, y, out nX, out nY);
            if (currentValue != null && currentValue < heightMap[x][y])
            {
                currentValue = heightMap[x][y] - currentValue;
                float larger  = (float)(Math.Cos(Math.Atan(0.5)) * currentValue);
                float smaller = (float)(Math.Sin(Math.Atan(0.5)) * currentValue);
                currentVx = larger;
                currentVy = -smaller;
                vectors.Add(new Vector(currentVx, currentVy));
            }



            currentValue = TerrainHelpers.getRightRightBottom(heightMap, x, y, out nX, out nY);
            if (currentValue != null && currentValue < heightMap[x][y])
            {
                currentValue = heightMap[x][y] - currentValue;
                float larger  = (float)(Math.Cos(Math.Atan(0.5)) * currentValue);
                float smaller = (float)(Math.Sin(Math.Atan(0.5)) * currentValue);
                currentVx = larger;
                currentVy = smaller;
                vectors.Add(new Vector(currentVx, currentVy));
            }

            currentValue = TerrainHelpers.getBottomBottomRight(heightMap, x, y, out nX, out nY);
            if (currentValue != null && currentValue < heightMap[x][y])
            {
                currentValue = heightMap[x][y] - currentValue;
                float larger  = (float)(Math.Cos(Math.Atan(0.5)) * currentValue);
                float smaller = (float)(Math.Sin(Math.Atan(0.5)) * currentValue);
                currentVx = smaller;
                currentVy = larger;
                vectors.Add(new Vector(currentVx, currentVy));
            }

            currentValue = TerrainHelpers.getBottomBottomLeft(heightMap, x, y, out nX, out nY);
            if (currentValue != null && currentValue < heightMap[x][y])
            {
                currentValue = heightMap[x][y] - currentValue;
                float larger  = (float)(Math.Cos(Math.Atan(0.5)) * currentValue);
                float smaller = (float)(Math.Sin(Math.Atan(0.5)) * currentValue);
                currentVx = -smaller;
                currentVy = larger;
                vectors.Add(new Vector(currentVx, currentVy));
            }

            currentValue = TerrainHelpers.getLeftLeftBottom(heightMap, x, y, out nX, out nY);
            if (currentValue != null && currentValue < heightMap[x][y])
            {
                currentValue = heightMap[x][y] - currentValue;
                float larger  = (float)(Math.Cos(Math.Atan(0.5)) * currentValue);
                float smaller = (float)(Math.Sin(Math.Atan(0.5)) * currentValue);
                currentVx = -larger;
                currentVy = smaller;
                vectors.Add(new Vector(currentVx, currentVy));
            }



            Vector finalVector = new Vector(0, 0);

            foreach (Vector vector in vectors)
            {
                finalVector.Vx += vector.Vx;
                finalVector.Vy += vector.Vy;
            }

            finalVector.normalize();

            return(finalVector);
        }