Mandelbrot 集函数崩溃

Mandelbrot Set function crashes

我为 Mandelbrot 集函数做了一个模拟,zn + 1 = power(zn) + c 它可以工作,但是当我说到函数不稳定时它会崩溃,现在我有一个布尔值,当它为真时会制作一条连接所有圆圈的电线,当它为假时它很好(dosent crash)但是当它打开时它会,代码是这样工作的:

开始: 建立一个圆的列表并通过等式使其成为 pos,然后在圆和最后一个圆之间制作一条线, 更新: 然后当你移动圆圈时,它会使用已经创建的 gameobj 列表来更新那里的位置。

你可以在这里试试: build github: git

但它崩溃了:(, 这是代码:

 private void updateCircles()
{
    StartUpdateCircles();
}


private void StartCircles()
{
    float x = BlackCircle.anchoredPosition.x;
    float y = BlackCircle.anchoredPosition.y;
    AllCircles.Add(BlackCircle.gameObject);

    for (int i = 1; i < itarations; i++)
    {
        Vector2 RedCircleVec2 = RedCircle.anchoredPosition;
        Vector2 LastCircleVec2 = AllCircles[i - 1].GetComponent<RectTransform>().anchoredPosition;
        GameObject Circle = Instantiate(BlackCircle.gameObject, Vector3.zero, Quaternion.identity);

        Circle.transform.SetParent(CanvasPerent);
        AllCircles.Add(Circle);

        x = Mathf.Pow(x, 2);
        x -= Mathf.Pow(LastCircleVec2.y, 2);
        x += RedCircleVec2.x;

        y = (2 * LastCircleVec2.x
            * LastCircleVec2.y) + RedCircleVec2.y;

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

        if (HasWire)
        {
             GameObject wire = GenrateWireStart(LastCircleVec2
           , Circle.GetComponent<RectTransform>().anchoredPosition);

            AllWires.Add(wire);
        }

    }

}

private void StartUpdateCircles()
{
    float x = BlackCircle.anchoredPosition.x;
    float y = BlackCircle.anchoredPosition.y;
    for (int i = 1; i < itarations; i++)
    {
        Vector2 RedCircleVec2 = RedCircle.anchoredPosition;
        Vector2 LastCircleVec2 = AllCircles[i - 1].GetComponent<RectTransform>().anchoredPosition;
        RectTransform ICircle = AllCircles[i].GetComponent<RectTransform>();


        x = Mathf.Pow(x, 2);
        x -= Mathf.Pow(LastCircleVec2.y, 2);
        x += RedCircleVec2.x;

        y = (2 * LastCircleVec2.x
            * LastCircleVec2.y) + RedCircleVec2.y;

        ICircle.anchoredPosition = new Vector2(x, y);
        if (HasWire)
        {
            
              GenrateWireUpdate(LastCircleVec2
                ,ICircle.anchoredPosition, i);
            
            
        }
    }
}

public GameObject GenrateWireStart(Vector2 NodeA, Vector2 NodeB)
{
    GameObject Connector = new GameObject("connector", typeof(Image));
    Connector.transform.SetParent(CanvasPerent);
    RectTransform ConnectorRT = Connector.GetComponent<RectTransform>();

    ConnectorRT.anchorMin = new Vector2(0, 0);
    ConnectorRT.anchorMax = new Vector2(0, 0);

    Connector.GetComponent<Image>().color = new Color(0f, 0f, 0f, 0.25f);

    Vector2 dir = (NodeB - NodeA).normalized;
    float distance = Vector2.Distance(NodeA, NodeB);

    ConnectorRT.sizeDelta = new Vector2(distance, 0.005f);

    ConnectorRT.position = NodeA + dir * distance * .5f;

    ConnectorRT.localEulerAngles = new Vector3(0, 0, UtilsClass.GetAngleFromVectorFloat(dir));

    return Connector;
}

public void GenrateWireUpdate(Vector2 NodeA, Vector2 NodeB, int i)
{
    RectTransform ConnectorRT = AllWires[i - 1].GetComponent<RectTransform>();
    Vector2 dir = (NodeB - NodeA).normalized;
    float distance = Vector2.Distance(NodeA, NodeB);
    ConnectorRT.sizeDelta = new Vector2(distance, 0.005f);
    ConnectorRT.position = NodeA + dir * distance * .5f;
    ConnectorRT.localEulerAngles = new Vector3(0, 0, UtilsClass.GetAngleFromVectorFloat(dir));
}

请帮忙,谢谢。

我简单地查看了您的代码,您似乎从计算中得到了一些无效的位置,例如 infinite / undefined,或者只是一些位置对于 Unity 来说太远了。

我可以通过简单地将位置限制为例如

来删除这些
 x = Mathf.Clamp(Mathf.Pow(x, 2), -Screen.width, Screen.width);
 x = Mathf.Clamp(x - Mathf.Pow(LastCircleVec2.y, 2), -Screen.width, Screen.width);
 x = Mathf.Clamp(x + RedCircleVec2.x, -Screen.width, Screen.width);

 y = Mathf.Clamp((2 * LastCircleVec2.x * LastCircleVec2.y) + RedCircleVec2.y, -Screen.width, Screen.width);

这只是将所有位置限制在一些 off-screen 最大位置