## 2 Materials and Methods
### Determination of the Cardinal Direction
\begin{table}
\begin{tabular}{c c c c c c c c} \hline \hline
**Factor Case** & **1** & **2** & **3** & **4** & **5** & **6** & **7** & **8** \\ \hline Latitudes’ difference & 0 & 0 & + & – & + & + & – & – \\ Longitudes’ difference & + & – & 0 & 0 & + & – & + & – \\
**The determined direction** & **E** & **W** & **N** & **S** & **NE** & **NW** & **SE** & **SW** \\ \hline \hline \end{tabular}
\end{table} 
Table 1: Factors and cases for determining the exact cardinal direction. if a value of either of the variables beforeLatitude or beforeLongitude is zero, the algorithm is stopped; otherwise, the differences in the values of the latitudes and longitudes are obtained. If the two differences are 0, it indicates that there is no change in location, and therefore, the direction of the azimuth sensor of the smartphone is accordingly updated. The next step obtains the heading direction by the following Algorithm 2, which determines the direction of vehicles based on the difference in the values of the latitudes and longitudes estimated by Algorithm 1. ### Determination of the Representative Heading Direction for Connected Cars

The previous section presented the method for determining the cardinal direction using the smartphone inside a vehicle. This section describes the mechanism for determining the representative heading direction for subgroups of connected cars by obtaining the directions estimated from multiple smartphones in the previous section. The reason for determining the heading direction of the subgroup of connected cars is illustrated in Figure 1.

As illustrated in Figure 1, the two connected cars are moving in the same direction, but vehicle 1 is heading in the northwest (NW) direction, and vehicle 2 in the northeast (NE) direction. The driving direction should be the same for both vehicles in this connected group. However, as the headings differ, a representative heading direction must be designated to determine the sequence of the vehicles. Therefore, in this section, four cardinal directions are determined by analyzing the headings of the connected cars, as presented in Table 2.

In Table 2, Direction refers to the representative direction for the subgroup of connected cars, and Heading refers to the cardinal direction of each connected car. As an example, for a sequence of a \begin{table}
\begin{tabular}{c c c c c} \hline \hline
**Heading Direction** & **E** & **W** & **S** & **N** \\ \hline E & **1** & 0 & 0 & 0 \\ W & 0 & **1** & 0 & 0 \\ S & 0 & 0 & **1** & 0 \\ N & 0 & 0 & 0 & **1** \\ SE & **1** & 0 & **1** & 0 \\ NE & **1** & 0 & 0 & **1** \\ SW & 0 & **1** & **1** & 0 \\ NW & 0 & **1** & 0 & **1** \\ \hline \hline \end{tabular}
\end{table} 
Table 2: Estimation of the representative heading direction of the connected cars. Figure 1: Situation for same driving directions but different headings. connected car group with the east direction, 'E,' 'SE,' or 'NE' are marked as 1. The algorithm used for this method is as follows. ```
/* Input: v \(\leftarrow\) A list of connected cars */ BEGIN  Initialize eastCount, westCount, southCount, northCount to 0  FOR i = 0 to size of v  IF v contains 'E' THEN eastCount++ ENDIF  IF v contains 'W' THEN westCount++ ENDIF  IF v contains 'S' THEN southCount++ ENDIF  IF v contains 'N' THEN northCount++ ENDIF  ENDFOR IF eastCount >= 2 THEN headingDecision \(\leftarrow\) \"EAST\"  ELSE IF westCount >= 2 THEN headingDecision \(\leftarrow\) \"WEST\"  ELSE IF southCount >= 2 THEN headingDecision \(\leftarrow\) \"SOUTH\"  ELSE IF northCount >= 2 THEN headingDecision \(\leftarrow\) \"NORTH\"  ELSE headingDecision \(\leftarrow\) \"NONE\"  ENDIF  RETURN headingDecision  END
```
**Algorithm 2** Determining the representative heading direction

In Algorithm 2, the heading of the connected cars represents the number of vehicles heading in the east, west, north, and south. If the value of each count variable is 2 or more, it means that vehicles are connected and driving in the same direction, and therefore, the corresponding direction is determined as representative.