将两个符号都为的力相加

本文关键字:符号 两个 | 更新日期: 2023-09-27 18:20:20

我正在尝试制作一个很好的算法来求和力对。这里的主要问题是,你可以用另一个符号来表示力,这意味着力在任何时候都可以是+或-。

例如:F1=±100 kN,F2=200 kN-->最大力=+100+200=300 kN,最小力=-100+200=100 kN。

我已经做了一个简单的算法,它结合了所有的可能性,但我要求更好的东西。作为我的方法的输出,我有:

public List<Force> SumForces(Force firstForce, Force secondForce)
    {
        Force maxForce = new Force();
        Force minForce = new Force();
        // All possible sumatuons
        double sumCaseFirst = firstForce.ForceValue + secondForce.ForceValue;
        double sumCaseSecond = firstForce.ForceValue - secondForce.ForceValue;
        double sumCaseThird = -firstForce.ForceValue + secondForce.ForceValue;
        double sumCaseFourth = -firstForce.ForceValue - secondForce.ForceValue;
        // Calculating all posible sumations
        if (firstForce.Sign == ForceSign.Alter && secondForce.Sign == ForceSign.Alter)
        {
            maxForce.ForceValue = sumCaseFirst;
            minForce.ForceValue = sumCaseFourth;
        }
        else if (firstForce.Sign == ForceSign.Alter && secondForce.Sign == ForceSign.Plus)
        {
            maxForce.ForceValue = sumCaseFirst;
            minForce.ForceValue = sumCaseThird;
        }
        else if (firstForce.Sign == ForceSign.Alter && secondForce.Sign == ForceSign.Minus)
        {
            maxForce.ForceValue = sumCaseSecond;
            minForce.ForceValue = sumCaseFourth;
        }
        else if (firstForce.Sign == ForceSign.Plus && secondForce.Sign == ForceSign.Alter)
        {
            maxForce.ForceValue = sumCaseFirst;
            minForce.ForceValue = sumCaseSecond;
        }
        else if (firstForce.Sign == ForceSign.Plus && secondForce.Sign == ForceSign.Plus)
        {
            maxForce.ForceValue = sumCaseFirst;
            minForce.ForceValue = 0;
        }
        else if (firstForce.Sign == ForceSign.Plus && secondForce.Sign == ForceSign.Minus)
        {
            maxForce.ForceValue = sumCaseSecond;
            minForce.ForceValue = 0;
        }
        else if (firstForce.Sign == ForceSign.Minus && secondForce.Sign == ForceSign.Alter)
        {
            maxForce.ForceValue = sumCaseThird;
            minForce.ForceValue = sumCaseFourth;
        }
        else if (firstForce.Sign == ForceSign.Minus && secondForce.Sign == ForceSign.Plus)
        {
            maxForce.ForceValue = sumCaseThird;
            minForce.ForceValue = 0;
        }
        else
        {
            maxForce.ForceValue = 0;
            minForce.ForceValue = sumCaseFourth;
        }
        // Ensure that true maximum force value is at index 0
        if (maxForce.ForceValue > minForce.ForceValue)
        {
            Sum.Add(maxForce);
            Sum.Add(minForce);
        }
        else
        {
            Sum.Add(minForce);
            Sum.Add(maxForce);
        }

        return Sum;
    }

将两个符号都为的力相加

最大值总是在添加正值时

double maxValue = Math.Abs(firstForce.ForceValue) + Math.Abs(secondForce.ForceValue);

最小值总是在添加负值时

double minValue = -Math.Abs(firstForce.ForceValue) - Math.Abs(secondForce.ForceValue);

没有必要考虑积极和消极的组合。