Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2233 | - | 1 | '****************************************** |
2 | 'Code from Tom Pycke (http://tom.pycke.be) |
||
3 | 'Artificial horizon (http://tom.pycke.be/mav/100/artificial-horizon) |
||
4 | '****************************************** |
||
5 | Public Class ArtificialHorizon |
||
6 | |||
7 | Private g As Graphics |
||
8 | |||
9 | Private _roll_angle As Double |
||
10 | Public Property roll_angle() As Double |
||
11 | Get |
||
12 | Return _roll_angle |
||
13 | End Get |
||
14 | Set(ByVal value As Double) |
||
15 | _roll_angle = value |
||
16 | Invalidate() |
||
17 | End Set |
||
18 | End Property |
||
19 | Private _pitch_angle As Double |
||
20 | Public Property pitch_angle() As Double |
||
21 | Get |
||
22 | Return _pitch_angle |
||
23 | End Get |
||
24 | Set(ByVal value As Double) |
||
25 | _pitch_angle = value |
||
26 | Invalidate() |
||
27 | End Set |
||
28 | End Property |
||
29 | |||
30 | |||
31 | Private Sub ArtificialHorizon_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load |
||
32 | |||
33 | End Sub |
||
34 | |||
35 | Private Function pitch_to_pix(ByVal pitch As Double) As Integer |
||
36 | Return pitch / 35.0 * Me.Height / 2 |
||
37 | 'Return pitch / 45.0 * Me.Height / 2 |
||
38 | End Function |
||
39 | |||
40 | Private Sub ArtificialHorizon_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint |
||
41 | g = e.Graphics |
||
42 | g.SmoothingMode = Drawing2D.SmoothingMode.HighQuality |
||
43 | |||
44 | g.Clear(Me.BackColor) |
||
45 | Dim sin As Double = Math.Sin(roll_angle / 180 * 3.14) |
||
46 | |||
47 | g.ResetTransform() |
||
48 | ' g.FillRegion(Brushes.White, New Region(New Rectangle(0, 0, Me.Width, Me.Height))) |
||
49 | |||
50 | ' rounded rectangle |
||
51 | Dim path As New Drawing2D.GraphicsPath() |
||
52 | Dim r As Single = 50 |
||
53 | path.AddArc(0, 0, r, r, 180, 90) |
||
54 | path.AddArc(Me.Width - r, 0, r, r, 270, 90) |
||
55 | path.AddArc(Me.Width - r, Me.Height - r, r, r, 0, 90) |
||
56 | path.AddArc(0, Me.Height - r, r, r, 90, 90) |
||
57 | 'path.AddEllipse(0, 0, Me.Width, Me.Height) |
||
58 | path.CloseFigure() |
||
59 | g.SetClip(path) |
||
60 | |||
61 | g.TranslateTransform(Me.Width / 2, Me.Height / 2) |
||
62 | |||
63 | g.RotateTransform(roll_angle) |
||
64 | g.TranslateTransform(0, pitch_to_pix(pitch_angle)) |
||
65 | |||
66 | ' chocolate |
||
67 | Dim b As New System.Drawing.Drawing2D.LinearGradientBrush(New RectangleF(-Me.Width, 0, Me.Height * 2, Me.Width * 2), Color.FromArgb(255, 219, 140, 21), Color.Brown, Drawing2D.LinearGradientMode.Vertical) |
||
68 | g.FillRectangle(b, New RectangleF(-Me.Width * 2, +1, Me.Height * 4, Me.Width * 4)) |
||
69 | |||
70 | g.RotateTransform(180) |
||
71 | |||
72 | ' color.aqua |
||
73 | b = New System.Drawing.Drawing2D.LinearGradientBrush(New RectangleF(-Me.Width, -1, Me.Height * 2, Me.Width * 2), Color.FromArgb(255, 28, 134, 186), Color.DarkBlue, Drawing2D.LinearGradientMode.Vertical) |
||
74 | g.FillRectangle(b, New RectangleF(-Me.Width * 2, 0, Me.Height * 4, Me.Width * 4)) |
||
75 | |||
76 | |||
77 | |||
78 | |||
79 | g.ResetTransform() |
||
80 | Dim w2 As Single = Me.Width / 2 |
||
81 | Dim s As Single = Me.Width / 38 |
||
82 | g.TranslateTransform(Me.Width / 2, Me.Height / 2) |
||
83 | g.RotateTransform(45) |
||
84 | g.TranslateTransform(-w2 + s, 0) |
||
85 | g.DrawLine(New Pen(Color.White, 2), 0, 0, s * 2, 0) |
||
86 | g.TranslateTransform(+w2 - s, 0) |
||
87 | g.RotateTransform(15) |
||
88 | g.DrawLine(New Pen(Color.White, 2), -w2 + s, 0, -w2 + s * 2, 0) |
||
89 | g.RotateTransform(15) |
||
90 | g.DrawLine(New Pen(Color.White, 2), -w2 + s, 0, -w2 + s * 2, 0) |
||
91 | g.RotateTransform(15) |
||
92 | g.DrawLine(New Pen(Color.White, 2), -w2 + s, 0, -w2 + s * 3, 0) |
||
93 | 'g.DrawString("0°", New System.Drawing.Font("sans-serif", 9), Brushes.White, -w2 + 40, -4) |
||
94 | g.RotateTransform(15) |
||
95 | g.DrawLine(New Pen(Color.White, 2), -w2 + s, 0, -w2 + s * 2, 0) |
||
96 | g.RotateTransform(15) |
||
97 | g.DrawLine(New Pen(Color.White, 2), -w2 + s, 0, -w2 + s * 2, 0) |
||
98 | g.RotateTransform(15) |
||
99 | g.DrawLine(New Pen(Color.White, 2), -w2 + s, 0, -w2 + s * 3, 0) |
||
100 | 'g.DrawString("+45°", New System.Drawing.Font("sans-serif", 9), Brushes.White, -w2 + 40, -4) |
||
101 | |||
102 | |||
103 | g.ResetTransform() |
||
104 | |||
105 | Dim length As Single = Me.Width / 4 |
||
106 | Dim notch As Single = Me.Width / 30 |
||
107 | g.TranslateTransform(Me.Width / 2, Me.Height / 2) |
||
108 | g.DrawLine(New Pen(Color.White, 3), -length + notch * 2, 0, -notch, 0) |
||
109 | g.DrawLine(New Pen(Color.White, 3), notch, 0, length - notch * 2, 0) |
||
110 | g.DrawArc(New Pen(Color.White, 3), -notch, -notch, notch * 2, notch * 2, 180, -180) |
||
111 | |||
112 | g.ResetTransform() |
||
113 | |||
114 | ' driehoekje |
||
115 | Dim ww As Single = Me.Width / 38 |
||
116 | g.TranslateTransform(Me.Width / 2, Me.Height / 2) |
||
117 | g.RotateTransform(-90 + roll_angle) |
||
118 | path = New Drawing2D.GraphicsPath() |
||
119 | path.AddLine(w2 - ww * 3, 0, w2 - ww * 4, ww) |
||
120 | path.AddLine(w2 - ww * 4, -ww, w2 - ww * 4, ww) |
||
121 | path.AddLine(w2 - ww * 4, -ww, w2 - ww * 3, 0) |
||
122 | g.FillRegion(Brushes.White, New Region(path)) |
||
123 | g.DrawLine(New Pen(Color.White, 1), w2 - ww * 3, 0, w2 - ww * 4, ww) |
||
124 | g.DrawLine(New Pen(Color.White, 1), w2 - ww * 4, -ww, w2 - ww * 4, ww) |
||
125 | g.DrawLine(New Pen(Color.White, 1), w2 - ww * 4, -ww, w2 - ww * 3, 0) |
||
126 | |||
127 | |||
128 | |||
129 | g.ResetTransform() |
||
130 | g.ResetClip() |
||
131 | path = New Drawing2D.GraphicsPath() |
||
132 | path.AddPie(New Rectangle(ww * 3, ww * 3, Me.Width - ww * 6, Me.Height - ww * 6), 0, 360) |
||
133 | g.SetClip(path) |
||
134 | |||
135 | g.TranslateTransform(Me.Width / 2, Me.Height / 2) |
||
136 | g.RotateTransform(roll_angle) |
||
137 | g.TranslateTransform(0, pitch_to_pix(pitch_angle)) |
||
138 | For i As Integer = -80 To 80 Step 10 |
||
139 | drawpitchline(g, i) |
||
140 | Next i |
||
141 | |||
142 | End Sub |
||
143 | |||
144 | Private Sub drawpitchline(ByVal g As Graphics, ByVal pitch As Double) |
||
145 | Dim w As Single = Me.Width / 8 |
||
146 | g.DrawLine(Pens.White, -w, pitch_to_pix(-pitch + 5), w, pitch_to_pix(-pitch + 5)) |
||
147 | g.DrawLine(Pens.White, -w * 5 / 3, pitch_to_pix(-pitch), w * 5 / 3, pitch_to_pix(-pitch)) |
||
148 | g.DrawString(pitch, Me.Font, Brushes.White, -w * 75 / 30, pitch_to_pix(-pitch) - 5) |
||
149 | g.DrawString(pitch, Me.Font, Brushes.White, w * 2, pitch_to_pix(-pitch) - 5) |
||
150 | End Sub |
||
151 | |||
152 | Private Sub drawrollline(ByVal g As Graphics, ByVal a As Single) |
||
153 | Dim w2 As Single = Me.Width / 2 |
||
154 | |||
155 | |||
156 | g.RotateTransform(a + 90) |
||
157 | g.TranslateTransform(-w2 + 10, 0) |
||
158 | g.DrawLine(Pens.White, 0, 0, 20, 0) |
||
159 | g.TranslateTransform(10, 5) |
||
160 | g.RotateTransform(-a - 90) |
||
161 | g.DrawString("" & (a) & "°", New System.Drawing.Font("sans-serif", 9), Brushes.White, 0, 0) |
||
162 | g.RotateTransform(+90 + a) |
||
163 | g.TranslateTransform(-10, -5) |
||
164 | g.TranslateTransform(+w2 - 10, 0) |
||
165 | g.RotateTransform(-a - 90) |
||
166 | End Sub |
||
167 | End Class |