Exemplo n.º 1
0
        /// <summary>
        ///     The universe rendered to an image.
        /// </summary>
        public void Visualize(WriteableBitmap bitmap)
        {
            int width  = _universe.Width;
            int height = _universe.Height;

            // I am usually against unsafe code, but this seems to be the
            // only way in XAML to render bit-by-bit and get any degree of
            // speed.  If anyone knows a better way (that is still reasonably fast),
            // let me know. --JHeaton.
            bitmap.Lock();
            unsafe
            {
                for (int row = 0; row < height; row++)
                {
                    for (int col = 0; col < width; col++)
                    {
                        double dr = (_universe.Get(row, col).Data[0] + 1.0) / 2.0;
                        double dg = (_universe.Get(row, col).Data[1] + 1.0) / 2.0;
                        double db = (_universe.Get(row, col).Data[2] + 1.0) / 2.0;

                        // Compute the pixel's color.
                        int colorData = ((byte)(dr * 255.0)) << 16; // R
                        colorData |= ((byte)(dg * 255.0)) << 8;     // G
                        colorData |= ((byte)(db * 255.0)) << 0;     // B


                        for (int y = 0; y < _zoom; y++)
                        {
                            for (int x = 0; x < _zoom; x++)
                            {
                                // Get a pointer to the back buffer.
                                var pBackBuffer = (int)bitmap.BackBuffer;

                                // Find the address of the pixel to draw.
                                pBackBuffer += (row * _zoom + y)
                                               * bitmap.BackBufferStride
                                               + (col * _zoom + x) * 4;
                                // Assign the color data to the pixel.
                                *((int *)pBackBuffer) = colorData;
                            }
                        }
                    }
                }
            }

            // Specify the area of the bitmap that changed.
            bitmap.AddDirtyRect(new Int32Rect(0, 0, width * _zoom, height * _zoom));
            bitmap.Unlock();
        }
Exemplo n.º 2
0
 /// <summary>
 /// Copy another universe into this one.
 /// </summary>
 /// <param name="source">The source universe.</param>
 public void Copy(UniverseHolder source)
 {
     for (int row = 0; row < Height; row++)
     {
         for (int col = 0; col < Width; col++)
         {
             for (int i = 0; i < _cellSize; i++)
             {
                 _data[row][col].Data[i] = source.Get(row, col).Data[i];
             }
         }
     }
 }
Exemplo n.º 3
0
        /// <summary>
        /// Compare this universe to another and return the difference.  A value of zero indicates an identical universe.
        /// The lower the value, the more similar.
        /// </summary>
        /// <param name="otherUniverse">The other universe.</param>
        /// <returns>The difference between the universes.</returns>
        public double Compare(UniverseHolder otherUniverse)
        {
            int result = 0;
            int total  = 0;

            for (int row = 0; row < otherUniverse.Height; row++)
            {
                for (int col = 0; col < otherUniverse.Width; col++)
                {
                    int d1 = Math.Abs((int)(255 * Get(row, col).Avg));
                    int d2 = Math.Abs((int)(255 * otherUniverse.Get(row, col).Avg));
                    if (Math.Abs(d1 - d2) > 10)
                    {
                        result++;
                    }
                    total++;
                }
            }

            return(result / (double)total);
        }
Exemplo n.º 4
0
 /// <summary>
 /// Copy another universe into this one.
 /// </summary>
 /// <param name="source">The source universe.</param>
 public void Copy(UniverseHolder source)
 {
     for (int row = 0; row < Height; row++)
     {
         for (int col = 0; col < Width; col++)
         {
             for (int i = 0; i < _cellSize; i++)
             {
                 _data[row][col].Data[i] = source.Get(row, col).Data[i];
             }
         }
     }
 }
Exemplo n.º 5
0
        /// <summary>
        /// Compare this universe to another and return the difference.  A value of zero indicates an identical universe.
        /// The lower the value, the more similar.
        /// </summary>
        /// <param name="otherUniverse">The other universe.</param>
        /// <returns>The difference between the universes.</returns>
        public double Compare(UniverseHolder otherUniverse)
        {
            int result = 0;
            int total = 0;
            for (int row = 0; row < otherUniverse.Height; row++)
            {
                for (int col = 0; col < otherUniverse.Width; col++)
                {
                    int d1 = Math.Abs((int) (255*Get(row, col).Avg));
                    int d2 = Math.Abs((int) (255*otherUniverse.Get(row, col).Avg));
                    if (Math.Abs(d1 - d2) > 10)
                    {
                        result++;
                    }
                    total++;
                }
            }

            return result/(double) total;
        }