c# - 平衡三个变量之间的数字与偏差
问题描述
我也有一个问题,我也正在发布解决方案。我有三个变量 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;
}
}
}
解决方案
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 时发生变化
推荐阅读
- php - 如何解决摘录内搜索结果中的重复项
- jsp - 当用户单击添加到购物车时,项目应存储在表格中
- reactjs - 使用 React 设计(真实房屋的)窗框
- javascript - Android 应用程序强制关闭,SQLite 数据库的东西
- javascript - onEachfeatures 不显示内容但 [object Object]
- raku - 带有自定义词法分析器的 Raku 语法
- javascript - 如何编辑图像 JavaScript 的加载优先级
- reactjs - 如何将条件道具传递给打字稿中未知类型的反应组件?
- c++ - PCL、SACS分割检测球
- asp.net-core - Asp.Net core 3.1 web 应用程序在运行时意外终止,没有任何异常