Menu

Source code

Source code

Making math easy

In order to avoid getting entangled with all the complex mathematics, so that I could easily deal with the logic of the application, I've created the following 3 classes:

Direction This class makes sure your degrees is in the right quadrant. (0 = North, 90 = East, 180 = South, 270 = West) It also converts values smaller than 0 and larger than 359 to the corresponding value between 0 - 359. It is therefore possible to add two degrees together and use the result without any need for further calculations and checks. Also handy is that the value is exposed in two properties: Degrees and Radians (sic), which eliminate any further need to convert between the two.

Vector A vector consist of a Direction (Using the Direction class) and a Distance (double) property. The Vector Class also has a ConvertToCoordinate method, which allows you to get the corresponding Coordinate for the Vector, when the starting point of the vector is at Coordinate 0,0. Additionally, the Vector has an Add Method which allows you to add two vectors together. This is achieved by converting both vectors to Coordinates, and then adding them together. There is also a Subtract Method which works on the same principle.

Coordinate A coordinate consists of an X (double) and Y (double) property. The Coordinate class also has a ConvertToVector method, which allows you to get the corresponding Vector for the Coordinate, when the, when the starting point of the vector is at Coordinate 0,0. Additionally, the Coordinate has an Add Method which allows you to add two Coordinates together. This is achieved by adding their individual X and Y properties together.

These three classes turned what would otherwise be very complicated mathematical formula's into simple addition and subtraction, which allowed me to focus on other areas of the application.


Related

Wiki: Home

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.