minimum distance when a circle is beetween two points
3 views (last 30 days)
Show older comments
Hello,
I have a question, I have the situation like figure below, a circle and two points A and B. I want to go from point A to point B, I want a code that shows if we have such situations do : first go to A1 (which is the tangent point) then move across the circumference of the circle upto point B1, then go to point B.
How can I show this (Red) path in matlab and calculate the distance?
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/250970/image.jpeg)
0 Comments
Answers (3)
Image Analyst
on 27 Nov 2019
Simply get the x and y of the line using simple analytical equations (geometry, algebra) using the known coordinates of the center, point A, point B, and the known radius), then use plot:
plot(x, y, 'r-', 'LineWidth', 5); % Plot thick red curve.
To get the distance, you could just sum x and y segment lengths
distance = sum(sqrt(x(2:end) - x(1:end-1)) .^ 2 + (y(2:end) - y(1:end-1) .^ 2);
if you want MATLAB to do it quantitatively. Of course there is also an analytical equation using the pure mathematical equations of the lines and curve.
5 Comments
Image Analyst
on 30 May 2020
OK, how can we help?
By the way, this is what your code does:
Unrecognized function or variable 'roller_positions'.
Error in test3 (line 1)
center = roller_positions(:, cnt_arcs+1);
Veronica Spelbrink
on 30 May 2020
Edited: Veronica Spelbrink
on 31 May 2020
If you're willing to help the function that computes the whole trajectory and the variables to load into the workspace are attached. plot_rollers.m is just a secondary function that plots the line and dashed line circles. You can run the trajectory.m with the inputs like so:
trajectory(circles, shapes, rt, plane_depth);
however the section of the code that I need help with is in the trajectory.m file, from line 76 to 113, which is sort of what I referenced in my first comment, where the curved segments are computed.
UPDATE: I had to change the way I was doing it to choose whether I wanted to draw the curve from point 1 to point 2 clockwise or Anti-clockwise. And now it should work. Since the code above is slightly deprecated as it wasn't functioning correctly, I will leave here the correct solution as an answer once I have it. It might be of help to someone.
Veronica Spelbrink
on 31 May 2020
function [X, Y, Z] = arc()
A = [1.66870803851856; 0.500000000000000; 1.22112689094119];
A1 = [0.791819441598963; 0.5; 1.03409208510982]; %your points belonging to the circle ircumference go here
B1 = [0.720451021787587; 0.5; 0.859809300488578];
B = [0.969580132133386;0.500000000000000;0.482295106876854];
r = 0.114; %your circle radius here
center = [0.8156; 0.5; 0.9226]; %your circle center here
winding_direction = 1; %direction in which you want your line to wrap around the circle, clockwise (1) or conter-clockwise (0)
xc = center(1);
yc = center(2);
zc = center(3);
%Separate the X, Y and Z components for easy plotting
P = []; %final path
Points =[A A1 B1 B];
% compute line between A and A1
x_l = linspace(A(1), A1(1), 200);
y_l = linspace(A(2), A1(2), 200);
z_l = linspace(A(3), A1(3), 200);
p_line = [x_l; y_l; z_l];
P = cat(2, P, p_line);
%compute the arc between A1 and B1
if (winding_direction == 1) %clockwise rotation
a = atan2(A1(3) - zc, A1(1) - xc);
b = atan2(B1(3) - zc, B1(1) - xc);
b = mod(b-a, 2*pi)+a; % Ensure that arc moves cw
elseif (winding_direction == 0) %counter-clockwise rotation
a = atan2(A1(3) - zc, A1(1) - xc);
b = atan2(B1(3) - zc, B1(1) - xc);
b = mod(b-a, 2*pi)+a-2*pi; % Ensure that arc moves ccw
end
t = linspace(a, b, 1000);
x_a = xc + r*cos(t);
y_a = repmat(yc, 1, 1000);
z_a = zc + r*sin(t);
p_arc = [x_a; y_a; z_a];
P = cat(2, P, p_arc);
% compute line between B1 and B
x_l = linspace(B1(1), B(1), 200);
y_l = linspace(B1(2), B(2), 200);
z_l = linspace(B1(3), B(3), 200);
p_line = [x_l; y_l; z_l];
P = cat(2, P, p_line)
num_p = size(P, 2);
X = zeros(1, num_p);
Y = zeros(1, num_p);
Z = zeros(1, num_p);
for q = 1:num_p
X(1, q) = P(1, q);
Y(1, q) = P(2, q);
Z(1, q) = P(3, q);
end
plot3(X, Y, Z, 'r-'), grid on, hold on, axis square;
plot3(Points(1,:), Points(2,:), Points(3,:), 'o', 'MarkerFaceColor', 'black');
view(180,0);
hold off
end
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!