在Yaw刻度上增加文字,
每45度代表不同方位,
飛控回傳的值有可能會超過+-360度,
所以必須做出相對應的處理。
#region Yaw graphics.ResetTransform(); //增加Yaw背景 var yawBg = new RectangleF(0, 0, Width, Height / 2f / 7.5f); using (var brush = new SolidBrush(Color.FromArgb(128, 255, 255, 255))) { graphics.FillRectangle(brush, yawBg); } //只顯示+-60度 var start = Yaw - 60f; var end = Yaw + 60f; var boundaryWidth = Width / 15f;//邊界 var space = (Width - boundaryWidth) / 120f;//間隔 var lineHeight = yawBg.Height * 2 / 5f;//刻度 //畫刻度(start要轉型成int,不然如果是double或float會畫不出來) for (var a = (int)start; a <= end; a++) { if (a % 5 == 0) { //每5度為一刻度 var posX = yawBg.Left + boundaryWidth / 2f + (a - start) * space; var posY = yawBg.Bottom - lineHeight / 2f; using (var pen = new Pen(Color.White, 2f)) { graphics.DrawLine(pen, posX, posY, posX, posY - lineHeight); } //刻度加上文字,每45度為一方位角 if (a % 15 == 0) { var disp = a; while (disp < 0) disp += 360;//將角度轉換為正向角 disp %= 360; string directionString; switch (disp) { case 0: directionString = "N"; break; case 45: directionString = "NE"; break; case 90: directionString = "E"; break; case 135: directionString = "SE"; break; case 180: directionString = "S"; break; case 225: directionString = "SW"; break; case 270: directionString = "W"; break; case 315: directionString = "NW"; break; default: directionString = disp.ToString(); break; } posY -= Height/2f/15f;//10 DrawString(directionString.PadLeft(5), new Font(Font.FontFamily, fontSize), new SolidBrush(Color.White), posX, posY, graphics, StringAlignment.Center); } } } //刻度底線 using (var pen = new Pen(Color.White, 2f)) { graphics.DrawLine(pen, yawBg.Left + boundaryWidth / 2f, yawBg.Bottom - lineHeight / 2f, yawBg.Right - boundaryWidth / 2f, yawBg.Bottom - lineHeight / 2f); } #endregion
沒有留言:
張貼留言