Anda di halaman 1dari 7

A Car Navigation System Using C Language and OpenGL

Yoga Febrian Pratama - B6TB1712

I. Introduction
The C Language is a powerful language capable of giving the user bigger control of the hardware,
in addition to the portability. For example, a C code file can be run in a PC, your smartphone, or a
scientific graphing calculator. In addition to this, C has an advantage from other languages in terms
of memory allocation.
Unlike most programming languages, C enables the user to write directly into the memory. Its
features, such as arrays and pointers, are designed to manipulate and arrange memory in an efficient,
yet machine-independent way.
In development of C language, a lot of the users build up a set of commonly used functions known
as a library. As of now, various libraries are available to be used by users to simplify programming
process. In addition to libraries, the programming language has cross platform capabilities and can
run several other programming interfaces, one of them is OpenGL. It is a platform to render 2D
and 3D vector graphics,
II. Discussion and Result
1. The Idea
The idea behind the car navigation system is loading a database of crossing which holds
information about its ID, name, location, and its adjacent crossing. Each of this location is
symbolized by a circle to a drawn map. A line is then drawn between two adjacent crossings to
symbolize a road. Each of this line is drawn in different colors depending on the randomized
condition of the road. The code for the mentioned function is as follows.
data, it would be better to use a randomizer
to assign an integer to flag each road
condition. The integer 0 means normal
traffic, 1 means light traffic, 2 means
medium traffic, 3 means heavy traffic, and
4 means road under construction.
In addition to this, a color is assigned to the
corresponding line of each crossing. Green
for normal traffic, dark green for light
traffic, yellow for medium traffic, orange
Figure 1. The code to randomize condition for heavy traffic, and red for roads under
flags construction. This is possible by using a
As the map itself is not based on real-time conditional function to set the glColor3d
function before producing the line. The possibility of too many roads are blocked
number of road under construction is also causing the program to fail in searching a
limited to 10. This is done to evade the possible path to target

2. The Basics of Navigation

a. Reading Database and Searching for Matches
The first step of building the navigation system is to build the program to read
a set of data from a given database. This can be done by using the following
set of codes.

The variable fp are set by opening the database file, “08-map.dat”. The
database file starts with an integer that shows the number of crossing available.
The program would then proceed to read the list below the first line and stores
each data into its corresponding locations. After closing the file, the function
map_read will return the number of available crossing as it will be used in
different functions as a condition for a loop.
Shortly after saving the read data to the variable called cross, which is a
structure, the program would ask for the user to input their current location
and their destination. Using a string match function, the program will select
from the database the matching crossing name. When one or more is found,
the program will print out all the matches and the user will be asked to pick
the suitable choice. The program will run the algorithm to find the shortest
path afterwards.
The interface of the program in getting the input and giving options can be
seen in the figure below.

Figure 2. Front screen interface of the built car navigation system

One of the features shown here is choosing the mode of transportation. Each
of the options manipulates a variable called velocity which changes the length
of waiting time for buffer swaps. In addition, the road condition will also affect
buffer swapping time for car and motorbike users, as pedestrians and people
who cycles are rarely affected by road conditions.
b. Finding Shortest Path
To find the shortest path, Dijkstra’s algorithm is used. This algorithm
calculates the distance of a crossing nearby to the goal to the starting line.
Then, the program will verify whether it is the closest or not each time new
data is added. The function to run the algorithm can be divided into three
main parts, Initialization, Node Definition, Response.
The initialization is where all needed values of the variables contained in the
structure called cross is initialized. These variables are distance, nextPath, and
done[i]. In addition, the variable distance_add[i] is initialized with a value
that depends on the traffic condition. The value of distance is set to a really
huge number, nextPath is set to -1, and done[i] to 0.
The code script beside is the code for
the initialization part of the function
to run Dijkstra’s algorithm. In
addition to the normally calculated
distance between two points, the
condition in each crossing are also
allocated. However, as the program
itself does not know the concept of
time, each condition is converted to
act as an additional distance. This is
done by introducing a new array
called distance_add and adding it
into the calculation of comparison
between calculated distance and
minimum distance. If the value of
problem is equal to 0, then no additional distance will be given. If the value of
problem is 1, an additional 1,5 distance will be added into the calculation.
Worse traffic has bigger additional distance.
The next part of the function is node definition. This can be translated as
finding the next nearest node to a chosen crossing for each of the crossing.
This is done by using a double loop as seen in the code below.
If the crossing is not
flagged as checked
(done is not equal to
1) and the distance is
smaller than the
minimal distance, the
crossing being
checked will be the
nearest crossing chosen as a node. After doing so, the closest crossing done
value will be flagged to 1 and its distance to the target point will be set as the
minimum distance.
After doing so, the program will run a response to the data found. The
response is either to retain data or update data based on the value calculated.
In this part of the function, as shown in the following code script, the value of
distance_add will be added to calculation.
Typically, the additional distance_add[k] are not included in the program.
However, by adding it, a closer crossing would not be chosen if the traffic is
way too heavy or road is under construction, unless no shorter way can be
found. An update response will be done if the distance of the crossing to its
adjacent crossings are bigger than the value of calculated distance with the
additional distance incorporated.
Another variable, distance2 was added to the structure cross to store the real
distance between two points, without the value of distance_add[k], since we
want this value to be shown in the screen instead of the manipulated value.
However, during calculation, manipulated distance is used instead of this real
distance to incorporate the effect of traffic into path searching.

c. Outputting Map to Screen

The program will then output the map and moves the map depending on the
found path. This is done by using the following code.
An example output is given in Figure 3.

Figure 3. Example of an output map

The figure above does not represent the whole map, but rather a close-up of
the dot resembling the vehicle. On the screen, several features can be seen.
The first one being the road colored depending on the traffic conditions. The
next one is the addition of next crossing name on top of the vehicle. The
background color has also been swapped with a greener color to resemble the
earth better.
In addition to this, the road to each crossing are divided into 2, One for each
direction (to crossing and from crossing). This is done because each of the
direction are made to have different conditions. By doing so, clearer depiction
of the level of traffic in each crossing can be shown.
3. Possible Feature Upgrades
The program is far from being complete. There are several noticeable parts that
can be further improved. The first one being the position of the next crossing sign.
It is clearly unpleasing to the eyes, the challenges lie in the fact that the map is
rotated in both x and z axis, thus finding the correct position would require deeper
math calculations.
The second one would be the fact that the user had to switch screens between
map and terminal. It would be better if both are done in one screen, deeper
research would be needed to understand how the openGL screen can accept
inputs from the user. In addition to this, math calculations would be needed to
find the perfect coordinates for each menu.
Another simple addition would be a navigation or 3D view, this would take some
time, but the concept shouldn’t be too different with combining translation and
rotation matrix to create a 3D visual effect. Making a full 3D view would be
impossible just by assigning a 3D coordinate, especially the database file does not
have the z-coordinates of each crossing.
III. References
[1] Lextrait, Vincent. (n.d.). The Programming Languages Beacon. Retrieved
January 28, 2018, from
[2] Computer Seminar Lecture Notes