Click here to Skip to main content
15,886,052 members
Articles / Multimedia / GDI+
Article

Drawing a Radar Display Using C#

Rate me:
Please Sign up or sign in to vote.
4.54/5 (31 votes)
17 Aug 2007CC (ASA 2.5)3 min read 549.9K   16K   134   41
Plot symbols on a radar using the azimuth and elevation

Screenshot - Radar1.jpg

Introduction

Originally, I implemented the idea of a radar-esque object as a UserControl for the purpose of parsing NMEA-structured GPS data and displaying the satellites in view. This worked well, but I've also wanted to write my first article for some time now, and this seemed like the time to do it. The Radar class internally maintains a structure of RadarItem implementations and draws them on radar. The position of the RadarItem is determined by translating two values: Azimuth and Elevation.

Using the code

Class Diagram

Screenshot - Radar2.jpg

Getting the Radar image

As shown in the Class Diagram, there are a few classes and an interface to work with in this implementation. First and foremost is the Radar class. This is the main class for display of the radar image. There are two ways to get the Image from the Radar class:

  1. Use the Radar.Image member
  2. Use the ImageUpdateEventArgs.Image passed by Radar.ImageUpdate

The following example demonstrates a combination of both for the purpose of showing the blank radar initially, not having to wait for an update to occur. The Radar.ImageUpdate event is fired every time a change is made to the drawable elements of the Radar class.

C#
Radar radar;

private void frmMain_Load(object sender, EventArgs e)
{
    // create a new instance of the Radar class
    Radar radar = new Radar(pictureBox1.Width); 
    // hint: use a square PictureBox

    // display the base image initially
    pictureBox1.Image = radar.Image;

    // add a handler for the ImageUpdate event
    radar.ImageUpdate += new ImageUpdateHandler(radar_ImageUpdate);
}

void radar_ImageUpdate(object sender, ImageUpdateEventArgs e)
{
    // display the updated image
    pictureBox1.Image = e.Image;
}

Color Options

The Radar class has a few customizable colors in order to include the background colors (top and bottom of gradient) and the color of the lines drawn.

C#
Radar radar = new Radar(pictureBox1.Width);

// change the colors of the radar
radar.CustomGradientColorTop = Color.FromArgb(0, 100, 0);
radar.CustomGradientColorBottom = Color.FromArgb(0, 40, 0);
radar.CustomLineColor = Color.FromArgb(0, 255, 0);

pictureBox1.Image = radar.Image;

Adding RadarItem objects to the Radar

There are three included classes that implement the RadarItem interface. They are the CircleRadarItem, SquareRadarItem and TriangleRadarItem objects. Feel free to create your own (see source code for examples) and email it to me!

RadarItems are differentiated from one another using the ID member. This also allows for the Radar object to update the position of a particular RadarItem. The Radar class contains an internal List<RadarItem> that you can add items to using the Radar.AddItem method. When you pass a RadarItem with a pre-existing ID, the RadarItem in the List is replaced with the new RadarItem.

C#
Radar radar = new Radar(pictureBox1.Width);

// Arguments:
//   1. ID
//   2. Size
//   3. Azimuth
//   4. Elevation
RadarItem item = new CircleRadarItem(2, 8, 45, 45);

pictureBox1.Image = radar.Image;

Extending the RadarItem Interface

C#
public interface RadarItem : IComparable<RadarItem>
{
    int ID { get; }
    int Azimuth { get; set; }
    int Elevation { get; set; }
    int Height { get; set; }
    int Width { get; set; }
    DateTime Created { get; }
    void DrawItem(Radar radar, Graphics g);
}

This is relatively straightforward. Just implement the interface and go to town! The DrawItem method is called from the Radar class. Supplied is the instance of the Radar class, because of the AzEl2XY method. In order to convert the Azimuth and Elevation to a PointF, the algorithm has to be aware of the size of the Radar. This method lives in the Radar class for logical reasons and to not duplicate the code.

Also exposed through the Radar class are the colors. I use Radar.CustomLineColor as the color of my custom RadarItem implementations. Also passed is an instance of Graphics. This is the Graphics that is used to draw directly on the Radar. Although the RadarItem interface requires the implementation of IComparable<RadarItem>, it is not used for anything yet. The Created member is also unused for the time being.

The Scan Line

Radar includes a minor implementation of a "Scan Line" with fade. NOTE: When using the ScanLine, you will want to use the Radar.ImageUpdate event to update the Image on screen. This is because the Image is constantly being updated.

