【问题标题】:Lost trying to draw a line of x length y degrees试图画一条 x 长度 y 度的线时迷路了
【发布时间】:2014-01-08 21:01:59
【问题描述】:

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

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

以下工作,但在 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;
       }

【问题讨论】:

  • 你能展示一张你得到什么的照片吗?

标签: c# winforms trigonometry


【解决方案1】:

如果您查看结果:从 270 度到 -20 度,而不是预期的 +20 度,这意味着您得到正确的 Y 但错误的 X。我建议检查(更改)X 坐标的符号。除非您是专家,否则图形通常会反复试验。

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

【讨论】:

  • 谢谢你,我快疯了!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-02
  • 2020-09-30
  • 1970-01-01
  • 2018-04-08
  • 2021-08-18
相关资源
最近更新 更多