It's a fairly simple App and here is a zipped up project for it. It's for Visual C# 2010 though there's nothing specifically 2010 about it. If you have an earlier version of Visual Studio, create a new Winform application (Empty form, drop a PictureBox on to the form, name it PB and stretch it to cover most of the form or at least 650 x 500). Drop a close button on the form and rename it btnClose and finally copy the Form1.cs file into the project and overwrite the existing one. If you have that file open in Visual Studio/C# Express, it will notice the change as and prompt if you want to use the new file.
- Download ShowTrack.zip (Contains source code, no executables)
The Data
This consist of two arrays with about 15 int values in each. You can change this data, add more values or take some away but there must be the same number of elements in each array. The first array angles holds values from 0 to 11. Each is a multiple of 30 degrees, and corresponds to the hours on a clock face except for 0 instead of 12. The lengths array holds the length of each track segment in meters.
The application creates a Bitmap of the same size as the PictureBox and assigns it to the PB's image property. Until then the PictureBox is an empty container. Now it has a bitmap, you can obtain the Graphics context from the bitmap and draw on the form. If all forms came with a Bitmap image, it would eat up memory and slow the system down. This way doesn't.
The track is drawn as a series of lines. To simplify things I used the paint event to draw it. This means that if you move another window in front of the App, when you make the RaceTrack application the current ion, it redraws it. Just select the form, and in the form properties window, select the events tab (the lightning symbol), scroll down to the word paint and double click it. That generates a paint event handler and that's where all the code is.
Each line segment needs a start and end point which are imaginatively named P and NewP. Because the origin of the graphics, the point (0,0) is at the top left of the PictureBox and some lines have negative coordinates, I created the offx and offy variables which hold an offset. These are added in the short function Offset just before drawing the line.
Main Loop
This goes through the arrays, effectively converting from vectors (angle, length) to points. Virtually all programming languages have sin and cos(ine) functions that need radians not degrees and .NET is no exception. There are 2xPI radians in a circle's circumference because a radian is that length of part of the circumference that is the same length as the radius. So a radian is 180/PI degrees and 30 degrees = pi/6 radians. Multiplying the angle (0-11) by pi/6 gives the angle
In trigonometry the horizontal distance = length * Sin (angle) and vertical = length* cos(angle). However for those angles that point towards North, ie 300,330,0,30 and 60 the vertical component should be negative. That's why the len*Math.Cos(...) is subtracted.
After that the new point's x and y are converted to int and stored in Point NewP, OffP and OffNewP are the offset values of P and NewP and the line is drawn.
Note If you want to see text values on the form uncomment the three commented lines, //string Astr, //astr = and //G.Drawstring(...


