Polarplot - plot arc between points instead of line
Afficher commentaires plus anciens
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.
Réponse acceptée
Plus de réponses (1)
chicken vector
le 27 Avr 2023
Modifié(e) : chicken vector
le 27 Avr 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:

Catégories
En savoir plus sur Discrete Data Plots dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

