1

I'm trying to make the function of the Mandelbrot Set, and I'm not sure what I'm doing wrong or right, here's the code:

private void StartCircles()
{
    float savePower = BlackCircle.anchoredPosition.x;
    GameObject[] AllCircles = new GameObject[itarations];
    AllCircles[0] = BlackCircle.gameObject;
    for (int i = 1; i < itarations; i++)
    {
        GameObject Circle = Instantiate(BlackCircle.gameObject, Vector3.zero, Quaternion.identity);
        Circle.transform.SetParent(CanvasPerent);
        savePower = Mathf.Pow(savePower, 2);
        savePower += RedCircle.anchoredPosition.x;
        Circle.GetComponent<RectTransform>().anchoredPosition = new Vector2(savePower,
            AllCircles[i - 1].GetComponent<RectTransform>().anchoredPosition.y * -1);
        AllCircles[i] = Circle;
    }
    CleanSqud = new GameObject[itarations];
    CleanSqud = AllCircles;
}

I'm not sure what the y position should be and how could the x position be < 0 if it's a power of 2, it's automaticly > 0.

Here's the display:

only 2 circles

more then 1

  • 4
    The x position of a power of 2 can be < 0 if the number being squared is a [complex number](https://en.wikipedia.org/wiki/Complex_number): the x value is the real component, and the y position is the imaginary component. An understanding of how to multiply and add complex numbers and the geometrical effects of these operations is fundamental to rendering the Mandelbrot Set. – samgak Feb 25 '22 at 08:35
  • 1
    exactly I do not see any complex domain and also no ending condition based on escape radius ... why to heck are you using transformations, circles and rectangles inside single pixel iteration loop ??? That will be Sloooooooow and also it does not make any sense. See this [simple Mandelbort](https://stackoverflow.com/a/53666890/2521214) (pay attention to the `for` loop in the fragment shader) and once working you can move to [more advanced](https://stackoverflow.com/a/56197067/2521214) one – Spektre Feb 25 '22 at 09:02
  • 1
    to be clear that for loop will compute number of iterations per single pixel so you need to do this for every pixel of your image ... PS if you are not familiar with complex domain here you can find how to compute [basic operations](https://stackoverflow.com/a/26355569/2521214) using real domain. And here something more exotic [tetration fractal](https://stackoverflow.com/a/56735614/2521214) – Spektre Feb 25 '22 at 09:06
  • thanks, i will learn about complex nums – bill the computer wiz Feb 25 '22 at 09:58
  • Note that `2**n` is a power of 2, but `n**2` *is not*. The later is phrased "*to the power of two*" (`n**2`) and not "*a power of two*" (`2**n`). The language difference is slight, but significant because the difference in meaning is critical. – RBarryYoung Feb 25 '22 at 16:08
  • Hi, thanks for the help, how do i display complex numbers in unity? i know how to calculate them but not how to display them as a value in the recttransform – bill the computer wiz Feb 26 '22 at 16:28
  • Is this unity3D? If so please add appropriate tags. – John Alexiou Feb 27 '22 at 22:10

1 Answers1

0

i manged to get my code working after some time and i got some answars to share if anyone has my problen:

well i only wanted to make the function of the zn + 1 = zn * zn + c i dident made the full set only this function, heres my code:

    #region Actions
private void OnDestroy()
{
    MoveBlack.HasMoved -= HasMoved;
    MoveBlack.HasStoped -= HasStoped;

    MoveRed.HasMoved -= HasMoved;
    MoveRed.HasStoped -= HasStoped;
}
private void LateUpdate()
{
    if (moved) { updateCircles(); }
    if (hasparty)
    {
        foreach(GameObject game in CleanSqud)
        {
            game.GetComponent<Image>().color = new Color(Random.Range(0f, 1f), Random.Range(0f, 1f), Random.Range(0f, 1f));
        }
    }
}
private void HasMoved()
{
    moved = true;
}

private void HasStoped()
{
    moved = false;
}


#endregion

#region Updateing
private void updateCircles()
{
    foreach (GameObject Circle in CleanSqud) { if (Circle.gameObject.name != "BlackCirlce") { Destroy(Circle); } }
    StartCircles();
}


private void StartCircles()
{


    float x = BlackCircle.anchoredPosition.x;
    float y = BlackCircle.anchoredPosition.y;
    GameObject[] AllCircles = new GameObject[itarations];
    AllCircles[0] = BlackCircle.gameObject;
    for (int i = 1; i < itarations; i++)
    {

        GameObject Circle = Instantiate(BlackCircle.gameObject, Vector3.zero, Quaternion.identity);
        Circle.transform.SetParent(CanvasPerent);
        AllCircles[i] = Circle;

        x = Mathf.Pow(x, 2);
        x -= Mathf.Pow(AllCircles[i - 1].GetComponent<RectTransform>().anchoredPosition.y, 2);
        x += RedCircle.anchoredPosition.x;

        y = (2 * AllCircles[i - 1].GetComponent<RectTransform>().anchoredPosition.x
            * AllCircles[i - 1].GetComponent<RectTransform>().anchoredPosition.y) + RedCircle.anchoredPosition.y;

        Circle.GetComponent<RectTransform>().anchoredPosition = new Vector2(x, y);





    }
    CleanSqud = new GameObject[itarations];
    CleanSqud = AllCircles;
}
#endregion

so what you should do is instad of showing the y as a imaginary and the x as real show it using the equastion: this x = power of the old x - power of the old y + c.x this y = 2 * the old x * the old y + c.y

this should work! thanks.

Working