首页 > 解决方案 > 平衡三个变量之间的数字与偏差

问题描述

我也有一个问题,我也正在发布解决方案。我有三个变量 A、B 和 C。我需要做的是基于第四个数字(输入)平衡三个变量之间的值 100 和偏差(我认为这是正确的词)

因此,一开始 A 为 100,B 和 C 为 0。然后随着 Input 的增加,A 将减少,B 将增加相同的值(因此 A + B + C 始终等于 100)。然后当 B 达到 100 时,增加的输入会增加 C,同时减少 B。反过来也是如此,因此减少输入会使值从 C > B > A 移动。

我希望我已经解释得足够好。

编辑:到目前为止我的尝试是这样的,在这种情况下,A、B 和 C 是 vCam.m_weight0、vCam.m_weight1、vCam.m_weight2

我的问题是我在切换 activeCam 时绊倒了,所以下一个立即跳到 100。我相信这是糟糕的代码,但我正在努力想另一种方式。

//Input is clamped between 0 - 100
private void HandleCameraSwitching(float newWeight)
{
    if (activeCam == 0)
    {
        vCam.m_Weight1 = newWeight;
        vCam.m_Weight0 = 100 - newWeight;
    }
    else if (activeCam == 1)
    {
        vCam.m_Weight2 = newWeight;
        vCam.m_Weight1 = 100 - newWeight;
    }
    else if (activeCam == 2)
    {
        vCam.m_Weight2 = newWeight;
        vCam.m_Weight1 = 100 - newWeight;

    }

    if (vCam.m_Weight0 == 100)
    { activeCam = 0; }
    else if (vCam.m_Weight1 == 100)
    {
        if (zoomingIn)
        {
            activeCam = 1;
        } else
        {
            activeCam = 0;
        }
    }
    else if(vCam.m_Weight2 == 100)
    {
        if (zoomingIn)
        {
            activeCam = 2;
        }
        else
        {
            activeCam = 1;
        }
    }
}

标签: c#

解决方案


public class Camera
{
    public Camera()
    {
        zoomingIn = true;
        m_Weight0 = 100;
        m_Weight1 = 0;
        m_Weight2 = 0;
    }
    public bool zoomingIn { get; set; }
    public float m_Weight0 { get; set; }
    public float m_Weight1 { get; set; }
    public float m_Weight2 { get; set; }

    public void HandleCameraSwitching(float newWeight)
    {
        if (m_Weight0 == 100) zoomingIn = true;
        else if (m_Weight2 == 100) zoomingIn = false;

        if (zoomingIn)
        {
            if (m_Weight0 == 100 || (m_Weight0 > 0 && m_Weight1 > 0))
            {
                m_Weight0 = 100 - newWeight;
                m_Weight1 = newWeight;
            }
            else if (m_Weight1 == 100 || (m_Weight1 > 0 && m_Weight2 > 0))
            {
                m_Weight1 = 100 - newWeight;
                m_Weight2 = newWeight;
            }
        }
        else
        {
            if (m_Weight2 == 100 || (m_Weight2 > 0 && m_Weight1 > 0))
            {
                m_Weight2 = 100 - newWeight;
                m_Weight1 = newWeight;
            }
            else if (m_Weight1 == 100 || (m_Weight1 > 0 && m_Weight0 > 0))
            {
                m_Weight1 = 100 - newWeight;
                m_Weight0 = newWeight;
            }
        }
    }
}

bool zoomingIn当 A 或 C 等于 100 时发生变化


推荐阅读