Draw an arc between two points --> (x1,y1,z1) and (x2,y2,z2)
21 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Huseyin Eldem
am 23 Feb. 2013
Kommentiert: Esther
am 8 Apr. 2024
Hi. I have several points on the sphere. I want to combine these points with arc. I can draw these points with lines but I can't draw arcs Please help
0 Kommentare
Akzeptierte Antwort
Youssef Khmou
am 23 Feb. 2013
Bearbeitet: Youssef Khmou
am 23 Feb. 2013
hi, you can try drawing a circle but restrict it into certain region,
I started a code, try to adjust/enhance it :
% Draw an arc between two points
p1=[-2 -2 -3];
p2=[10 15 20];
% Circle x²+y²+z²=Constant
%=> z=sqrt(Constant-x²-y²-);
Center=((p2+p1))./2;
xc=Center(1);
yc=Center(2);
zc=Center(3);
% Radius
R=norm((p2-p1))/2;
%Min=min(min(p1,p2));
%Max=max(max(p1,p2));
%x=linspace(Min,Max,20);
%y=linspace(Min,Max,20);
x=linspace(p1(1),p2(1),200);
y=linspace(p1(2),p2(2),200);
z=zc+sqrt((R^2)-((x-xc).^2)-((y-yc).^2));
figure, plot3(x,y,z), grid on, hold on
plot3(p1(1),p1(2),p1(3),'*','MarkerSize',10),
plot3(p2(1),p2(2),p2(3),'*','MarkerSize',10),
hold on, plot3(xc,yc,zc,'*','MarkerSize',10)
Weitere Antworten (5)
Huseyin Eldem
am 24 Feb. 2013
2 Kommentare
Youssef Khmou
am 24 Feb. 2013
Bearbeitet: Youssef Khmou
am 24 Feb. 2013
hi Huseyin, then you didnt have to accept the answer because the problem is not solved yet , in the code above we used circle's equation, so to draw arc between 200 points, theirs coordinates must satisfy :
1<=i,j,k <=200 xi²+yi²+zi²=R²
IF the points do not satisfy the equation they dont BELONG to the arc :
ok lets try to draw an arc between 200 points : we generalize the above code :
clear, clc
p=round(10*randn(200,3));
% Circle x²+y²+z²=Constant
%=> z=sqrt(Constant-x²-y²-);
Center=((sum(p)))./2;
xc=Center(1);
yc=Center(2);
zc=Center(3);
% Radius
p2=max(p);
p1=min(p);
R=norm((p2-p1))/2;
%Min=min(min(p1,p2));
%Max=max(max(p1,p2));
%x=linspace(Min,Max,20);
%y=linspace(Min,Max,20);
x=linspace(p1(1),p2(1),200);
y=linspace(p1(2),p2(2),200);
z=zc+sqrt((R^2)-((x-xc).^2)-((y-yc).^2));
figure, plot3(x,y,z), grid on, hold on
plot3(p(:,1),p(:,2),p(:,3),'.')
As points do not satisfy the equation the line cant contain them all .
ChristianW
am 25 Feb. 2013
Bearbeitet: ChristianW
am 25 Feb. 2013
function test_sphere
n = 200;
THETA = rand(1,n)*2*pi;
PHI = rand(1,n)*2*pi;
R = 1.1*ones(1,n);
[x,y,z] = sph2cart(THETA,PHI,R);
acc = 10; % accuracy: ~ lines per arc
V = track_arc(x,y,z,acc);
plot3(x,y,z,'.r'); axis([-1 1 -1 1 -1 1]); axis square; hold on
plot3(V(1,:),V(2,:),V(3,:),'g')
sphere(31)
colormap([1 1 1]*0.1)
function V = track_arc(x,y,z,acc)
v1 = [x(1:end-1);y(1:end-1);z(1:end-1)]; % Vector from center to 1st point
v2 = [x(2:end);y(2:end);z(2:end)]; % Vector from center to 2nd point
r = sqrt(sum([x(1); y(1); z(1)].^2,1));
v3a = cross(cross(v1,v2),v1); % v3 lies in plane of v1 & v2 and is orthog. to v1
v3 = r*v3a./repmat(sqrt(sum(v3a.^2,1)),3,1); % Make v3 of length r
% Let t range through the inner angle between v1 and v2
tmax = atan2(sqrt(sum(cross(v1,v2).^2,1)),dot(v1,v2));
V = zeros(3,sum(round(tmax*acc))); % preallocate
k = 0; % index in v
for i = 1:length(tmax)
steps = round(tmax(i)*acc)+1; %Edited +1
k = (1:steps) + k(end);
t = linspace(0,tmax(i),steps);
V(:,k) = v1(:,i)*cos(t)+v3(:,i)*sin(t);
end
For the shortest great circle path, I modified code from Roger Stafford: http://www.mathworks.com/matlabcentral/newsreader/view_thread/277881
Huseyin Eldem
am 25 Feb. 2013
1 Kommentar
Youssef Khmou
am 25 Feb. 2013
hi, they numbers need more processing to convert them into Mat format, anyway try this with numbers :
X=reshape(x,20,10);
Y=reshape(y,20,10);
Z=reshape(z,20,10);
figure, surf(X,Y,Z), shading interp
If they satisfy the equation, them they must give a partial Sphere
...
Siehe auch
Kategorien
Mehr zu Surface and Mesh Plots finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!