Mandelbrot Set 函数崩溃

发布于 2025-01-10 23:12:05 字数 3900 浏览 1 评论 0原文

我为 Mandelbrot Set 函数做了一个 sim,zn + 1 = power(zn) + c 它可以工作,但是当我达到功能不稳定的地步时,它会崩溃,现在我有一个布尔值,当 true 时,它​​会连接所有圆圈,当它为 false 时,它​​很好(不崩溃),但当它打开时,它会崩溃,代码的工作原理如下:

开始: 建立一个圆列表,并通过方程确定 pos,然后在圆和最后一个圆之间拉一条线, 更新: 然后,当您移动圆圈时,它会使用已经创建的 gameobj 列表来更新那里的位置。

你可以在这里尝试一下: 构建 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));
}

请帮助,谢谢.

项目

i made a sim for the Mandelbrot Set function, zn + 1 = power(zn) + c
and it work but when i get to the point were the function is unstable it crashes, now i have a boolen that when true makes a wire that connects all the circles, when its false its fine(dosent crash) but when its on it does, the code works like this:

start:
building a list of circles and making there pos by the equation, and then crating a wire between the circle and the last circle,
update:
then when you move the circle it uses the already made list of gameobj to update there pos.

you can try it here:
build
github:
git

but it crashes:(, heres the code:

 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));
}

pls help, thank you.

the project

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤君无依 2025-01-17 23:12:05

我简单地查看了你的代码,你似乎从你的计算中得到了一些无效的位置,比如无限/未定义的位置,或者只是一些对于 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);

将所有位置限制为某些屏幕外最大位置来删除这些

I looked briefly into your code and you seem to get some invalid positions like infinite / undefined from your calculations or just some positions too far away for Unity.

I could remove these by simply limiting positions to e.g.

 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);

which simply limits all positions to some off-screen max positions

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文