matlab - Finding the point of intersection on a 3D line perpendicular to a target point -
i have line , point , wanted find point (x,y,z) on line 90 degrees or perpendicular if draw line point of intersection point.
so far can create line code , have code calculates angle between 3 points, doesn't apply here:
a = [1 1 2]; %line b = [20 28 90]; % line c = [50 30 67]; %point ab = b - a; n = max(abs(ab)) + 1; s = repmat(linspace(0, 1, n)', 1, 3); d = 1:3 s(:, d) = s(:, d) * ab(d) + a(d); end s = round(s); z = 100; n = 100; x = zeros(n, n, z); x(sub2ind(size(x), s(:, 1), s(:, 2), s(:, 3))) = 1; x = c(:,1); clf plot3(s(:, 1), s(:, 2), s(:, 3), 'r.-') axis(n * [0 1 0 1 0 1]) grid on
this require bit of mathematics determine analytically. "90 degrees", i'm assuming want find point on 3d line perpendicular line if extended line point of intersection desired point.
i'm assuming 2 points a , b denote coordinates in 3d space line can join them , c point of interest. here's better diagram of i'm talking about:

source: mathworld
in case, x1 , x2 denote a , b in code , x0 denotes c. distance d distance on line allow point perpendicular line if extended line point of intersection point c.
you can define parametric equation describes line between x1 , x2 so:

a point on line between x1 , x2 can described taking each of (x,y,z) values x1 , x2 , writing in above parametric form , varying parameter t, goes [0,1]. therefore t=0 give first point x1 or a , t=1 give second point x2 or b. value of t between [0,1] give point along line. objective find value t minimize distance x0 or c line. said before, know geometry smallest distance point line make angle of intersection perpendicular / 90 degrees if extended line point of intersection point x0 or c.
therefore, have find value of t, substitute above parametric equation find desired point. in other words, want minimize distance between point , line, , distance can described so:

to find minimum distance, you'd find parameter t minimized above equation finding derivative respect t , setting equal 0. logistically, take square root of equation minimizing distance, , not distance squared. however, it's easier minimize distance squared, why equation above represented so. makes sense because if minimize distance squared.... distance minimized you'd place square root on answer asked for. eliminating square root equation make calculating derivative easier.
if that, , solve t, equation:

therefore, find difference between a , c, take dot product difference between b , a, divide magnitude squared of difference between b , a. solves t, , you'd substitute above parametric equation find point.
in matlab code, this:
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 = + (b - a)*t; the code t took advantage of using matrix multiplication. dot product can found multiplying row array column array , in similar fashion, if row array , column array have same coefficients, results in magnitude squared of vector.
for example, get:
xinter = 16.9889 23.7211 76.0539 to show right, let's plot line, , point , point of intersection:

the code produce above figure is:
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 intersection point point of interest in black plot3([c(1) xinter(1)], [c(2) xinter(2)], [c(3) xinter(3)], 'k'); %// turn on grid grid;
Comments
Post a Comment