Polarplot - plot arc between points instead of line
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Tomas Brezina
il 27 Apr 2023
Commentato: Star Strider
il 27 Apr 2023
Hi,
I would like to plot line plot in polar plot, but to have arcs between points instead of straight lines. Like for example, if I have two points with same radius, the line between them shouldn't be straight, but an arc, with the radius same as the two points. If they dont have same radius, it should lineary interpolate separately radius and angle, instead of x and y coordinates.
0 Commenti
Risposta accettata
Star Strider
il 27 Apr 2023
Define the two points you want, crate an angle vector connecting them, and then interpolate to create the radius vector —
points_r = rand*[1;1];
points_a = rand(2,1)*2*pi;
a_vct = linspace(points_a(1), points_a(2));
r_vct = interp1(points_a, points_r, a_vct);
figure
polarplot(points_a, points_r, 'pm')
hold on
polarplot(a_vct, r_vct, '-b')
hold off
title('Points With Equal Radii')
points_r = rand(2,1);
points_a = rand(2,1)*2*pi;
[a_vct,r_vct] = interp_line(points_a,points_r);
figure
polarplot(points_a, points_r, 'pm')
hold on
polarplot(a_vct, r_vct, '-b')
hold off
title('Points With Different Radii')
function [a_vct,r_vct] = interp_line(points_a,points_r)
a_vct = linspace(points_a(1), points_a(2));
r_vct = interp1(points_a, points_r, a_vct);
end
It would be straightforward to write a function to do the interpolation, so in the second plot, I did just that.
.
2 Commenti
Più risposte (1)
chicken vector
il 27 Apr 2023
Modificato: chicken vector
il 27 Apr 2023
point1 = [3, 4];
point2 = [10, 0];
radius = 6;
dir = 'Up';
nPoints = 50;
[x, y, c] = getArc(point1, point2, radius, dir, nPoints);
figure;
grid on;
hold on;
scatter(point1(1), point1(2), 50, 'k', 'filled')
scatter(point2(1), point2(2), 50, 'k', 'filled')
plot(x, y, 'r', 'LineWidth', 2)
scatter(c(1), c(2), 50, 'x', 'r')
plot([c(1) point1(1)], [c(2) point1(2)], 'k', 'LineWidth', 1, 'LineStyle', ':')
plot([c(1) point2(1)], [c(2) point2(2)], 'k', 'LineWidth', 1, 'LineStyle', ':')
dir = 'Down';
[x, y, c] = getArc(point1, point2, radius, dir, nPoints);
plot(x, y, 'b', 'LineWidth', 2)
scatter(c(1), c(2), 50, 'x', 'b')
plot([c(1) point1(1)], [c(2) point1(2)], 'k', 'LineWidth', 1, 'LineStyle', ':')
plot([c(1) point2(1)], [c(2) point2(2)], 'k', 'LineWidth', 1, 'LineStyle', ':')
hold off;
axis equal;
xlim([0 12])
ylim([-3 7])
function [x, y, c] = getArc(point1, point2, radius, dir, nPoints)
midPoint = (point2 - point1) / 2;
a = sqrt(sum(midPoint.^2));
b = sqrt(radius^2 - a^2);
xC12 = point1(1) + midPoint(1) + [1 -1] * b * midPoint(2) / a;
yC12 = point1(2) + midPoint(2) + [-1 1] * b * midPoint(1) / a;
[yC, idx] = sort(yC12);
xC = xC12(idx);
if ~diff(yC)
xC = sort(xC);
end
c = zeros(1,2);
switch lower(dir)
case 'up'
c(1) = xC(2);
c(2) = yC(2);
th1 = atan2(point1(2) - c(2), point1(1) - c(1));
th2 = atan2(point2(2) - c(2), point2(1) - c(1));
case 'down'
c(1) = xC(1);
c(2) = yC(1);
th1 = atan2(point2(2) - c(2), point2(1) - c(1));
th2 = atan2(point1(2) - c(2), point1(1) - c(1));
otherwise
error("Use ''Up'' or ''Down'' for centre direction.")
end
th = linspace(th1, th2, nPoints);
x = radius * cos(th) + c(1);
y = radius * sin(th) + c(2);
end
Result:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1367694/image.png)
0 Commenti
Vedere anche
Categorie
Scopri di più su Interpolation in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!