Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#太阳系,行星轨道数学问题_C#_Animation_Matrix_Graphics_Simulation - Fatal编程技术网

C#太阳系,行星轨道数学问题

C#太阳系,行星轨道数学问题,c#,animation,matrix,graphics,simulation,C#,Animation,Matrix,Graphics,Simulation,我正在写一个太阳系模拟程序;我只是一个C#的初学者 我正在自定义控件上使用OnPaint在窗体上绘制图形。我在动画中遇到了问题,因为它不是让行星围绕太阳旋转(控制中心的一个固定点),而是围绕行星应该所在的点旋转。但是,该点仍围绕控制中心旋转 我在自定义控件的顶部声明了这些变量: private color col1; private float angle; private double r1, r2, ex, why; 以下是OnPaint中的代码: protected override v

我正在写一个太阳系模拟程序;我只是一个C#的初学者

我正在自定义控件上使用OnPaint在窗体上绘制图形。我在动画中遇到了问题,因为它不是让行星围绕太阳旋转(控制中心的一个固定点),而是围绕行星应该所在的点旋转。但是,该点仍围绕控制中心旋转

我在自定义控件的顶部声明了这些变量:

private color col1;
private float angle;
private double r1, r2, ex, why;
以下是OnPaint中的代码:

protected override void OnPaint(PaintEventArgs pe)
{
        this.DoubleBuffered = true;
        base.OnPaint(pe);
        Graphics g = pe.Graphics;

        AnimationControl anim = new AnimationControl(); 

        Planet sun = new Planet(50, 60);
        sun.drawSun(pe);

        angle += 0.01f;
        if (angle > 359)
        {
            angle = 0; 
        }
        Matrix matrix = new Matrix();
        matrix.Rotate(angle, MatrixOrder.Append);
        matrix.Translate(SandboxForm.ActiveForm.Width / 2,
            SandboxForm.ActiveForm.Height / 2, MatrixOrder.Append);
        g.Transform = matrix;

        r1 = 200; 
        r2 = 100; 
        double diameter = 40; 
        col1 = Color.Red;
        SolidBrush bru2 = new SolidBrush(col1); 
        ex = ((SandboxForm.ActiveForm.Width / 2) - diameter - (sun.getSunRadius())) + (r1 * (Math.Cos(angle))); /
        why = ((SandboxForm.ActiveForm.Height / 2) - diameter - (sun.getSunRadius())) + (r2 * (Math.Sin(angle))); 
        g.FillEllipse(bru2, (float)ex, (float)why, (float)diameter, (float)diameter); 
        Invalidate();
}

我不得不简化你的代码,因为有丢失的类等。但是在这里你可以看到它现在做你想要的

如果您尝试这段代码,您将看到旋转和平移的顺序确实很重要,但在您尝试我的建议时没有任何效果,因为您在应用变换之前正在绘制

还请注意,我在矩阵中使用了
,这是因为您应该在使用完它后处理它

    protected override void OnPaint(PaintEventArgs pe)
    {
        this.DoubleBuffered = true;
        base.OnPaint(pe);
        Graphics g = pe.Graphics;


        angle += 0.2f;
        if (angle > 359)
        {
            angle = 0;
        }
        using (Matrix matrix = new Matrix())
        {
            matrix.Rotate(angle, MatrixOrder.Append);
            matrix.Translate(300, 200, MatrixOrder.Append);

            g.Transform = matrix;
            pe.Graphics.DrawEllipse(Pens.Red, new Rectangle(50, 60, 50, 50));
        }


        Invalidate();
    }

你试过交换矩阵的顺序。旋转和矩阵。平移吗?是的,这不能解决我的问题。这和我现在做的差不多。谢谢!我感谢你的帮助。