I've asked this question first on Stack Overflow (How to find position in pixels of intersection of two lines?) but it's actually a math question so I'm asking it here and I'll delete the SO one.
I have $3$ points $A$, $B$ and $C$ and I need to calculate point $D$ in the picture above, so I can draw that shorter line segment. We should have $AC\perp BD$.
It should be simple (high school difficulty), but I don't know how to solve it. Do I need to calculate the line equations that go through two point and then perpendicular line equation that go through a point and then intersection of two lines, or is there easiest way?
It seems that when the ratio is $4:3$ the point is in golden point but if ratio is different the point is in other place.
$\endgroup$4 Answers
$\begingroup$Say $\vert AD\vert=a, \vert AB\vert =b, \vert DC\vert =c$ and $\vert BC\vert =d$. Now, by Pythagoras' theorem $$\vert DB\vert^2=b^2-a^2=d^2-c^2.$$ This implies that $$b^2-d^2=a^2-c^2\\\frac{b^2-d^2}{a+c}=a-c.$$ Now add $a+c$ to both sides and divide by two:$$\frac{b^2-d^2}{2(a+c)}+\frac{a+c}{2}=a.$$ So now we know that $\vert AD\vert = a=\frac{b^2-d^2}{2(a+c)}+\frac{a+c}{2}$. Note that $b$, $d$ and $a+c$ can be derived from the coordinates of $A$, $B$ and $C$.
Can you take it from here?
$\endgroup$ $\begingroup$Here are a couple more ways to solve this problem.
Since $D$ is on the line segment $AC$, we have $D=(1-\lambda)A+\lambda B$ for some $\lambda\in[0,1]$. We want $AC\perp BD$, which can be expressed as $(C-A)\cdot(D-B)=0$. Solve the resulting equation for $\lambda$ and check that $0\le\lambda\le1$ so that $D$ actually falls on the line segment.
Using homogeneous coordinates, the line $\overline{AC}$ is given by $A\times C$. We know that $AC\perp BD$, so from the point-slope equation for the line $\overline{BD}$, $(C-A)\cdot(X-B)=0$, we find that this line is represented by the vector $(x_C-x_A,y_C-y_A,(A-C)\cdot B)$. The intersection of these lines is given by the cross product of these two vectors. A simple range check then tells you whether or not the point thus found is between $A$ and $C$ as required. (N.B.: this use of cross products is equivalent to solving a system of linear equations using Cramer’s rule.)
$\endgroup$ 1 $\begingroup$There are several ways to solve this problem.
Suppose the coordinates for the points $A$, $B$, and $C$ are given by $$ \begin{align*} A &=(a_1,a_2, a_3,\ldots, a_n), \\ B &=(b_1,b_2,b_3,\ldots, b_n), \mbox{ and }\\ C &=(c_1,c_2,c_3,\ldots, c_n). \\ \end{align*} $$ Then the position vectors for these points $A$, $B$, and $C$ are $$ \begin{align*} \vec{\mathcal{O}A} &=\langle a_1,a_2, a_3,\ldots, a_n\rangle , \\ \vec{\mathcal{O}B} &=\langle b_1,b_2,b_3,\ldots, b_n\rangle, \mbox{ and }\\ \vec{\mathcal{O}C} &=\langle c_1,c_2,c_3,\ldots, c_n\rangle, \\ \end{align*} $$ where $\mathcal{O}$ is the origin $(0,0,\ldots, 0)$. Let $$ \begin{align*} \vec{v_1}&=\vec{AC}=\langle c_1-a_1,c_2-a_2,\ldots, c_n-a_n \rangle \mbox{ and } \\ \vec{v_2}&=\vec{AB}=\langle b_1-a_1,b_2-a_2,\ldots, b_n-a_n \rangle. \\ \end{align*} $$ Then the vector obtained by projecting $\vec{AB}$ onto $\vec{AC}$ is given as $$ \text{proj}_{\vec{AC}}\vec{AB} = \text{proj}_{\vec{v_1}}\vec{v_2} = \dfrac{\vec{v_1}\cdot \vec{v_2}}{\vec{v_1}\cdot \vec{v_1}}\vec{v_1} =\vec{AD}. $$ So $\vec{\mathcal{O}D}=\vec{\mathcal{O}A}+\vec{AD}$, which gives the position vector for the point $D$.
$\endgroup$ $\begingroup$Method 1
Let the coordinates be $A(x_1, y_1), B(x_2, y_2), C(x_3, y_3), D(x_4, y_4)$.
If $x_1 = x_3$, $AC$ is vertical and $D \equiv (x_1, y_2)$.
If $y_1 = y_3$, $AC$ is horizontal and $D \equiv (x_2, y_1)$.
If neither is the case, the slope of line $AC$ is given by $m = \dfrac{y_3 - y_1}{x_3 - x_1}$ and equation of (extended) line $AC$ is $\dfrac{y - y_1}{x - x_1} = m \iff y - mx = y_1 - m x_1$.
Since $BD$ is perpendicular to $AC$, the slope of (extended) line $BD$ must be $- \dfrac{1}{m}$ and the equation is given by $y + \dfrac{1}{m} x = y_2 + \dfrac{1}{m} x_2$.
If you solve these two equations, you will get the coordinates of $D$.
That's straightforward method.
Method 2
Assume that $AD:DC = 1:q$. (If $D$ lies outside $AB$, $q$ will turn out to be negative).
Then $D \equiv (x_4, y_4) = (\frac{x_3 + q x_1}{1 + q}, \frac{y_3 + q y_1}{1 + q})$. Since $BD$ is perpendicular to $AC$, we must have
$\dfrac{y_2 - \dfrac{y_3 + q y_1}{1 + q}}{x_2 - \dfrac{x_3 + q x_1}{1 + q}} % = - \dfrac{x_3 - x_1}{y_3 - y_1} \quad \quad \textit{(= negative reciprocal of slope of $AC$)}$
Solve the equation above for $q$, substitute the value back in $(x_4, y_4)$ and get the co-ordinates. Solving the equation is not as hard as it looks.
$\endgroup$ 4