如何进行计算

时间:2017-07-23 03:33:55

标签: c# math

我需要在C#中执行以下数学运算: Formula

我尝试使用类Matrix(System.Windows.Media),但我没有成功

我的代码:

    public double DegreeToRadian(double Degree)
    {
        return Degree * Math.PI / 180;
    }

    public double ToCotg(double Angle)
    {
        return (Angle < 0 ? -Math.PI / 2 : Math.PI / 2) - Math.Atan(Angle);
    }

    public List<double> YShear(double B, double X, double Y)
    {
        List<List<double>> Formula = new List<List<Double>>()
        {
            new List<double>(){ 1, 0, 0 },
            new List<double>(){ B, 1, 0 },
            new List<double>(){ 0, 0, 1 }
        };
        List<double> Multiplier = new List<double>() { X, Y, 1 };
        List<double> Result = new List<double>();

        foreach (List<Double> Matriz in Formula)
        {
            double Resultado = 0;
            for (int i = 0; i < Matriz.Count; i++)
            {
                double Value = Matriz[i];
                double Mutiply = Multiplier[i];
                Resultado += Value * Mutiply;
            }
            Result.Add(Resultado);
        }
        return Result;
    }


    public List<double> XShear(double A, double X, double Y)
    {
        List<List<double>> Formula = new List<List<Double>>()
        {
            new List<double>(){ 1, A, 0 },
            new List<double>(){ 0, 1, 0 },
            new List<double>(){ 0, 0, 1 }
        };
        List<double> Multiplier = new List<double>() { X, Y, 1 };
        List<double> Result = new List<double>();

        foreach (List<Double> Matriz in Formula)
        {
            double Resultado = 0;
            for (int i = 0; i < Matriz.Count; i++)
            {
                double Value = Matriz[i];
                double Mutiply = Multiplier[i];
                Resultado += Value * Mutiply;
            }
            Result.Add(Resultado);
        }
        return Result;
    }

代码测试:

        double Angle = 30f; // 30º
        double Radians = DegreeToRadian(Angle);
        double X = 30;
        double Y = 30;
        double Cotg = ToCotg(Radians);
        double ResultX = X + Cotg * Y; // (MBo code) Result: 30
        double ResultY = Y + Cotg * X; // (MBo code) Result: 30
        XShear(Cotg, X, Y); // 62,6534525908162, 30, 1
        YShear(Cotg, X, Y); // 62,6534525908162, 30, 1

预期结果: XShear,30º=(~46,30,1),YShear,30º=(30,~46)

1 个答案:

答案 0 :(得分:2)

1)函数ToCotg期望以弧度为单位的参数(它使用Math.PI常量和Math.Atan函数),而用30度值调用它。

使用ToRadians之类的功能(如果可用)或仅将度数乘以Math.PI / 180

2)函数ToCotg错误 - 从角度减去atan值。正确的伪代码:

if Angle = 0 or Angle = Math.Pi  // if Sin(Angle) = 0
  return Infinity  //extra case
else
  return 1.0 / Math.Tan(Angle)

3)Y_Shear使用什么角度?如果OX和倾斜边缘之间为30度,则B = Tan(30)

4)为什么不使用像

这样的简单公式
Result.X = X + A * Y
Result.Y = Y

Result.X = X
Result.Y = Y + B * X

用于XShear和YShear以避免过度计算(在任何情况下你都不利用像vector *矩阵乘法这样的通用子程序)

图片中绘制的theta的纸笔计算:

 Cotan(30) = 1.73
 X after XShear = 30 +  1.73 * 30 = 81.96