试图绘制x长度为y度的线

时间:2014-01-08 12:48:27

标签: c# winforms trigonometry

我认为我的数学让我失望了!

我正试图在一个带有东南西北线的图片框控件上画一条x长度的Y线。

以下作品但是将线条绘制在250度而不是290度?

            Bitmap bmp = new Bitmap(400, 400);
        Graphics g = Graphics.FromImage(bmp);
        // smooth graphics
        g.SmoothingMode = SmoothingMode.AntiAlias;
        g.DrawLine(new Pen(Color.Black, 2), 205, 20, 205, 385);
        g.DrawLine(new Pen(Color.Red, 2), 20, 205, 390, 205);
        // let's draw a coordinate equivalent to (20,30) (20 up, 30 across)
        g.DrawString("N", new Font("Calibri", 12 , FontStyle.Bold), new SolidBrush(Color.Black), 197, 0);
        g.DrawString("S", new Font("Calibri", 12, FontStyle.Bold), new SolidBrush(Color.Black), 197, 385);
        g.DrawString("W", new Font("Calibri", 12, FontStyle.Bold), new SolidBrush(Color.Black), 0, 195);
        g.DrawString("E", new Font("Calibri", 12, FontStyle.Bold), new SolidBrush(Color.Black), 390, 195);

        //Draw Wind line
        int x = 205, y = 205;

        int wSpeed = 30*3; //length of line 
        int  angle = 290; //angle to draw line at.
        int  startX = x;
        int startY = y;
        int endX = Convert.ToInt32(Math.Round(x + wSpeed * Math.Sin(Calcs.Radians(angle))));
        int endY = Convert.ToInt32(Math.Round(y + wSpeed * Math.Cos(Calcs.Radians(angle))));
        g.DrawLine(new Pen(Color.Blue, 2), startX, startY, endX, endY);
        PictureBox display = pictureBox1;
        this.Controls.Add(display);
        display.Image = bmp;

public static double Radians(double dDegrees)
       {
           double result = Math.PI * dDegrees / 180.0;
           return result;
       }

1 个答案:

答案 0 :(得分:1)

如果你看一下结果:从270开始的-20度,而不是预期的+20,这意味着你得到了正确的Y但是错了X.我建议检查(改变)X坐标的符号。除非您是专家,否则图形通常会出现试错。

int endX = Convert.ToInt32(Math.Round(x - wSpeed * Math.Sin(Calcs.Radians(angle))));