- Define "midpoint". Is this a median? mean? center of mass?
- In your quesiton, I'm assuming C is a scalar value (not a vector but a single data point). So how is that combined with A in D= mid of (A,C)?
How can i write recursive function using midpoint equation 3d coordinate system?
10 views (last 30 days)
Show older comments
A,B are 2 inputs.with this 2 inputs i want to generate n number of midpoints
A = [2 2 2];
B = [13 13 13];
c = mid of (A,B);
D = mid of (A,C);
E = mid(C,B);
how can i write this program in recursive function to calculate n number of midpoints...
and each midpoint distance is>1
1 Comment
Adam Danz
on 4 Dec 2019
Edited: Adam Danz
on 6 Dec 2019
Your examples of inputs A and B are helpful but please provide an example of expected outputs so we have a complete picture.
----[update after your question-edit]----
By 'midpoint' do you mean you want the (x,y,z) coordinate of the midpoint between the line whose endpoints are A and B?
Accepted Answer
Adam Danz
on 4 Dec 2019
Edited: Adam Danz
on 5 Dec 2019
This solution creates an anonymous function midPointFcn() that receives two inputs A and B which are both (x,y,z) coordinates of a line. The output are the 3 midpoints from your question: C, D, & E.
Then I plot the results for confirmation.
See inline comments for more detail
% INPUTS: (x,y,z) endpoints of a line AB
A = [2 2 2];
B = [13 13 13];
% Create midpoint function
% Inputs A and B are 1x3 (x,y,z) coordinates.
% Output is 3x3 where columns are (x,y,z) coordinates.
% Row 1 is the mid point of AB
% Row 2 is the mid points between A and midpoint(AB)
% Row 3 is the mid points between B and midpoint(AB)
midPointFcn = @(A,B)[mean([A;B]); mean([A;mean([A;B])]); mean([B;mean([A;B])])];
% Compute midpoints C,D,&E
CDE = midPointFcn(A,B);
% Plot results
clf()
plot3([A(1),B(1)],[A(2),B(2)],[A(3),B(3)],'k-o','DisplayName', 'Line AB')
grid on
hold on
plot3(CDE(1,1),CDE(1,2),CDE(1,3),'m*','DisplayName','Midpoint C')
plot3(CDE(2,1),CDE(2,2),CDE(2,3),'b*','DisplayName','Midpoint D')
plot3(CDE(3,1),CDE(3,2),CDE(3,3),'r*','DisplayName','Midpoint E')
legend()
4 Comments
Adam Danz
on 9 Dec 2019
A recursive function is just a function that calls itself. Check out tue midPointFcn function in my answer. it just receives two coordinates A and B and determines the midpoint between A and B. If you want that to be recursive, you could create a while-loop that does the following
- It starts with the two end points A and B as the initial inputs and produces C which is the midpoint of AB.
- Then the new inputs are A and C which finds the midpoint D of AC.
- Then the new inputs are A and D etc.....
- After each midpoint calculation, you calculate the distance and when it falls below threshold. you stop.
- But then you have to do the same thing from the other end where the inputs are (B,C) etc...
If you get stuck, show us how far you got and we can help you get unstuck.
More Answers (1)
See Also
Categories
Find more on 2-D and 3-D Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!