C#
Radar radar = new Radar(pictureBox1.Width);
radar.ImageUpdate += new ImageUpdateHandler(_radar_ImageUpdate);

radar.DrawScanInterval = 60; // in millisecs
radar.DrawScanLine = true;

Points of Interest

Particularly fun was adding the "Scan Line" to the Radar as suggested by Ali, a coworker. I learned all about the GraphicsPath object, as well as the PathGradientBrush. See the code for details.

Radar stores the background image separately in order to cut down on the drawing time. This image is updated every time the value of a background-related property is changed, i.e. CustomLineColor.

History

  • 17 August, 2007 -- Original version posted

License

This article, along with any associated source code and files, is licensed under The Creative Commons Attribution-ShareAlike 2.5 License


Written By
Technical Lead
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionHow to catch of a target? Pin
long buivan26-Nov-23 5:30
long buivan26-Nov-23 5:30 
QuestionMissing Assembly in the Source Pin
Tim8w22-Jan-21 14:15
Tim8w22-Jan-21 14:15 
QuestionRemove one of the radar items Pin
Member 1482365810-May-20 2:28
Member 1482365810-May-20 2:28 
AnswerRe: Remove one of the radar items Pin
Member 1482365810-May-20 5:00
Member 1482365810-May-20 5:00 
QuestionDelay in scanline revolution Pin
Member 1409034614-Feb-19 18:04
Member 1409034614-Feb-19 18:04 
QuestionGAMES Pin
Member 1095317117-Jul-14 2:03
Member 1095317117-Jul-14 2:03 
QuestionHow to dispose of radar item? Pin
afs0012-Jul-14 11:05
afs0012-Jul-14 11:05 
Questionhow to add scale to the radar,track numbers to plots. also is it possible to show the full tracks in addition to plots Pin
gaurav47921-Nov-13 18:53
gaurav47921-Nov-13 18:53 
Hello ,
Great work.
Q 1. Is it possible to show full tracks instead of plot.
Q 2. Is it possible to add scale to the image in order to tell what is the max-min range the radar can plot.
Q 3. Is it possible to add track/plot Id to the plots.
Thanks in advance and expecting a quick reply.
Questionmultithread exception Pin
Member 1032943910-Oct-13 21:30
Member 1032943910-Oct-13 21:30 
AnswerRe: multithread exception Pin
Member 1032943910-Oct-13 22:08
Member 1032943910-Oct-13 22:08 
GeneralMy vote of 5 Pin
will_aha8-Jan-13 14:45
will_aha8-Jan-13 14:45 
QuestionScan-line angle Pin
Member 952790819-Oct-12 23:42
Member 952790819-Oct-12 23:42 
GeneralMy vote of 5 Pin
syscom028614-Oct-10 2:12
syscom028614-Oct-10 2:12 
QuestionHow to use the source code Pin
kelvinjacobs14-Sep-10 15:49
kelvinjacobs14-Sep-10 15:49 
Questionhow to move the square on the radar sweep form point to point? Pin
P57018-Jul-10 18:09
P57018-Jul-10 18:09 
GeneralTracking targets Pin
chakri411-Mar-10 7:06
chakri411-Mar-10 7:06 
Questionis it possible to add tooltipe to radar item ?? Pin
yoramn@cti2.com20-May-09 3:31
yoramn@cti2.com20-May-09 3:31 
AnswerRe: is it possible to add tooltipe to radar item ?? Pin
ahmed eid ahmed12-Feb-10 23:03
ahmed eid ahmed12-Feb-10 23:03 
Questionabout cpu usage problem Pin
webgiser5-Mar-09 1:59
webgiser5-Mar-09 1:59 
GeneralSector Scan Pin
Stellenbosch3-Dec-08 2:42
Stellenbosch3-Dec-08 2:42 
QuestionMemory hog Pin
ebenedict20-Oct-08 7:31
ebenedict20-Oct-08 7:31 
AnswerRe: Memory hog Pin
JimBlaney22-Oct-08 17:14
JimBlaney22-Oct-08 17:14 
GeneralRe: Memory hog Pin
ebenedict24-Oct-08 16:35
ebenedict24-Oct-08 16:35 
GeneralRe: Memory hog Pin
JimBlaney25-Oct-08 5:18
JimBlaney25-Oct-08 5:18 
Generalnot a real radar Pin
_marsim_11-Jun-08 5:50
_marsim_11-Jun-08 5:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.