# Among all intersection points between a ray defined by a point $M$ and a vector and a surface, find which one is closest to $M$?

Suppose I have a ray in 3d space with an origin at $$(x_0, y_0, z_0)$$ and direction $$(a, b, c)$$. This can be represented with the following equation

$$\frac{x-x_0}{a} = \frac{y-y_0}{b} = \frac{z-z_0}{c}$$

And suppose I have another function in 3d space,

$$y=sin(x)+cos(z)$$

Assuming the intersection points between the straight line and the surface, how do I find the point (or a close approximation) closest to $$(x_0, y_0, z_0)$$ that these two entities intersect?

My understanding is I can rework the ray into the following:

$$x = \frac{a}{b}(y-y_0)+x_0$$

$$z = \frac{c}{b}(y-y_0)+z_0$$

Then plug those equations for x and z into the sinusoidal function,

$$y = sin(\frac{a}{b}(y-y_0)+x_0) + cos(\frac{c}{b}(y-y_0)+z_0)$$

Rearrange it and set it equal to 0:

$$f(y) = sin(\frac{a}{b}(y-y_0)+x_0) + cos(\frac{c}{b}(y-y_0)+z_0) - y$$

And this new function $$f(y)$$ represents the distance of a point on the ray from the original sinusoidal function. So I'd want to find $$y$$ where $$f(y) = 0$$. Finally I could plug the discovered $$y$$ value back into the ray equations to get $$x$$ and $$z$$.

Am I on the right track, and how would I find $$f(y)=0$$?

## 1 Answer

In general (see remark below), the straight line intersects the surface in a finite number of points. Fig. 1 : A case with 3 intersection points. Point $$(x_0;y_0;z_0)$$ is figured as a little circle.

You are right when you look for solutions to your equation

$$y = \sin\left(\frac{a}{b}(y-y_0)+x_0\right) + \cos\left(\frac{c}{b}(y-y_0)+z_0\right)$$

But solutions $$y_k$$ of (1) w haven't to be plugged into parametric equations $$x=... ; z=...$$

It suffices

1) To consider them as abscissas on the straight line (variable $$y$$ can be thought as a sort of time variable). Then select, among all $$y_k$$s, the smallest one say $$y_{min}$$ in absolute value (avoiding in this way any real distance computation with formulas such as $$\sqrt{(...)^2+(...)^2+(...)^2}$$).

2) Then to plug this unique value of $$y_{min}$$ into the parametric equations in order to get $$x_{min}$$ and $$z_{min}$$.

Remark : In the exceptional case where the straight line is parallel to the $$x-z$$ plane :

• either there is an infinite number of solutions (but the previous method can still be applied)

• or there is no solution.

• Thank you for your detailed answer! So if I understand correctly, to find $y_min$ I test the combined equation to find a f($y_k$)=0 closest to 0 by basically trying successively larger values of $y_k$ (and smaller values $y_k$ < 0). At least, that would be a brute force way of doing it. I recently read about Newton's Method and have been experimenting with it. I'm wondering if that is the most appropriate way to efficiently find a solution to this particular type of problem? – JasonCG Jun 5 at 1:15
• The issue is that for a transcendental equation of this type : $y=\sin\left(\alpha y+\beta\right) +\cos\left(\gamma y+\delta\right)$, you don't know "a priori" how many roots you have. I would not advise you to use Newton's method (which is excellent when you are already close to a given root and you want to "refine" it and get quickly say an error $<10^{-10}$ ). Here, I would recommand the following strategy : as the RHS of the equation never takes values outside $[-2;2]$, it means that the roots are all in this interval, therefore if you want say an error $< 10^{-3}$, divide your (ctd...) – Jean Marie Jun 5 at 14:24
• (...ctd) interval $[-2,2]$ into 4000 intervals $[a,b]$ of width $10^{-3}$ and test for each of them whether g(a) has the same sign or not as g(b) where $g(y):=(\sin\left(\alpha y+\beta\right) +\cos\left(\gamma y+\delta\right)) - y$. If it has not the same sign, it means that there is (at least) a root between $a$ and $b$. In this way you are sure not to miss any root, even if there are many of them. – Jean Marie Jun 5 at 14:30