*2.3.2. The problem of selecting a connection*

In the HypeRSimRIP program, it is necessary to select a connection between two devices, in order to see its properties or in order to activate/deactivate it. A connection between two devices is represented in the graphical zone by a 2 px wide line between the devices in question, which are in turn represented by discs of radiuses between 5 px and 15 px (see Figure 3).

**Figure 3.** Connection example between the routers A and B.

Two devices (i.e. discs) cannot overlap, thus the length between two centres is at least 5 px + 5 px=10 px (i.e. 5 px represents the minimum radius). Moreover, as it was previously seen, a selection cannot occur at less than 15 px of a device (i.e. 15 px represents the maximum device radius), in order to disallow the user the possibility of inserting overlapping devices.

In this problem, we not only know the coordinates of the device centres *A* and *B*, but also the coordinates of the selection point *e*.

In mathematics, to test if the point *e* is on the segment [*AB*], it is first tested if *A,e,B* are collinear and if so, the second test verifies if *e* is between the segment end points.

In informatics, the problem that appears is that the plane (graphical zone) is discrete (it only has a finite set of points), the size of one point being of 1 px. Thus, it is almost impossible to select a point exactly on the [*AB*] segment. To correct this problem, instead of verifying if the point *e* is on the segment, it must be checked if the point is near the segment, at maximum 5 px distance from the segment, which is 'somewhere' between points *A* and *B* and at least 20

px distance from these two points (5 px, because a selection inside the disc—the minimum length being 5 px—would represent the selection of the device and not of the connection, and another 15 px to compensate with the unselectable area around the device, as previously mentioned). By being 'somewhere' between points *A* and *B*, it is understood that the angles ∢*eAB* and ∢*eBA* must be acute angles (see Figure 4).

**Figure 4.** If ∢*eAB* is obtuse, then clearly the selection was outside of the [*AB*] segment.

It can be easily proved that ∢AeB must be obtuse, by using the cosine theorem and the restrictions given by the problem. In a similar fashion, it can be proved that ∢AeB is obtuse, if and only if

$$A\mathbf{B}^2 > A\mathbf{e}^2 + \mathbf{e}\mathbf{B}^2\tag{1}$$

In order to calculate the distance *h* between *e* and the segment *[AB]*, by only knowing the coordinates of the *A,e,B* points, with simple surface formulae, it can be obtained that

$$\begin{vmatrix} A.X & A.Y & 1\\ B.X & B.Y & 1\\ e.X & e.Y & 1 \end{vmatrix} \tag{2}$$
 
$$\frac{A}{AB}$$

which gives the following evaluation statement:

$$\text{Math.Abs}(\text{surfance}(\mathbf{e}\_\mathsf{A}, \mathsf{A}, \mathsf{B})) \Leftarrow \text{PointDistance}(\mathsf{A}, \mathsf{B}) \texttt{\*}\mathsf{S} \tag{3}$$

where the surface method is defined by the Sarrus formula for the calculation of 3 × 3 deter‐ minants. Note that the division from the formula was not done in the code because when working with int-type variables (integer numbers), there are some precision issues in the event of division with remainder.

Thus, we obtain the following selection management algorithm:

**Step 1.** If the selection was made inside the disc of a device, the device will be selected.

**Step 2.** Else, if the selection was made at maximum 15 px around a device, the selection process will be cancelled.

**Step 3.** Else, if conditions (1) and (3) are satisfied, then it is considered that the connection between devices A and B was selected.
