上一篇已經完成天空與地面的繪製,
接下來要繪製Pitch的刻度。
UserControl.cs
#region Pitch graphics.TranslateTransform(Width / 2f, Height / 2f);//設定繪圖原點至畫面中心 graphics.RotateTransform(Roll);//與Roll連動 var longEdge = 30f; var shortEdge = longEdge - 10f; var offset = 4.5f;//增加間距(純美觀可不加) for (var a = -90; a <= 90; a += 5) { //每5度為一刻度,以Pitch為中心只顯示+-20度 if (a <= Pitch + 20 && a >= Pitch - 20) { using (var pen = new Pen(Color.White, 3f)) { //每10度加強顯示 if (a % 10 == 0) { graphics.DrawLine(pen, new PointF(-longEdge + 0, 0 + (Pitch - a) * offset), new PointF(longEdge + 0, 0 + (Pitch - a) * offset)); } else { graphics.DrawLine(pen, new PointF(-shortEdge + 0, 0 + (Pitch - a) * offset), new PointF(shortEdge + 0, 0 + (Pitch - a) * offset)); } } } } graphics.ResetTransform(); #endregion
可以使用三元運算子簡化
#region Pitch graphics.TranslateTransform(Width / 2f, Height / 2f); graphics.RotateTransform(Roll); var longEdge = Width/2f/5f;//30 var shortEdge = longEdge - 10f; var offset = Height/2f/15f/5*4.5f;//4.5 //確認數值後跟長寬連動,這樣無論長寬如何改變比例都會維持 for (var a = -90; a <= 90; a += 5) { if (a <= Pitch + 20 && a >= Pitch - 20) { var edge = a % 10 == 0 ? longEdge : shortEdge; using (var pen = new Pen(Color.White, 3f)) { graphics.DrawLine(pen, new PointF(-edge + 0, 0 + (Pitch - a) * offset), new PointF(edge + 0, 0 + (Pitch - a) * offset)); } } } graphics.ResetTransform(); #endregion
為了讓角度0時有明顯的提示,另外加了綠粗線條。
#region Pitch graphics.TranslateTransform(Width / 2f, Height / 2f); graphics.RotateTransform(Roll); var longEdge = Width/2f/5f;;
var shortEdge = longEdge - 10f; var offset = Height/2f/15f/5f*4.5f;
for (var a = -90; a <= 90; a += 5) { if (a <= Pitch + 20 && a >= Pitch - 20) { var edge = a % 10 == 0 ? longEdge : shortEdge; using (var pen = a == 0 ? new Pen(Color.Green, 5f) : new Pen(Color.White, 3f)) { graphics.DrawLine(pen, new PointF(-edge + 0, 0 + (Pitch - a) * offset), new PointF(edge + 0, 0 + (Pitch - a) * offset)); } } } graphics.ResetTransform(); #endregion
加入刻度及水平指標
graphics.ResetTransform(); graphics.TranslateTransform(Width / 2f, Height / 2f); var centerRec = new RectangleF(-Width / 4f, -Height / 4f, Width / 2, Height / 2); using (var pen = new Pen(Color.Red, 4f)) { //刻度指標 graphics.DrawLine(pen, new PointF(0, 0), new PointF(-longEdge, longEdge / 2f)); graphics.DrawLine(pen, new PointF(0, 0), new PointF(longEdge, longEdge / 2f)); //水平指標 graphics.DrawLine(pen, new PointF(centerRec.Left - longEdge, 0), new PointF(centerRec.Left, 0)); graphics.DrawLine(pen, new PointF(centerRec.Right + longEdge, 0), new PointF(centerRec.Right, 0)); }
沒有留言:
張貼留言