-
Notifications
You must be signed in to change notification settings - Fork 0
/
Radar.cs
478 lines (383 loc) · 15.5 KB
/
Radar.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
using System.Windows.Forms;
namespace Interface_RADAR
{
public class Radar
{
#region Private Fields
// the diameter of the radar
int _size;
// the base image of the radar
// this saves time/cycles, b/c we only have to draw the
// background of the radar once (and update it as necessary)
Image _baseImage;
// the final rendered image of the radar
Image _outputImage;
// the azimuth of the radar
int _az = 0;
// some internally used points for drawing the fade
// of the radar scanline
PointF _pt = new PointF(0F, 0F);
PointF _pt2 = new PointF(1F, 1F);
PointF _pt3 = new PointF(2F, 2F);
// how the scanline knows when to move
Timer t;
// do draw the scanline or not to draw the scanline?
bool _scanLine = false;
// colors to use in drawing the image
Color _topColor = Color.FromArgb(0, 120, 0);
Color _bottomColor = Color.FromArgb(0, 40, 0);
Color _lineColor = Color.FromArgb(0, 255, 0);
Color _criticalColor = Color.FromArgb(255, 0, 0);
Color _WarColor = Color.FromArgb(255, 255, 0);
Color _orgColor = Color.FromArgb(255, 165, 0);
// list of items to draw on the radar
List<RadarItem> radarItemList = new List<RadarItem>();
#endregion
#region Constructor
public Radar(int diameter)
{
// set the diameter of the control
// this influences the size of the output image
_size = diameter;
// create the base image
CreateBaseImage();
// create the export image
Draw();
// set up the default timer intervals
// the timer is used for the scanline drawing
// it is also disabled by default
t = new Timer();
t.Tick += new EventHandler(t_Tick);
t.Interval = 40;
}
#endregion
#region Public Field Accessor/Mutators
public int DrawScanInterval
{
get
{
return t.Interval;
}
set
{
// update the timer interval
if (value > 0)
t.Interval = value;
}
}
public bool DrawScanLine
{
get
{
return _scanLine;
}
set
{
_scanLine = value;
// turn the timer on/off
t.Enabled = _scanLine;
// if the user turns off the scanline, redraw the image
if (!_scanLine)
Draw();
}
}
public Color CustomGradientColorTop
{
get
{
return _topColor;
}
set
{
_topColor = value;
// update the base image
CreateBaseImage();
// update the output image
Draw();
}
}
public Color CustomGradientColorBottom
{
get
{
return _bottomColor;
}
set
{
_bottomColor = value;
// update the base image
CreateBaseImage();
// update the output image
Draw();
}
}
public Color CustomLineColor
{
get
{
return _lineColor;
}
set
{
_lineColor = value;
// update the base image
CreateBaseImage();
// update the output image
Draw();
}
}
public Image Image
{
get
{
return _outputImage;
}
}
#endregion
#region Private Functions
/// <summary>
/// Creates the base radar image
/// </summary>
void CreateBaseImage()
{
// create the drawing objects
Image i = new Bitmap(_size, _size);
Graphics g = Graphics.FromImage(i);
Pen p = new Pen(_lineColor);
Pen p_red = new Pen(_criticalColor);
Pen p_yellow = new Pen(_WarColor);
Pen p_org = new Pen(_orgColor);
// set a couple of graphics properties to make the
// output image look nice
g.CompositingQuality = CompositingQuality.HighQuality;
g.InterpolationMode = InterpolationMode.Bicubic;
g.SmoothingMode = SmoothingMode.AntiAlias;
g.TextRenderingHint = TextRenderingHint.AntiAlias;
// draw the background of the radar
g.FillPie(new LinearGradientBrush(new Point((int)(_size / 2), 0), new Point((int)(_size / 2), _size - 1), _topColor, _bottomColor), 0, 0, _size - 1, _size - 1, 0, -180);
g.DrawArc(p, 0, 0, _size - 1, _size - 1, 0, -180);
Pen p1 = new Pen(_lineColor);
Pen p2 = new Pen(_topColor);
p2.Width = 2;
p1.Width = 3;
g.DrawArc(p1, 0, 0, _size - 1, _size - 1, -60, -60);
Font drawFont = new Font("Gill Sans MT Condensed", 10);
SolidBrush drawBrush = new SolidBrush(Color.AntiqueWhite);
g.DrawString("20 m", drawFont, drawBrush, new Point((int)(_size / 2), 0));
int interval = _size * 3/4 ;
g.DrawArc(p2, (_size - interval) / 2, (_size - interval) / 2, interval, interval, 0, -180);
p_yellow.Width = 3;
g.DrawArc(p_yellow, (_size - interval) / 2, (_size - interval) / 2, interval, interval, -60, -60);
g.DrawString("15 m", drawFont, drawBrush, new Point((int)((_size) / 2), (_size - interval) / 2));
interval = _size * 1/2;
g.DrawArc(p2, (_size - interval) / 2, (_size - interval) / 2, interval, interval, 0, -180);
p_org.Width = 3;
g.DrawArc(p_org, (_size - interval) / 2, (_size - interval) / 2, interval, interval, -60, -60);
g.DrawString("10 m", drawFont, drawBrush, new Point((int)((_size) / 2), (_size - interval) / 2));
interval = _size / 4;
g.DrawArc(p2, (_size - interval) / 2, (_size - interval) / 2, interval, interval, 0, -180);
p_red.Width = 3;
g.DrawArc(p_red, (_size - interval) / 2, (_size - interval) / 2, interval, interval, -60, -60);
g.DrawString("5 m", drawFont, drawBrush, new Point((int)((_size) / 2), (_size - interval) / 2));
interval = _size / 8;
g.DrawArc(p2, (_size - interval) / 2, (_size - interval) / 2, interval, interval, 0, -180);
interval = 3*_size / 8;
g.DrawArc(p2, (_size - interval) / 2, (_size - interval) / 2, interval, interval, 0, -180);
interval = 5 * _size / 8;
g.DrawArc(p2, (_size - interval) / 2, (_size - interval) / 2, interval, interval, 0, -180);
interval = 7 * _size / 8;
g.DrawArc(p2, (_size - interval) / 2, (_size - interval) / 2, interval, interval, 0, -180);
int a = _size - ((int)(_size / 2 - _size / 2 * Math.Cos(45.0)));
int b = (int)(_size / 2 - _size / 2 * Math.Cos(45.0));
int c = (int)(1 * _size / 4 * Math.Cos(45.0));
int d = (int)((_size-1-_size/7));
// Ecrire les valeurs des angles 45 et -45
g.DrawString("45°", drawFont, drawBrush, new Point(d-_size/35, c));
g.DrawString("-45°", drawFont, drawBrush, new Point(c, c));
// draw the x and y axis lines
g.DrawLine(p, new Point(0, (int)(_size / 2)), new Point(_size - 1, (int)(_size / 2)));
g.DrawLine(p, new Point((int)(_size / 2), 0), new Point((int)(_size / 2), _size /2));
//g.DrawLine(p, new Point((int)(_size / 2), (int)(_size / 2)), new Point(d, (int)(_size / 7)));
g.DrawLine(p, new Point((int)(_size / 2), (int)(_size / 2)), new Point((int)(_size / 7), (int)(_size / 7)));
g.DrawLine(p, new Point((int)(_size / 2), (int)(_size / 2)), new Point(d, (int)(_size / 7)));
// release the graphics object
g.Dispose();
// update the base image
_baseImage = i;
}
/// <summary>
/// Draws the output image and fire the event caller for ImageUpdate
/// </summary>
void Draw()
{
// create a copy of the base image to draw on
Image i = (Image)_baseImage.Clone();
// create the circular path for clipping the output
Graphics g = Graphics.FromImage(i);
GraphicsPath path = new GraphicsPath();
path.FillMode = FillMode.Winding;
path.AddEllipse(-1F, -1F, (float)(_size + 1), (float)(_size + 1));
// clip the output image to the circular shape
g.Clip = new Region(path);
// set a couple of graphics properties to make the
// output image look nice
g.CompositingQuality = CompositingQuality.HighQuality;
g.InterpolationMode = InterpolationMode.Bicubic;
g.SmoothingMode = SmoothingMode.AntiAlias;
g.TextRenderingHint = TextRenderingHint.AntiAlias;
// draw each RadarItem in the list
foreach (RadarItem item in radarItemList)
{
item.DrawItem(this, g);
}
// if the scanline is on, draw it
if (_scanLine)
{
// create the fade path and gradient
GraphicsPath gp = new GraphicsPath(FillMode.Winding);
gp.AddLine(new PointF((float)(_size / 2), (float)(_size / 2)), _pt2);
gp.AddCurve(new PointF[] { _pt2, _pt3, _pt });
gp.AddLine(_pt, new PointF((float)(_size / 2), (float)(_size / 2)));
PathGradientBrush pgb = new PathGradientBrush(gp);
pgb.CenterPoint = _pt;
pgb.CenterColor = Color.FromArgb(128, _lineColor);
pgb.SurroundColors = new Color[] { Color.Empty };
// draw the fade path
g.FillPath(pgb, gp);
// draw the scanline
g.DrawLine(new Pen(_lineColor), new PointF((float)(_size / 2), (float)(_size / 2)), _pt);
}
// draw the outer ring once more
// this gives the control a clearly defined edge, good for the UI
//g.DrawEllipse(new Pen(_lineColor), 0, 0, _size - 1, _size - 1);
// release the graphics object
g.Dispose();
// call the event caller
OnImageUpdate(i);
_outputImage = i;
}
//Rectangle GetRectFromPointFs(PointF[] pfArray)
//{
// int xMin = (int)pfArray[0].X;
// int xMax = (int)pfArray[0].X;
// int yMin = (int)pfArray[0].Y;
// int yMax = (int)pfArray[0].Y;
// foreach (PointF pF in pfArray)
// {
// if ((int)pF.X < xMin)
// xMin = (int)pF.X;
// if ((int)pF.X > xMax)
// xMax = (int)pF.X;
// if ((int)pF.Y < yMin)
// yMin = (int)pF.Y;
// if ((int)pF.Y > yMax)
// yMax = (int)pF.Y;
// }
// return new Rectangle(xMin, yMin, xMax - xMin, yMax - yMin);
//}
#endregion
#region Public Functions
public PointF AzRg2XY(int azimuth, int range)
{
// rotate coords... 90deg W = 180deg trig
double angle = (270d + (double)azimuth);
// turn into radians
angle *= 0.0174532925d;
double r, x, y, x1, y1;
// determine the lngth of the radius
r = (double)_size * 0.5d;
r = (r * (double)range / 20);
x = (((double)_size * 0.5d) + (r * Math.Cos(angle)));
y = (((double)_size * 0.5d) + (r * Math.Sin(angle)));
return new PointF((float)x, (float)y);
}
public PointF AzRg2XY_Scan(int azimuth, int range)
{
// rotate coords... 90deg W = 180deg trig
double angle = (270d + (double)azimuth);
// turn into radians
angle *= 0.0174532925d;
double r, x, y;
// determine the lngth of the radius
r = (double)_size * 0.5d;
r -= (r * (double)range / 90);
// r = (r * (double)range / 20);
x = (((double)_size * 0.5d) + (r * Math.Cos(angle)));
y = (((double)_size * 0.5d) + (r * Math.Sin(angle)));
return new PointF((float)x, (float)y);
}
public void AddItem(RadarItem item)
{
bool bFlag = true;
for (int i = 0; i < radarItemList.Count; i++)
{
if (radarItemList[i].ID == item.ID)
radarItemList[i] = item;
}
if (bFlag)
radarItemList.Add(item);
Draw();
}
#endregion
#region Event Handlers
void t_Tick(object sender, EventArgs e)
{
// increment the azimuth
_az++;
// reset the azimuth if needed
// if not, this will cause an OverflowException
if (_az >= 60)
_az = -60;
// update the fade path coordinates
_pt = AzRg2XY_Scan(_az, 0);
_pt2 = AzRg2XY_Scan(_az - 20, 0);
_pt3 = AzRg2XY_Scan(_az - 10, -10);
// redraw the output image
Draw();
}
#endregion
#region Outgoing Event Code
/// <summary>
/// Event caller for the ImageUpdate event
/// </summary>
/// <param name="i"></param>
private void OnImageUpdate(Image i)
{
if (ImageUpdate != null)
ImageUpdate(this, new ImageUpdateEventArgs(i));
}
/// <summary>
/// Event fired when the output image is redrawn
/// </summary>
public event ImageUpdateHandler ImageUpdate;
#endregion
}
/// <summary>
///
/// </summary>
/// <param name="b"></param>
public delegate void ImageUpdateHandler(object sender, ImageUpdateEventArgs e);
/// <summary>
///
/// </summary>
public class ImageUpdateEventArgs : EventArgs
{
Image _image;
public Image Image
{
get
{
return _image;
}
}
public ImageUpdateEventArgs(Image i)
{
_image = i;
}
}
}