20,741
edits
mNo edit summary |
m (add a vector based approach) |
||
| Line 58: | Line 58: | ||
As you can see below, indexing starts at 0. | As you can see below, indexing starts at 0. | ||
The problem is, that this only gives us a list of waypoints: | The problem is, that this only gives us a list of single waypoints: | ||
<syntaxhighlight lang="php"> | <syntaxhighlight lang="php"> | ||
| Line 69: | Line 69: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
What we really need to save all the other waypoint specific information is a new variable type that serves as the "container" for variables | What we really need to save all the other waypoint specific information is a new variable type that serves as the "container" for variables, so that we can save several variables for each waypoint. | ||
<syntaxhighlight lang="php"> | <syntaxhighlight lang="php"> | ||
| Line 80: | Line 80: | ||
var wp4brg = 0; # waypoint bearing | var wp4brg = 0; # waypoint bearing | ||
</syntaxhighlight> | </syntaxhighlight> | ||
== A vector based version == | |||
One simple way to accomplish this is using a nested vector for each waypoint: | |||
<syntaxhighlight lang="php"> | |||
var wp4 = 0; # waypoint number | |||
var wp4alt = 0; # waypoint altitude | |||
var wp4dist = 0; # waypoint distance | |||
var wp4angle = 0; #waypoint angle | |||
var wp4length = 0; # waypoint length | |||
var wp4id = ""; # waypoint id | |||
var wp4brg = 0; # waypoint bearing | |||
var waypoint4 = [wp4,wp4alt,wp4dist,wp4angle,wp4length,wp4id, wp4brg]; | |||
var waypoints = [waypoint4] | |||
</syntaxhighlight> | |||
First of all, we are setting up all the different variables for wp4, next you are storing all variables in a vector called "waypoint4". In the end, we store this vector in another vector named "waypoints". | |||
So, the very first vector element would be waypoints[0] and it would point to another vector (waypoint4), the elements of waypoint4 would be also available by index: | |||
<syntaxhighlight lang="php"> | |||
var wp4 = 1; # waypoint number | |||
var wp4alt = 1000; # waypoint altitude | |||
var wp4dist = 20.4; # waypoint distance | |||
var wp4angle = 33.4; #waypoint angle | |||
var wp4length = 12; # waypoint length | |||
var wp4id = "none"; # waypoint id | |||
var wp4brg = 122; # waypoint bearing | |||
var waypoint4 = [wp4,wp4alt,wp4dist,wp4angle,wp4length,wp4id, wp4brg]; | |||
var waypoints = [waypoint4] | |||
print ( waypoints[0][0] ) # contains the data stored in wp4 | |||
print ( waypoints[0][1] ) # contains the data stored in wp4alt | |||
print ( waypoints[0][2] ) # contains the data stored in wp4dist | |||
print ( waypoints[0][3] ) # contains the data stored in wp4angle | |||
print ( waypoints[0][4] ) # contains the data stored in wp4length | |||
print ( waypoints[0][5] ) # contains the data stored in wp4id | |||
print ( waypoints[0][6] ) # contains the data stored in wp4brg | |||
</syntaxhighlight> | |||
Obviously, you could add a bunch of other waypoints to the "waypoints" vector, too: | |||
<syntaxhighlight lang="php"> | |||
var waypoint4 = [wp4,wp4alt,wp4dist,wp4angle,wp4length,wp4id, wp4brg]; | |||
var waypoint5 = [wp5,wp5alt,wp5dist,wp5angle,wp5length,wp5id, wp5brg]; | |||
var waypoints = [waypoint4, waypoint5] | |||
</syntaxhighlight> | |||
The only problem with this approach is, that you'll need to set up all those wp variables - so there's a shorter version possible, by directly adding your data without using variables, i.e. "inline": | |||
<syntaxhighlight lang="php"> | |||
var waypoint4 = [1,1000,12,22,44,"none", 33]; | |||
var waypoint5 = [2,1500,22,42,14,"none", 133]; | |||
var waypoints = [waypoint4, waypoint5] | |||
</syntaxhighlight> | |||
There's an even more succinct version possible. The next step would be to use embedded vectors directly: | |||
<syntaxhighlight lang="php"> | |||
var waypoints = [[1,1000,12,22,44,"none", 33], [2,1500,22,42,14,"none", 133]] | |||
</syntaxhighlight> | |||
Accessing such a vector with embedded (or nested) vectors is still simple: | |||
<syntaxhighlight lang="php"> | |||
var waypoints = [[1,1000,12,22,44,"none", 33], [2,1500,22,42,14,"none", 133]] | |||
print(waypoints[0][0]) # prints 1 | |||
print(waypoints[0][1]) # prints 1000 | |||
print(waypoints[1][0]) # prints 2 | |||
print(waypoints[1][1]) # prints 1500 | |||
</syntaxhighlight> | |||
The only issue here is that you'll need to keep vector ordering in mind, so that you can always access the right element number. This could be simplified by using some variables with telling names as the index for each vector: | |||
<syntaxhighlight lang="php"> | |||
var NUMBER=0; var ALTITUDE=1; var DISTANCE=2; var ANGLE=3; var LENGTH=4; var ID=5; var BRG=6; | |||
var waypoints = [[1,1000,12,22,44,"none", 33], [2,1500,22,42,14,"none", 133]] | |||
print(waypoints[0][ALTITUDE]) # prints 1 | |||
print(waypoints[0][DISTANCE]) # prints 1000 | |||
print(waypoints[1][ALTITUDE]) # prints 2 | |||
print(waypoints[1][DISTANCE]) # prints 1500 | |||
</syntaxhighlight> | |||
So, this would already be much better than our original version, because we can now have an arbitrary number of waypoints. New waypoints would need to be added to the waypoints vector by using the append() library function: | |||
<syntaxhighlight lang="php"> | |||
var NUMBER=0; var ALTITUDE=1; var DISTANCE=2; var ANGLE=3; var LENGTH=4; var ID=5; var BRG=6; | |||
var waypoints = [[1,1000,12,22,44,"none", 33], [2,1500,22,42,14,"none", 133]] | |||
print(waypoints[0][ALTITUDE]) # prints 1 | |||
print(waypoints[0][DISTANCE]) # prints 1000 | |||
print(waypoints[1][ALTITUDE]) # prints 2 | |||
print(waypoints[1][DISTANCE]) # prints 1500 | |||
append(waypoints, [3,3000,122,212,34,"none", 133] ); | |||
</syntaxhighlight> | |||
Obviously, there's still one issue though: we are using lots of variables and helpers that all belong to the "waypoints" type, but which clutter our source code. So, next we are going to look into an even more flexible approach that nicely maps each waypoint field to a symbolic name, without having to remember vector indices. | |||
== A hash based version == | |||
Now, to wrap these fields into a single variable that serves as the container for other variables, we could use a Nasal hash. Consider the following empty hash: | Now, to wrap these fields into a single variable that serves as the container for other variables, we could use a Nasal hash. Consider the following empty hash: | ||