Exemplo n.º 1
0
        /// <summary>Starts calculating the mandelnumbers for the given area of the mandelbrot set.</summary>
        /// <param name="args">The area to calculate</param>
        public void Calculate(MandelAreaArgs args)
        {
            lock(locker)
            {
                // Abort any old workers
                Abort();

                // Make sure the x axis lies in the middel of a pixel
                args.CenterOnXAxis();

                // Create a new worker
                worker = new Worker(args);
                worker.OnWorkFinished = WorkFinished;

                // Determine which indexes should be calculated and which can be determined using the symmetry of the mandelbrot set
                int startIndex = 0;
                int endIndex = args.CalcArea.Width * args.CalcArea.Height;
                if(args.CalcAreaTop() > 0.0 && args.CalcAreaBottom() < 0.0)
                {
                    if(args.CalcAreaTop() >= -args.CalcAreaBottom())
                    {
                        // `endIndex` = `rows of pixels above the x-axis including the x-axis` * `width in pixels`
                        endIndex = (int) Math.Min(endIndex, Math.Round((args.CalcAreaTop() / args.Scale + 1) * args.CalcArea.Width));
                    }
                    else
                    {
                        // `startIndex` = `rows of pixels above the x-axis excluding the x-axis` * `width in pixels`
                        startIndex = (int) Math.Round((args.CalcAreaTop() / args.Scale) * args.CalcArea.Width);
                    }
                }

                // Queue the work items
                workFinished = new Dictionary<int, bool>((int) Math.Ceiling(((double) endIndex - startIndex) / WORK_PER_THREAD));
                for(int i = startIndex; i < endIndex; i += WORK_PER_THREAD)
                {
                    workFinished[i] = false;
                    ThreadPool.QueueUserWorkItem(new WaitCallback(worker.Work), new Worker.WorkerArgs(i, Math.Min(i + WORK_PER_THREAD, endIndex)));
                }
            }
        }