× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS

Finding the point of intersection on a 3D line perpendicular to a target point

Ray Phan
Jul 09, 2015
<p>This will require a bit of mathematics to determine that analytically. By "90 degrees", I'm assuming you want to find the point on this 3D line that would be <strong>perpendicular</strong> to this line if you extended a line from this point of intersection to the desired point. </p> <p>I'm assuming the two points <code>a</code> and <code>b</code> denote the coordinates in 3D space where a line can join them and that <code>c</code> is the point of interest. Here's a better diagram of what I'm talking about:</p> <p><img src="http://mathworld.wolfram.com/images/eps-gif/PointLineDistance3D_1000.gif" alt=""></p> <p><sup>Source: <a href="http://mathworld.wolfram.com/Point-LineDistance3-Dimensional.html" rel="nofollow">MathWorld</a></sup></p> <p>In your case, <code>x1</code> and <code>x2</code> denote <code>a</code> and <code>b</code> in your code and <code>x0</code> denotes <code>c</code>. The distance <code>d</code> would be the distance on the line that would allow the point to be perpendicular to this line if you extended a line from the point of intersection to the point <code>c</code>. </p> <p>You can define a parametric equation that describes the line between <code>x1</code> and <code>x2</code> like so:</p> <p><img src="http://mathworld.wolfram.com/images/equations/Point-LineDistance3-Dimensional/NumberedEquation1.gif" alt=""></p> <p>Here, <code>t</code> is a parameter that varies from <code>[0,1]</code>. Therefore <code>t=0</code> would give you the first point <code>x1</code> or <code>a</code> and <code>t=1</code> would give you the second point <code>x2</code> or <code>b</code>. Any value of <code>t</code> between <code>[0,1]</code> would give you a point along the line. The objective is to find the value <code>t</code> that would <strong>minimize</strong> the distance from <code>x0</code> or <code>c</code> to this line. Like I said before, we all know from geometry that the smallest distance from a point to a line would make the angle of intersection perpendicular / 90 degrees if you extended a line from this point of intersection to the point <code>x0</code> or <code>c</code>.</p> <p>Therefore, all you have to do is find this value of <code>t</code>, then substitute this into the above parametric equation to find your desired point. In other words, we want to minimize the distance between the point and the line, and the distance can be described like so:</p> <p><img src="http://mathworld.wolfram.com/images/equations/Point-LineDistance3-Dimensional/NumberedEquation2.gif" alt=""></p> <p>To find the minimum distance, you'd find the parameter <code>t</code> that minimized the above equation by finding the derivative with respect to <code>t</code> and setting it equal to 0. Logistically, you would take the square root of the equation so that you would be minimizing the distance, and not the distance squared. However, it's actually easier to minimize the distance squared, which is why the equation above is represented like so. It makes sense because if you minimize the distance squared.... the distance would also be minimized as you'd simply just place a square root on the answer to get what you asked for. Eliminating the square root from the equation would make calculating the derivative easier.</p> <p>If you do that, and solve for <code>t</code>, we get this equation:</p> <p><img src="http://mathworld.wolfram.com/images/equations/Point-LineDistance3-Dimensional/NumberedEquation3.gif" alt=""></p> <p>Therefore, find the difference between <code>a</code> and <code>c</code>, take this with the dot product with the difference between <code>b</code> and <code>a</code>, then divide this by the magnitude squared of the difference between <code>b</code> and <code>a</code>. This solves for <code>t</code>, and then you'd substitute this into the above parametric equation to find your point.</p> <p>In MATLAB code, it could look something like this:</p> <pre><code>a = [1 1 2]; %line - x1 b = [20 28 90]; % line - x2 c = [50 30 67]; %point - x0 ab = b - a; %// Find x2 - x1 %// -(x1 - x0).(x2 - x1) / (|x2 - x1|^2) t = -(a - c)*(ab.') / (ab*ab.'); %// Calculate t %// Find point of intersection Xinter = a + (b - a)*t; </code></pre> <p>The code for <code>t</code> I took advantage of using matrix multiplication. The dot product can be found by multiplying a row array by a column array and in a similar fashion, if the row array and column array have the same coefficients, this results in the magnitude squared of a vector.</p> <p>For your example, we get:</p> <pre><code>Xinter = 16.9889 23.7211 76.0539 </code></pre> <p>To show that this is right, let's plot the line, and the point and the point of intersection:</p> <p><img src="http://i.stack.imgur.com/jCIVB.png" alt="enter image description here"></p> <p>The code to produce the above figure is:</p> <pre><code>figure; %// Plot line plot3([a(1) b(1)], [a(2) b(2)], [a(3) b(3)]); hold on; %// Plot point of interest in red plot3(c(1), c(2), c(3), 'r.'); %// Plot intersection point in green plot3(Xinter(1), Xinter(2), Xinter(3), 'g.'); %// Plot line from intersection point to point of interest in black plot3([c(1) Xinter(1)], [c(2) Xinter(2)], [c(3) Xinter(3)], 'k'); %// Turn on a grid grid; </code></pre> <p>This tip was originally posted on <a href="http://stackoverflow.com/questions/31273991/Finding%20the%20point%20of%20intersection%20on%20a%203D%20line%20perpendicular%20to%20a%20target%20point/31274489">Stack Overflow</a>.</p>

Get New Tutorials Delivered to Your Inbox

New tutorials will be sent to your Inbox once a week.

comments powered by Disqus