예제 #1
0
        private async Task <FunctionResult> CallFunction(FractalOptions options)
        {
            var request = TestFactory.CreateHttpRequest(options);
            var result  = (OkObjectResult)await EntryPoint.RunLocally(request, _logger);

            return((FunctionResult)result.Value);;
        }
        protected FractalGenerator(FractalOptions options)
        {
            Options = options;
            var element = MathElement.Parse(Options.Expression, Options.VariableName);

            Function = element.ToNewtonFunction(Options.Multiplicity).ToFunc();
        }
예제 #3
0
 void BuildTerrainTextures(FractalOptions options)
 {
     int[,] fracMap = GetMapFromFractal(options, new float[] { 5, 3 });
     map            = fracMap;
     //float[, ,] map = new float[t.terrainData.alphamapWidth, t.terrainData.alphamapHeight, 2];
     // For each point on the alphamap...
     ApplyTextureFromFracMap(fracMap, 0);
 }
예제 #4
0
        public static DefaultHttpRequest CreateHttpRequest(FractalOptions options)
        {
            var serialized = JsonConvert.SerializeObject(options);
            var bytes      = Encoding.UTF8.GetBytes(serialized);
            var request    = new DefaultHttpRequest(new DefaultHttpContext())
            {
                Body = new MemoryStream(bytes),
            };

            return(request);
        }
예제 #5
0
        public async Task CanDetectParseErrors(string expression)
        {
            var options = new FractalOptions
            {
                Expression = expression,
            };

            var result = await CallFunction(options);

            Assert.NotNull(result.ErrorMessage);
        }
예제 #6
0
        public async Task CanGenerateFractalAsync()
        {
            var options = new FractalOptions
            {
                Expression = "z^3+1",
            };

            var result = await CallFunction(options);

            Assert.Null(result.ErrorMessage);
        }
예제 #7
0
        public List <PopulationCentre> buildData(out int[,] terrainMap)
        {
            var options      = new FractalOptions(10, 15);
            int n            = 10;
            var popMap       = WrappingFractal.fractal(128, n, n, options, FractalWrapMode.NoWrap, 89761234);
            var forestMap    = WrappingFractal.fractal(128, n, n, options, FractalWrapMode.NoWrap, 1245612);;
            var forestLevels = FractalUtility.getCutOffLevels(forestMap, new float[] { 80, 20 });
            var image        = new Bitmap(128 * n, 128 * n);

            terrainMap = new int[128 * n, 128 * n];
            for (int i = 0; i < image.Width; i++)
            {
                for (int j = 0; j < image.Height; j++)
                {
                    popMap[i, j] = popMap[i, j] - forestMap[i, j];
                }
            }
            var popLevels = FractalUtility.getCutOffLevels(popMap, new float[] { 98, 2 });

            for (int i = 0; i < image.Width; i++)
            {
                for (int j = 0; j < image.Height; j++)
                {
                    popMap[i, j] = popMap[i, j] - popLevels[0];
                    if (popMap[i, j] < 0)
                    {
                        popMap[i, j] = 0;
                    }
                    var pop = (popMap[i, j] / (popLevels[1] - popLevels[0]));
                    if (forestMap[i, j] > forestLevels[0])
                    {
                        terrainMap[i, j] = 1;
                        forestMap[i, j] -= forestLevels[0];
                        pop -= 1f;
                        var g = (int)(255 * forestMap[i, j] / (forestLevels[1] - forestLevels[0]));
                        image.SetPixel(i, j, Color.FromArgb(255 - g, 255, 255 - g));
                    }
                    else// if (pop >0)
                    {
                        terrainMap[i, j] = 2;
                        var b = (int)(255 * pop);
                        image.SetPixel(i, j, Color.FromArgb(255, 255 - b, 255 - b));
                    }

                    /*else
                     * {
                     *  image.SetPixel(i, j, Color.FromArgb(255, 255, 255));
                     * }*/
                    popMap[i, j] = pop;
                }
            }
            pictureBox.Image = image;
            return(getStartPoints(popMap));
        }
