Changing Vector Plot to Curve - Please HELP!
Show older comments
I am looking to calculate the area swept by h{13}. How do I change h{14} and h{15} to be able to find the area between two curves? Once they are changed, how can I calculate both the area and fill the area between the two curves?
hold on
axis ([-20 20 -20 20])
axis off
P1=[-11,0];
P2=[-1,0];
h{1}=plot([P1(1) P2(1)],[P1(2) P2(2)],'LineWidth',5,'Color','black');
x=5*pi/8;
b=0:pi/40:x;
P7vct=nan(numel(b),2);
for k=1:numel(b)
Z=[4,0];
h{2}=viscircles(Z,5,'LineWidth',2,'LineStyle','--','Color','black');
A=[0,0];
h{3} = viscircles(A,1,'LineWidth',2,'Color','black');
PA=A+[cos(b(k)-(pi)),sin(b(k)-(pi))];
h{4}=viscircles(PA,.1,'Color','black');
B = [4-4*cos(2*asin(.25)),-4*sin(2*asin(.25))];
h{5} = viscircles(B,1,'LineWidth',2,'Color','green');
PB=B+[cos(-b(k)-(pi/2)),sin(-b(k)-(pi/2))];
h{6}=viscircles(PB,.1,'Color','green');
C = [4-4*cos(4*asin(.25)),-4*sin(4*asin(.25))];
h{7} = viscircles(C,1,'LineWidth',2,'Color','blue');
PC=C+[cos(b(k)-(pi/2)),sin(b(k)-(pi/2))];
h{8}=viscircles(PC,.1,'Color','blue');
D = [4-4*cos(6*asin(.25)),-4*sin(6*asin(.25))];
h{9} = viscircles(D,1,'LineWidth',2,'Color','red');
PD=D+[cos(-b(k)-(pi/2)),sin(-b(k)-(pi/2))];
h{10}=viscircles(PD,.1,'Color','red');
E = [4-4*cos(8*asin(.25)),-4*sin(8*asin(.25))];
h{11} = viscircles(E,1,'LineWidth',2,'Color','yellow');
PE=E+[cos(b(k)-(pi/2)),sin(b(k)-(pi/2))];
h{12}=viscircles(PE,.1,'Color','yellow');
P3=B+[cos(b(k)),sin(b(k))];
P4=C+[cos(b(k)),sin(b(k))];
P5=D+[cos(b(k)),sin(b(k))];
P6=E+[cos(b(k)-x),sin(b(k)-x)];
P7=E+[11*cos(b(k)-x),11*sin(b(k)-x)];
P6vct(k,:)=P6;
P7vct(k,:)=P7;
h{13} = plot([P6(1) P7(1)],[P6(2) P7(2)],'LineWidth',5,'Color','black');
h{14}=plot(P6vct(:,1),P6vct(:,2),'LineWidth',3,'Color','black');
h{15}=plot(P7vct(:,1),P7vct(:,2),'LineWidth',3,'Color','black');
drawnow();
pause(0.001);
if k<numel(b)
delete(vertcat(h{2:13}));
end
end
hold off
axis ([-20 20 -20 20])
axis off
Answers (1)
Dev
on 27 Mar 2025
By calculating the area swept by h{13}, I am assuming that we aim to calculate the area covered by the h{13} arm when it moves around the circumference of the h{14} arc.
To achieve the same, we can use the ‘polyarea’ function in MATLAB along with the ‘flipud’ function. For a detailed usage of these functions, please refer the documentation links below-
I have also attached a reference code snippet which uses both these functions below.
% Calculate the area between the two curves
area = polyarea([P6vct(:,1); flipud(P7vct(:,1))], [P6vct(:,2); flipud(P7vct(:,2))]);
Next, to fill the area swept, we can use the ‘fill’ function in MATLAB. Below is a reference code snippet which fills in the area swept with a ‘cyan’ colour.
% Fill the area between the two curves
fill([P6vct(:,1); flipud(P7vct(:,1))], [P6vct(:,2); flipud(P7vct(:,2))], 'cyan', 'FaceAlpha', 0.5);
More information on the same can be found below-
I have also attached the output plot received after using the above function for your reference below-

Here, the area swept by the arc h{13} (green) on the curve h{14} (black) is filled with ‘cyan’ colour.
I hope the above approach solves the purpose.
Categories
Find more on Polar 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!