Polarplot - plot arc between points instead of line
显示 更早的评论
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.
采纳的回答
更多回答(1 个)
chicken vector
2023-4-27
编辑:chicken vector
2023-4-27
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:

类别
在 帮助中心 和 File Exchange 中查找有关 Discrete Data Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