예제 #8
0
    // Use this for initialization
    void Start()
    {
        terrain = GetComponent <Terrain>();
        size    = terrain.terrainData.size;
        var forestOptions = new FractalOptions(2, 5);
        var townOptions   = new FractalOptions(2, 5);

        InitializeAlphamap();
        BuildFields();
        BuildRoads();
        BuildTerrainTextures(forestOptions);
        BuildTownTextures(townOptions);
        BuildTrees();
        BuildHedges();
        terrain.terrainData.SetAlphamaps(0, 0, alphaMap);
        terrain.Flush();

        TerrainData.initialize(alphaMap);
        RoadNetwork.BuildNetwork();
    }
예제 #9
0
        public async Task CanCreateFullFractal()
        {
            var options = new FractalOptions
            {
                Expression    = "z^3+1",
                PixelSize     = new PixelSize(400, 400),
                DomainSize    = new DomainSize(-5, 5, -5, 5),
                MaxIterations = 50,
                Precision     = 1e-5,
                FillColor     = new Hsv(0, 0, 0),
                Depth         = 15,
                Gradient      = 0.25f,
            };

            var generator = new LocalFractalGenerator(options);
            var result    = await generator.GenerateAsync();

            var image = ImageUtils.GenerateImage(result.Contents);
            var path  = Path.Combine(Directory.GetCurrentDirectory(), "Fractal.png");

            image.Save(path);
            TestContext.AddTestAttachment(path, "Fractal image generated");
        }
예제 #10
0
    //done

    private static int[,] GetMapFromFractal(FractalOptions options, float[] cut)
    {
        var fractalMap = Fractal.fractal(fractalSize, fractalSize, options);
        var map        = new int[fractalSize, fractalSize];
        var cutoff     = Fractal.getCutOffLevels(fractalMap, cut);

        for (var i = 0; i < fractalSize; i++)
        {
            for (var j = 0; j < fractalSize; j++)
            {
                /**/
                if (fractalMap[i, j] < cutoff[0])
                {
                    map[i, j] = 0;
                }
                else
                {
                    map[i, j] = 1;
                }
            }
        }
        return(map);
    }
예제 #11
0
 public AzureGenerator(FractalOptions options, IDurableOrchestrationContext context, int chunks = 8) : base(options)
 {
     _context = context;
     _chunks  = chunks;
 }
