Given three points of vertices of a triangle, how would I compute the closest point to the origin

1.3k Views Asked by At

Note: I am an Engineering Undergrad with High School knowledge of Geometry

I am given three points in a Cartesian Co-ordinate System, how would I proceed to find the point closest to the origin on the perimeter of the triangle. A PseudoCode would always be appreciated, but even pointing me to the right direction would be sufficient.

6

There are 6 best solutions below

1
On

Find out the equations of the sides of triangle Then find equations of normal to these sides passing through the origin. Find the intersection pts of the normal and the edges. The distance of the closest intersection point is your answer

4
On

Set each straight line in the polar/pedal form $$ x \cos \alpha + y \sin \alpha = p$$

Let $P$ be the origin.In order to find three pedal distances to the vertices $$ p_{AB},p_{BC},p_{CA}$$

We have three distances to the vertices $$ {AP},{BP},{CP} $$

enter image description here

The nearest is the minimum of these six distances to vertices as well as sides (blue and red):

$$Min[{AP},{BP},{CP},p_{AB},p_{BC},p_{CA}] $$

2
On

The closest point may often be a vertex, unless there is a point on one of the edges where the line from the origin to that point is perpendicular to the edge. [Note that the latter occurs with all three edges when the origin is inside the triangle.] If the edge makes angle $\alpha$ with the $x$-axis (you can figure this out, of course, just from the coordinates of the endpoints), then you look at the angles the line from the origin to both vertices makes with the $x$-axis: If one is less than $\alpha+\pi/2$ (in radians) and the other is greater than $\alpha+\pi/2$, then somewhere in between is the closest point, on that edge.

Of course, this is probably more complicated than necessary. Drop a perpendicular from the origin to each of the three lines formed by the edges. If that perpendicular lands along the actual edge (and not outside it), that is a candidate closest point. Compare with other such and with the three vertices. If none of them does, check the three vertices.

0
On

A point on a segment between $P_1,P_2$ will be given by $P(\lambda)=\lambda P_1 + (1-\lambda)P_2$ with $0 \le \lambda \le 1$ .

So, to automize the job, minimize $|P(\lambda)|\quad |\ 0 \le \lambda \le 1$ for the three edges.
Due to the limited range for lambda you can have that the local minimum be outside that range: in that case it will be the vertex closer to the local minimum.

0
On

The locus approach:

The origin point can be closer to one of the three edges or one of the three vertices. In case of an edge, the closest point is the projection onto it.

The diagram below shows the "basins of attraction" of those six elements. The limits are made of the six normals to the edges at the vertices, and the three angle bissectors. There are six different regions, so that three comparison tests is an absolute minimum (three tests can separate at most eight cases). But here we need more because the limits don't have a single equation.

A possible scheme is to compare the origin against the three bissectors, which tells you an edge and its two endpoints, then against the two normals to the endpoints of this edge. By this approach, you have to evaluate at most five line equations*; more precisely the independent coefficient $c$ of $ax+by+c=0$, as only the sign of the origin matters.

enter image description here

*The initial classification wrt the bissectors will conclude after two or three comparisons, and the final edge/vertex classification takes one or two comparisons. You only compute the line equations on demand.

0
On

This calculation procedure is based on what @Vin has suggested in his answer. I doubt that Vin's answer meets the requirements of the OP. My intention here is to show the OP its computational complexity, which he thought intense. Therefore, please don't consider this as an answer to the original question posed by the OP.

Let $P_n=\left(x_n, y_n\right)$, where $n=1,2,3$. Define the sides of the triangle, which has $P_n$ as its vertices, as $L_1=P_1P_2$, $L_2=P_2P_3$, and $L_3=P_3P_1$. We assume that the equation of a line $L_n$ is given in the form $y=m_nx+c_n$. We need to compute $m\space$s and $c\space$s using the following equations. $$ \begin{matrix} L_1: & m_1=\frac{y_1-y_2}{x_1-x_2}, &\space & c_1=y_1- m_1x_1 \\ L_2: & m_2=\frac{y_2-y_3}{x_2-x_3}, &\space & c_2=y_2- m_2x_2 \\ L_3: & m_3=\frac{y_3-y_1}{x_3-x_1}, &\space & c_3=y_3- m_3x_3 \\ \end{matrix} $$

Next in our agenda is the determination of the perpendicular distances (i.e. $d_1, d_2,$ and $d_3$) to the three sides from the origin (0,0). It can be done by using the following formulae. $$d_1=\left|\frac{c_1}{\sqrt{1+m_1^2}}\right|$$ $$d_2=\left|\frac{c_2}{\sqrt{1+m_2^2}}\right|$$ $$d_3=\left|\frac{c_3}{\sqrt{1+m_3^2}}\right|$$

Find the smallest of the three $d\space$s. Assume that it turns out to be $d_k$, where $k$ is either 1, 2, or 3. Then, the coordinates of the closest point to the orgin are given by the following formula. $$P_\mathrm{closest}=\left(\frac{c_k}{1+m_k^2},\space-\frac{m_kc_k}{1+m_k^2}\right)$$