# How to find the nearest point inside a list in a given direction

Being $$\bar{\mathbf{x}} \in \mathbb{R}^3$$ a point and $$S =\{\mathbf{x}\}_{i=1}^N \in \mathbb{R}^3$$ a sample of N points. I am looking for a simple algorithm to determine the nearest point in $$S$$ in respect to $$\bar{x}$$ along a specific direction $$\mathbf{\hat{u}}$$

Thanks

• What do you mean nearest point along a specific direction? Is $S$ an unstructured point cloud? If yes, your definition is ill-posed. There might be no point at all in $\hat{\mathbf{u}}$ direction with respect to $\bar{\mathbf{x}}$. Oct 23 '19 at 21:30
• Do you accept an approximate solution? Well, if $S$ is an unstructured point cloud, which I assume it is indeed due to your notation of labeling points by their IDs, you need to accept just an approximate solution here. Please clarify a bit more here. Oct 23 '19 at 21:33

The question really boils down to how far two points $$x,y$$ are from each other in direction $$u$$. This is easily answered: You need to compute the component of $$y-x$$ onto $$u$$, i.e., their (signed) distance in direction $$u$$ is $$\frac{(y-x)\cdot u}{\|u\|}$$.
For your point cloud, you can then compute for each $$i$$ the (unsigned) distance $$d_i = \frac{(\bar x-x_i)\cdot u}{\|u\|}$$ and then you just need to find that $$i$$ for which $$d_i$$ is smallest.