예제 #12
0
    public static float[,] fractal(int n, int m, FractalOptions options)
    {
        int size = Mathf.FloorToInt(Mathf.Max(n, m));

        int width = nextPowerOf2(size);

        int height = nextPowerOf2(size);


        float[,] map = new float[width + 1, height + 1];
        for (var i = 0; i < width + 1; i++)
        {
            for (var j = 0; j < height + 1; j++)
            {
                map[i, j] = Single.NaN;
            }
        }


        int generation = 0;



        int   xLength = width >> 1;
        int   yLength = height >> 1;
        float h       = options.getScale(xLength);

        //System.UnityEngine.Random UnityEngine.RandomNumber = new System.UnityEngine.Random();

        map[0, 0]          = h * (getRandomNumber() - 0.5f);
        map[0, height]     = h * (getRandomNumber() - 0.5f);
        map[width, 0]      = h * (getRandomNumber() - 0.5f);
        map[width, height] = h * (getRandomNumber() - 0.5f);

        while (xLength > 0)
        {
            //Debug.Log(xLength);
            //diamond phase
            //float lengthScale = xLength;//xLength*Terrain.activeTerrain.terrainData.heightmapScale.x;
            h = options.getScale(xLength);
            int x = xLength;
            while (x < width)
            {
                int y = yLength;
                while (y < height)
                {
                    if (Single.IsNaN(map[x, y]))
                    {
                        map[x, y] = (map[x - xLength, y - yLength] +
                                     map[x + xLength, y - yLength] +
                                     map[x + xLength, y + yLength] +
                                     map[x - xLength, y + yLength]) / 4f +
                                    h * (getRandomNumber() - 0.5f);
                    }
                    y += 2 * yLength;
                }
                x += 2 * xLength;
            }
            //h =getScaleFactor(generation+0.5f);
            //square phase
            x = 0;
            while (x <= width)
            {
                int y = 0;
                while (y <= height)
                {
                    if (Single.IsNaN(map[x, y]))
                    {
                        if (y == 0)
                        {
                            map[x, y] = (map[x, y + yLength] +
                                         map[x + xLength, y] +
                                         map[x - xLength, y]) / 3f +
                                        h * (getRandomNumber() - 0.5f);
                        }
                        else if (y == height)
                        {
                            map[x, y] = (map[x, y - yLength] +
                                         map[x + xLength, y] +
                                         map[x - xLength, y]) / 3f +
                                        h * (getRandomNumber() - 0.5f);
                        }
                        else if (x == 0)
                        {
                            map[x, y] = (map[x, y - yLength] +
                                         map[x, y + yLength] +
                                         map[x + xLength, y]) / 3f +
                                        h * (getRandomNumber() - 0.5f);
                        }
                        else if (x == width)
                        {
                            map[x, y] = (map[x, y - yLength] +
                                         map[x, y + yLength] +
                                         map[x - xLength, y]) / 3f +
                                        h * (getRandomNumber() - 0.5f);
                        }
                        else
                        {
                            /*if(Single.IsNaN(map[x][ y - yLength])){
                             *      trace(x+", "+(y - yLength)+" has not been assigned");
                             * }
                             * if(Math.isNaN(map[x][ y + yLength])){
                             *      trace(x+", "+(y + yLength)+" has not been assigned");
                             * }
                             * if(Math.isNaN(map[x + xLength][ y])){
                             *      trace("a");
                             * }
                             * if(Math.isNaN(map[x - xLength][ y])){
                             *      trace("b");
                             * }*/


                            map[x, y] = (map[x, y - yLength] +
                                         map[x, y + yLength] +
                                         map[x + xLength, y] +
                                         map[x - xLength, y]) / 4f +
                                        h * (getRandomNumber() - 0.5f);
                        }
                    }
                    y += yLength;
                }
                x += xLength;
            }
            xLength     = xLength >> 1;
            yLength     = yLength >> 1;
            generation += 1;
        }
        var outMap = new float[n, m];

        for (var i = 0; i < n; i++)
        {
            for (var j = 0; j < n; j++)
            {
                outMap[i, j] = map[i, j];
            }
        }
        //Fractal.printToFile(map, "map.m");
        return(outMap);
    }
예제 #13
0
 void BuildTownTextures(FractalOptions options)
 {
     int[,] fracMap = GetMapFromFractal(options, new float[] { 10, 1 });
     ApplyTextureFromFracMap(fracMap, TerrainType.Town);
 }
 public LocalFractalGenerator(FractalOptions options, int?processCount = null) : base(options)
 {
     ProcessCount = processCount ?? Environment.ProcessorCount;
 }
