Search Unity

Class that handles complex numbers?

Discussion in 'Scripting' started by cst42, Dec 17, 2014.

  1. cst42

    cst42

    Joined:
    Mar 10, 2014
    Posts:
    12
    Hi, I searched the Unity docs for complex numbers and couldn't find any class that handled them. I wanted to build a mandelbrot set within unity using one of the many ready-made algorithms found online. Problem is they use complex numbers in their calculations.

    I tried importing the .Net Systems.Numeric namespace which has a complex numbers class but apparently this namespace is not compatible with Unity.

    Any help is appreciated.
     
    Last edited: Dec 17, 2014
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,528
    System.Numerics is .Net 4.0 and newer. Unity supports .Net 3.5 equivalent (mono).

    There are third party implementations of complex numbers out there. Don't search with Unity, just search with 'mono' or '.net' in mind.

    Such as this one:
    http://numerics.mathdotnet.com/
     
    cst42 likes this.
  3. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    You can make mandelbrot without complex numbers, just treat i as -1

    Code (CSharp):
    1.     Texture2D tex;
    2.     int texSize = 512;
    3.  
    4.     //to navigate the number space
    5.     Vector2 center;
    6.     float size;
    7.  
    8.     void UpdateTex () {
    9.         float xStep = size * 2 / texSize;
    10.         float yStep = size * 2 / texSize;
    11.  
    12.         int minIteration = int.MaxValue;
    13.         int maxInteration = 0;
    14.  
    15.         Color32[] cols = new Color32[texSize * texSize];
    16.  
    17.         for ( int y = 0; y < texSize; y++ ) {
    18.             double Y = center.y - size + y * yStep;
    19.             for ( int x = 0; x < texSize; x++ ) {
    20.                 double X = center.x - size + x * xStep;
    21.  
    22.                 double x0 = 0;
    23.                 double y0 = 0;
    24.  
    25.                 byte iteration = 0;
    26.                 int max_iteration = 255;
    27.                 while ( x0 * x0 + y0 * y0 < 4 && iteration < max_iteration ) {
    28.                     double xtemp = x0 * x0 - y0 * y0 + X;
    29.                     y0 = 2 * x0 * y0 + Y;
    30.                     x0 = xtemp;
    31.                     iteration++;
    32.                 }
    33.  
    34.                 if ( iteration < minIteration ) minIteration = iteration;
    35.                 if ( iteration > maxInteration ) maxInteration = iteration;
    36.  
    37.                 cols[y * texSize + x].r = iteration;
    38.             }
    39.         }
    40.  
    41.         float range = maxInteration - minIteration;
    42.  
    43.         //normalise and colorize
    44.         for ( int y = 0; y < texSize; y++ ) {
    45.             for ( int x = 0; x < texSize; x++ ) {
    46.  
    47.                 r = ( cols[y * texSize + x].r - minIteration ) / range;
    48.  
    49.                 //greyscale
    50.                 cols[y * texSize + x].r = (byte) ( r * 255 );
    51.                 cols[y * texSize + x].g = (byte) ( r * 255 );
    52.                 cols[y * texSize + x].b = (byte) ( r * 255 );
    53.  
    54.                 //rainbows
    55.                 //cols[y * texSize + x].r = (byte) ( Mathf.Sin( ( r + 0.5f ) * Mathf.PI ) * 255);
    56.                 //cols[y * texSize + x].g = (byte) ( Mathf.Sin( ( r ) * Mathf.PI ) * 255 );
    57.                 //cols[y * texSize + x].b = (byte) ( Mathf.Sin( ( r - 0.5f ) * Mathf.PI ) * 255 );
    58.             }
    59.         }
    60.         tex.SetPixels32( cols );
    61.         tex.Apply( false );
    62.     }
    63.  
     
    cst42 likes this.
  4. cst42

    cst42

    Joined:
    Mar 10, 2014
    Posts:
    12
    Thanks to both your replies. I will do both options.