How to generate and plot multiple semicircle with fixed endpoints but different radius

4 visualizzazioni (ultimi 30 giorni)
Hi,
I want to generate a plot with multiple semicircles with fixed endpoints but with different radius...below is the code that I'm using and a figure with the ouput...I want to make the three semicircules to oscillate outwards (like the yellow lines) and inwards (like the red lines) with specific rate...i.e. starting from the first semicircle and moves inwardly to the last one at specific rate (I want to synchronize this motion with some MRI images later on)...I'm still missing the straight line even if I use very high radius...any help would be really appreciated!!!
clc; clear; close all; hold on
%coordinates for semicircle 1
Aa = [11.9; -17.091]; %coordinates of point A
Ba = [13.8; -13.8]; % Same with point B
%coordinates for semicircle 2
Ab = [13.8; -13.8]; %coordinates of point A
Bb = [10; -13.8];
%coordinates for semicircle 3
Ac = [10; -13.8]; %coordinates of point A
Bc = [11.9; -17.091];
Coorda=[Aa Ba];
Coordb=[Ab Bb];
Coordc=[Ac Bc];
Coord=[Coorda,Coordb,Coordc];
F=cell(3,1);
for j=1:3
if j==1
F{j,1}=Coord(:,j:j+1);
elseif j==2
F{j,1}=Coord(:,j+1:j+2);
else
F{j,1}=Coord(:,j+2:j+3);
end
end
for i=1:3
n=0;
for h=1:10
d = norm(F{i,1}(:,1)-F{i,1}(:,2));
R = d/2+n; % Choose R radius >= d/2
C = (F{i,1}(:,2)+F{i,1}(:,1))/2+sqrt(R^2-d^2/4)/d*[0,-1;1,0]*(F{i,1}(:,2)-F{i,1}(:,1)); % Center of circle
a = atan2(F{i,1}(2,1)-C(2),F{i,1}(1,1)-C(1));
b = atan2(F{i,1}(2,2)-C(2),F{i,1}(1,2)-C(1));
b = mod(b-a,2*pi)+a; % Ensure that arc moves counterclockwise
t = linspace(a,b,1000);
x = C(1)+R*cos(t);
y = C(2)+R*sin(t);
plot(x,y,'y-',C(1),C(2),'w*')
axis equal
n=n+0.15;
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%for inwards movement
hold on
%coordinates for semicircle 1
Aa = [13.8; -13.8]; %coordinates of point A
Ba = [11.9; -17.091]; % Same with point B
%coordinates for semicircle 2
Ab = [10; -13.8]; %coordinates of point A
Bb = [13.8; -13.8];
%coordinates for semicircle 3
Ac = [11.9; -17.091]; %coordinates of point A
Bc = [10; -13.8];
Coorda=[Aa Ba];
Coordb=[Ab Bb];
Coordc=[Ac Bc];
Coord=[Coorda,Coordb,Coordc];
F=cell(3,1);
for j=1:3
if j==1
F{j,1}=Coord(:,j:j+1);
elseif j==2
F{j,1}=Coord(:,j+1:j+2);
else
F{j,1}=Coord(:,j+2:j+3);
end
end
for i=1:3
n=1.5;
for h=1:10
d = norm(F{i,1}(:,1)-F{i,1}(:,2));
R = d/2+n; % Choose R radius >= d/2
C = (F{i,1}(:,2)+F{i,1}(:,1))/2+sqrt(R^2-d^2/4)/d*[0,-1;1,0]*(F{i,1}(:,2)-F{i,1}(:,1)); % Center of circle
a = atan2(F{i,1}(2,1)-C(2),F{i,1}(1,1)-C(1));
b = atan2(F{i,1}(2,2)-C(2),F{i,1}(1,2)-C(1));
b = mod(b-a,2*pi)+a; % Ensure that arc moves counterclockwise
t = linspace(a,b,1000);
x = C(1)+R*cos(t);
y = C(2)+R*sin(t);
plot(x,y,'r-',C(1),C(2),'w*')
axis equal
n=n+2;
end
end
Plot-semicircles.jpg

Risposte (1)

Hank
Hank il 13 Nov 2019
Thats a neat looking graphic. I don't really understand what its for though. Why don't you just manually draw in the straight line if you need it, as a line and not a circle.
line(Coorda(1,:),Coorda(2,:),'color','k')
line(Coordb(1,:),Coordb(2,:),'color','k')
line(Coordc(1,:),Coordc(2,:),'color','k')
  2 Commenti
Konstantinos Stamatopoulos
Thanks for your reply. The point is that with the current code there is gap between the last yellow semicircle line and the first red semicircle line. Just drawing a line in between won't solve the issue. Moreover, if I want to generate a video showing the motion of those lines outwards and inwards with specific frequency (like in case of a wave), the current code doesn't help me...so I was looking for an improvement/suggestion.
Thanks
Konstantinos Stamatopoulos
Moreover, I manually changed the coordinates in reverse order in order to get the red coloured semicircles

Accedi per commentare.

Categorie

Scopri di più su Lighting, Transparency, and Shading in Help Center e File Exchange

Prodotti


Release

R2018a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by