예제 #15
0
        private void button1_Click(object sender, EventArgs e)
        {
            int n = 256;
            int w = 11;
            int h = 4;

            if (smallScaleBox.Text.Length == 0 || largeScaleBox.Text.Length == 0)
            {
                return;
            }
            FractalOptions options = new FractalOptions(parseInt(smallScaleBox), parseInt(largeScaleBox), parseInt(minScaleBox));
            Bitmap         image   = new Bitmap(w * n + 1, h * n + 1);

            WrappingFractal.Initialize(parseInt(seedBox));
            float[,] map = WrappingFractal.fractal(n, h, w, options, FractalWrapMode.Horizontal);
            if (shaveBox.Checked)
            {
                WrappingFractal.shaveEdges(ref map);
            }
            float      ocean  = oceanBar.Value;
            var        levels = FractalUtility.getCutOffLevels(map, new float[] { ocean, oceanBar.Maximum - ocean });
            BitmapData data   = image.LockBits(new Rectangle(0, 0, map.GetLength(0), map.GetLength(1)), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

            int[] bitmapColors = new int[map.GetLength(0) * map.GetLength(1)];
            for (int i = 0; i < map.GetLength(0); i++)
            {
                for (int j = 0; j < map.GetLength(1); j++)
                {
                    //Random r = new Random(seed);
                    Color c;
                    if (map[i, j] < levels[0])
                    {
                        c = Color.Blue;
                    }
                    else
                    {
                        c = Color.Green;
                    }

                    int x = i;
                    if (i < map.GetLength(0) / 2)
                    {
                        x += map.GetLength(0) / 2;
                    }
                    else
                    {
                        x -= map.GetLength(0) / 2;
                    }
                    int y = j;
                    //if (j < map.GetLength(1) / 2) y += map.GetLength(1) / 2;
                    //else y -= map.GetLength(1) / 2;

                    bitmapColors[i + j * map.GetLength(0)] = c.ToArgb();
                    //image.SetPixel(i, j, c);
                }
            }
            System.Runtime.InteropServices.Marshal.Copy(bitmapColors, 0, data.Scan0, map.GetLength(0) * map.GetLength(1));
            image.UnlockBits(data);
            image.Save("fractal.bmp");
            setImage(image);
        }
예제 #16
0
        public void OnGenerateMapExecute()
        {
            if (seed <= 0)
            {
                var seedGeneration = new Random();
                Seed = seedGeneration.Next().ToString();
            }
            Random r = new Random(seed);

            var townOptions   = new FractalOptions(townSmallScale, townLargeScale);
            int n             = blocks;
            var popMap        = WrappingFractal.fractal(128, n, n, townOptions, FractalWrapMode.NoWrap, (uint)r.Next());
            var forestOptions = new FractalOptions(forestSmallScale, forestLargeScale);
            var forestMap     = WrappingFractal.fractal(128, n, n, forestOptions, FractalWrapMode.NoWrap, (uint)r.Next());;
            var forestLevels  = FractalUtility.getCutOffLevels(forestMap, new float[] { 100 - forestProportion, forestProportion });
            var bitmap        = new Bitmap(128 * n, 128 * n);
            var terrainMap    = new int[128 * n, 128 * n];

            for (int i = 0; i < bitmap.Width; i++)
            {
                for (int j = 0; j < bitmap.Height; j++)
                {
                    popMap[i, j] = popMap[i, j] - forestMap[i, j];
                }
            }
            var popLevels = FractalUtility.getCutOffLevels(popMap, new float[] { (100 - (forestProportion + townProportion)), townProportion });

            for (int i = 0; i < bitmap.Width; i++)
            {
                for (int j = 0; j < bitmap.Height; j++)
                {
                    popMap[i, j] = popMap[i, j] - popLevels[0];
                    if (popMap[i, j] < 0)
                    {
                        popMap[i, j] = 0;
                    }
                    var pop = (popMap[i, j] / (popLevels[1] - popLevels[0]));
                    if (forestMap[i, j] > forestLevels[0])
                    {
                        terrainMap[i, j] = 2;

                        /*forestMap[i, j] -= forestLevels[0];
                         * pop -= 1f;
                         * var g = (int)(255 * forestMap[i, j] / (forestLevels[1] - forestLevels[0]));
                         * bitmap.SetPixel(i, j, Color.FromArgb(0, 255, 0));*/
                    }
                    else if (pop > 0)
                    {
                        terrainMap[i, j] = 3;

                        /*var b = (int)(255 * pop);
                         * bitmap.SetPixel(i, j, Color.FromArgb(255, 0, 0));
                         */
                    }
                    else
                    {
                        terrainMap[i, j] = 1;
                        //bitmap.SetPixel(i, j, Color.FromArgb(255, 255, 255));
                    }
                    //popMap[i, j] = pop;
                }
            }
            var popCentres = Sanitize(ref terrainMap);

            MapData  = new MapDataPacket(terrainMap, popCentres);
            TownData = null;
        }