R2021aを使用して、直線と円弧の交点を求める関数はありますか?
18 views (last 30 days)
Show older comments
R2021aを使用し、直線から円弧の外側のライン上を順に移動する円の交点を求める方法を教えてください。
A点からA’点に円が移動するとき、円の交点BやB’の交点、さらにC点やC’点を求める関数や方法はありますか?
3 Comments
Atsushi Ueno
on 29 Jul 2021
Mapping Toolboxが目的に合致する関数を持っています。
あと類似の質問にFile ExchangeのInterX関数が多数挙がっています。
Accepted Answer
Atsushi Ueno
on 29 Jul 2021
Edited: Atsushi Ueno
on 3 Aug 2021
を使いました。変数stepの値とpause(0.05)の時間を変更すれば、変化の粗さや表示速度を調整できます。
BとCは同心円なので単に角度から対応する座標を求めました。
step = 5; llen = 20;
xb = 0; yb = 0; rb = 12;
xc = 0; yc = 0; rc = 8;
xa = rb; ya = 0; ra = 15;
figure;
for ya = -llen:step:0 % 直線部
draw_path(xb,yb,rb,xc,yc,rc,llen);
draw_intersec(xa,ya,ra,xb,yb,rb,xc,yc,rc);
end
for th = 0:step/rb:pi % 円弧部
xa = xb + rb * cos(th);
ya = yb + rb * sin(th);
draw_path(xb,yb,rb,xc,yc,rc,llen);
draw_intersec(xa,ya,ra,xb,yb,rb,xc,yc,rc);
end
xa = -rb;
for ya = 0:-step:-5 % 直線部
draw_path(xb,yb,rb,xc,yc,rc,llen);
draw_intersec(xa,ya,ra,xb,yb,rb,xc,yc,rc);
end
%% 以下はサブルーチン化した関数
function draw_path(xb,yb,rb,xc,yc,rc,llen) % 軌跡を描く
arc(xb,yb,rb,1,'--');
xlim([-30 30]); ylim([-35 30]); xlim manual; ylim manual; hold on;
arc(xc,yc,rc,1,'--');
line([xb+rb xb+rb],[yb yb-llen],'LineStyle','--');
line([xb-rb xb-rb],[yb yb-llen],'LineStyle','--');
line([xc+rc xc+rc],[yc yc-llen],'LineStyle','--');
line([xc-rc xc-rc],[yc yc-llen],'LineStyle','--');
end
function draw_intersec(xa,ya,ra,xb,yb,rb,xc,yc,rc) % 円と交点を描く
[crs1x,crs1y] = circcirc(xb,yb,rb,xa,ya,ra); % 外側経路の円弧部(円B)と円Aの交点
[crs2x,crs2y] = linecirc(inf,-rb,xa,ya,ra); % 外側経路の左側直線部と円Aの交点
if (crs1x(1) < 0) && (crs1y(1) > 0)
crs1x(2) = []; crs1y(2) = [];
crs2x(crs2y>0) = []; crs2y(crs2y>0) = []; % 線対円: y座標が負の交点に絞る(NaNは残す)
[crs2y index] = sort(crs2y,'descend');
crs2x = crs2x(index); % 線対円: y座標の降順でソート (NaNは残す)
else
crs1x(crs1y<0) = []; crs1y(crs1y<0) = []; % 円対円: y座標が正の交点に絞る(NaNは残す)
crs2x(crs2y>0) = []; crs2y(crs2y>0) = []; % 線対円: y座標が負の交点に絞る(NaNは残す)
[crs1x index] = sort(crs1x,'ascend');
crs1y = crs1y(index); % 円対円: x座標の昇順でソート (NaNは残す)
[crs2y index] = sort(crs2y,'descend');
crs2x = crs2x(index); % 線対円: y座標の降順でソート (NaNは残す)
end
if isempty(crs2x) || any(isnan(crs2x)) % 線対円がNaN or y<=0の交点が無い
crsxb = crs1x; crsyb = crs1y; % 円対円の交点を採用(NaNでもOK)
else
crsxb = crs2x; crsyb = crs2y; % 線対円の交点を採用(NaNでもOK)
end
theta = atan2(crsyb,crsxb); % 円B上の交点の角度を求める
crsxc = xc + rc * cos(theta); % 円c上の交点を上記角度から求める
crsyc = yc + rc * sin(theta);
arc(xa,ya,ra,2,'-'); % 円Aを描画
plot(crsxb,crsyb,'*'); % 円B上の交点を描画
plot(crsxc,crsyc,'+'); % 円C上の交点を描画
line([xa crsxb],[ya crsyb]); % 円Aの中心と円B上の交点を線で結ぶ
pause(0.05); hold off;
end
function arc(x,y,r,rsw,linestyle) % rsw=1:半円を描く、rsw=2:円を描く
ang=0:0.01:rsw*pi;
xp=r*cos(ang);
yp=r*sin(ang);
plot(x+xp,y+yp,linestyle);
end
8 Comments
More Answers (1)
HO
on 30 Jul 2021
Edited: HO
on 30 Jul 2021
4 Comments
Atsushi Ueno
on 2 Aug 2021
>最初はA点もB点も直線上にいるため値が引数がないと言う理解で合っているでしょうか?
多分あってます。交点が無ければlinecircやcirccirc関数はNaNを返すので値が空になります
See Also
Categories
Find more on Traveling Salesman (TSP) 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!