Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Line between two points on a sphere

Subject: Line between two points on a sphere

From: Els

Date: 29 Mar, 2010 08:12:22

Message: 1 of 19

I have two points on a sphere (x1,y1,z1) and (x2,y2,z2).
Now I want to plot a straight line over the surface of the sphere between these point.

The radius and the center of the sphere are also known.

What should be the equation of this line? And how do I plot this?
Should I solve the great distance equation? Because this will only give me the length and not the line itsself.

Subject: Line between two points on a sphere

From: Roger Stafford

Date: 29 Mar, 2010 17:05:27

Message: 2 of 19

"Els " <y.e.t.reeuwijk@student.utwente.nl> wrote in message <hopnd6$19c$1@fred.mathworks.com>...
> I have two points on a sphere (x1,y1,z1) and (x2,y2,z2).
> Now I want to plot a straight line over the surface of the sphere between these point.
>
> The radius and the center of the sphere are also known.
>
> What should be the equation of this line? And how do I plot this?
> Should I solve the great distance equation? Because this will only give me the length and not the line itsself.
----------------
  I presume when you say "straight line over the surface of the sphere", you actually mean the shortest great circle path over the sphere. Here is one way to plot it. Let (x0,y0,z0) be the center of the sphere.

 v1 = [x1-x0;y1-y0;z1-z0]; % Vector from center to 1st point
 r = norm(v1); % The radius
 v2 = [x2-x0;y2-y0;z2-z0]; % Vector from center to 2nd point
 v3 = cross(cross(v1,v2),v1); % v3 lies in plane of v1 & v2 and is orthog. to v1
 v3 = r*v3/norm(v3); % Make v3 of length r
 % Let t range through the inner angle between v1 and v2
 t = linspace(0,atan2(norm(cross(v1,v2)),dot(v1,v2)));
 v = v1*cos(t)+v3*sin(t); % v traces great circle path, relative to center
 plot3(v(1,:)+x0,v(2,:)+y0,v(3,:)+z0) % Plot it in 3D

  This will work for any two points (x1,y1,z1) and (x2,y2,z2) on the sphere except for a diametrically opposite pair. (The norm of v3 in that case would be zero and couldn't be normalized.)

Roger Stafford

Subject: Line between two points on a sphere

From: Els

Date: 29 Mar, 2010 20:48:05

Message: 3 of 19

Wow, Roger, thanks a lot. This is exactly what I was hoping for.
And your comments were very informative, I completely understand what you did.
Thanks again, you are the hero of my day.....

Subject: Line between two points on a sphere

From: Vinita

Date: 24 Jul, 2012 10:56:06

Message: 4 of 19

Hi Roger,

What if I want to plot the euclidean line between two points on a sphere (and not on the arc of the sphere) --
http://www.clintoncurry.net/math/images/rabbit_euclidean.png

"Roger Stafford" wrote in message <hoqmkn$871$1@fred.mathworks.com>...
> "Els " <y.e.t.reeuwijk@student.utwente.nl> wrote in message <hopnd6$19c$1@fred.mathworks.com>...
> > I have two points on a sphere (x1,y1,z1) and (x2,y2,z2).
> > Now I want to plot a straight line over the surface of the sphere between these point.
> >
> > The radius and the center of the sphere are also known.
> >
> > What should be the equation of this line? And how do I plot this?
> > Should I solve the great distance equation? Because this will only give me the length and not the line itsself.
> ----------------
> I presume when you say "straight line over the surface of the sphere", you actually mean the shortest great circle path over the sphere. Here is one way to plot it. Let (x0,y0,z0) be the center of the sphere.
>
> v1 = [x1-x0;y1-y0;z1-z0]; % Vector from center to 1st point
> r = norm(v1); % The radius
> v2 = [x2-x0;y2-y0;z2-z0]; % Vector from center to 2nd point
> v3 = cross(cross(v1,v2),v1); % v3 lies in plane of v1 & v2 and is orthog. to v1
> v3 = r*v3/norm(v3); % Make v3 of length r
> % Let t range through the inner angle between v1 and v2
> t = linspace(0,atan2(norm(cross(v1,v2)),dot(v1,v2)));
> v = v1*cos(t)+v3*sin(t); % v traces great circle path, relative to center
> plot3(v(1,:)+x0,v(2,:)+y0,v(3,:)+z0) % Plot it in 3D
>
> This will work for any two points (x1,y1,z1) and (x2,y2,z2) on the sphere except for a diametrically opposite pair. (The norm of v3 in that case would be zero and couldn't be normalized.)
>
> Roger Stafford

Subject: Line between two points on a sphere

From: Vinita

Date: 24 Jul, 2012 10:56:06

Message: 5 of 19

"Roger Stafford" wrote in message <hoqmkn$871$1@fred.mathworks.com>...
> "Els " <y.e.t.reeuwijk@student.utwente.nl> wrote in message <hopnd6$19c$1@fred.mathworks.com>...
> > I have two points on a sphere (x1,y1,z1) and (x2,y2,z2).
> > Now I want to plot a straight line over the surface of the sphere between these point.
> >
> > The radius and the center of the sphere are also known.
> >
> > What should be the equation of this line? And how do I plot this?
> > Should I solve the great distance equation? Because this will only give me the length and not the line itsself.
> ----------------
> I presume when you say "straight line over the surface of the sphere", you actually mean the shortest great circle path over the sphere. Here is one way to plot it. Let (x0,y0,z0) be the center of the sphere.
>
> v1 = [x1-x0;y1-y0;z1-z0]; % Vector from center to 1st point
> r = norm(v1); % The radius
> v2 = [x2-x0;y2-y0;z2-z0]; % Vector from center to 2nd point
> v3 = cross(cross(v1,v2),v1); % v3 lies in plane of v1 & v2 and is orthog. to v1
> v3 = r*v3/norm(v3); % Make v3 of length r
> % Let t range through the inner angle between v1 and v2
> t = linspace(0,atan2(norm(cross(v1,v2)),dot(v1,v2)));
> v = v1*cos(t)+v3*sin(t); % v traces great circle path, relative to center
> plot3(v(1,:)+x0,v(2,:)+y0,v(3,:)+z0) % Plot it in 3D
>
> This will work for any two points (x1,y1,z1) and (x2,y2,z2) on the sphere except for a diametrically opposite pair. (The norm of v3 in that case would be zero and couldn't be normalized.)
>
> Roger Stafford

Subject: Line between two points on a sphere

From: Torsten

Date: 24 Jul, 2012 11:26:39

Message: 6 of 19

On 24 Jul., 12:56, "Vinita " <rsingh2...@gmail.com> wrote:
> Hi Roger,
>
> What if I want to plot the euclidean line between two points on a sphere (and not on the arc of the sphere) --http://www.clintoncurry.net/math/images/rabbit_euclidean.png
>
>
>
> "Roger Stafford" wrote in message <hoqmkn$87...@fred.mathworks.com>...
> > "Els " <y.e.t.reeuw...@student.utwente.nl> wrote in message <hopnd6$19...@fred.mathworks.com>...
> > > I have two points on a sphere (x1,y1,z1) and (x2,y2,z2).
> > > Now I want to plot a straight line over the surface of the sphere between these point.
>
> > > The radius and the center of the sphere are also known.
>
> > > What should be the equation of this line? And how do I plot this?
> > > Should I solve the great distance equation? Because this will only give me the length and not the line itsself.
> > ----------------
> > I presume when you say "straight line over the surface of the sphere", you actually mean the shortest great circle path over the sphere. Here is one way to plot it. Let (x0,y0,z0) be the center of the sphere.
>
> > v1 = [x1-x0;y1-y0;z1-z0]; % Vector from center to 1st point
> > r = norm(v1); % The radius
> > v2 = [x2-x0;y2-y0;z2-z0]; % Vector from center to 2nd point
> > v3 = cross(cross(v1,v2),v1); % v3 lies in plane of v1 & v2 and is orthog. to v1
> > v3 = r*v3/norm(v3); % Make v3 of length r
> > % Let t range through the inner angle between v1 and v2
> > t = linspace(0,atan2(norm(cross(v1,v2)),dot(v1,v2)));
> > v = v1*cos(t)+v3*sin(t); % v traces great circle path, relative to center
> > plot3(v(1,:)+x0,v(2,:)+y0,v(3,:)+z0) % Plot it in 3D
>
> > This will work for any two points (x1,y1,z1) and (x2,y2,z2) on the sphere except for a diametrically opposite pair. (The norm of v3 in that case would be zero and couldn't be normalized.)
>
> > Roger Stafford- Zitierten Text ausblenden -
>
> - Zitierten Text anzeigen -

You mean

t=linspace(0,1,10);
plot3(x1+t*(x2-x1),y1+t*(y2-y1),z1+t*(z2-z1));

for two points (x1,y1,z1) and (x2,y2,z2) on the sphere ?

Best wishes
Torsten.

Subject: Line between two points on a sphere

From: Bruno Luong

Date: 24 Jul, 2012 12:22:08

Message: 7 of 19

Torsten <Torsten.Hennig@umsicht.fraunhofer.de> wrote in message <dc084b59-3128-495b-8814-1e50bbe98b29@k21g2000vbj.googlegroups.com>...
>
> You mean
>
> t=linspace(0,1,10);
> plot3(x1+t*(x2-x1),y1+t*(y2-y1),z1+t*(z2-z1));
>

I think visually:

plot3([x1 x2],[y1 y2],[z1 z2]); %or
line([x1 x2],[y1 y2],[z1 z2]);

would look more or less the similar, but without surcharging the internal graphic data.

% Bruno

Subject: Line between two points on a sphere

From: Vinita

Date: 24 Jul, 2012 14:01:11

Message: 8 of 19

Actually I used this code to generate a 3d sphere :-

theta=linspace(0,2*pi,40);
phi=linspace(0,pi,40);
[theta,phi]=meshgrid(theta,phi);
rho=1;
x=rho*sin(phi).*cos(theta);
y=rho*sin(phi).*sin(theta);
z=rho*cos(phi);
mesh(x,y,z)
axis equal
grid on

now suppose i select 2 points on this unit sphere
                              x1,y1,z1=-.8203, -.06622, .5681
                              x2,y2,z2=-.2918, -.7695, .5682
Now I want to draw a straight line between these two points(cutting through the sphere) and not an arc on the circle.
 

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <jum41g$q60$1@newscl01ah.mathworks.com>...
> Torsten <Torsten.Hennig@umsicht.fraunhofer.de> wrote in message <dc084b59-3128-495b-8814-1e50bbe98b29@k21g2000vbj.googlegroups.com>...
> >
> > You mean
> >
> > t=linspace(0,1,10);
> > plot3(x1+t*(x2-x1),y1+t*(y2-y1),z1+t*(z2-z1));
> >
>
> I think visually:
>
> plot3([x1 x2],[y1 y2],[z1 z2]); %or
> line([x1 x2],[y1 y2],[z1 z2]);
>
> would look more or less the similar, but without surcharging the internal graphic data.
>
> % Bruno

Subject: Line between two points on a sphere

From: Vinita

Date: 24 Jul, 2012 14:07:09

Message: 9 of 19

I created a unit sphere using this code

theta=linspace(0,2*pi,40);
phi=linspace(0,pi,40);
[theta,phi]=meshgrid(theta,phi);
rho=1;
x=rho*sin(phi).*cos(theta);
y=rho*sin(phi).*sin(theta);
z=rho*cos(phi);
mesh(x,y,z)
axis equal
grid on

Then I manually selected two points on the sphere and tried
 plot3([x1 x2],[y1 y2],[z1 z2]); %or
 line([x1 x2],[y1 y2],[z1 z2]);
But its not showing on the 3d sphere plot. Although its working if i plot it in a new figure.

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <jum41g$q60$1@newscl01ah.mathworks.com>...
> Torsten <Torsten.Hennig@umsicht.fraunhofer.de> wrote in message <dc084b59-3128-495b-8814-1e50bbe98b29@k21g2000vbj.googlegroups.com>...
> >
> > You mean
> >
> > t=linspace(0,1,10);
> > plot3(x1+t*(x2-x1),y1+t*(y2-y1),z1+t*(z2-z1));
> >
>
> I think visually:
>
> plot3([x1 x2],[y1 y2],[z1 z2]); %or
> line([x1 x2],[y1 y2],[z1 z2]);
>
> would look more or less the similar, but without surcharging the internal graphic data.
>
> % Bruno

Subject: Line between two points on a sphere

From: Bruno Luong

Date: 24 Jul, 2012 14:17:10

Message: 10 of 19

"Vinita " <rsingh2083@gmail.com> wrote in message
> But its not showing on the 3d sphere plot. Although its working if i plot it in a new figure.

The line is inside the sphere, you might know it, no ?

Use "alpha" to set the transparency of the patches.

Bruno

Subject: Line between two points on a sphere

From: Vinita

Date: 24 Jul, 2012 15:01:12

Message: 11 of 19

Super !!!! That worked Bruno.
Bruno I am drawing a quadrilateral on this sphere. In this quadrilateral out of the 6 possible connections between these 4 points (2 diagonal and 4 sides) only 4 need to be considered as good edges(2 diagonal + 2 opposite edges) and the rest two as bad edges(2 left out opposite edges). This is also known as a concave quadrilateral (http://4.bp.blogspot.com/_nD_R3gXtq2Y/S8d43kzcg1I/AAAAAAAAAUc/GiH52XS5gR0/s400/cq5.JPG.jpeg)
The biggest problem is that what representation should I choose so as to discriminate between the good and the bad edges. Its a subset of an optimization problem.
  
"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <jumap6$ote$1@newscl01ah.mathworks.com>...
> "Vinita " <rsingh2083@gmail.com> wrote in message
> > But its not showing on the 3d sphere plot. Although its working if i plot it in a new figure.
>
> The line is inside the sphere, you might know it, no ?
>
> Use "alpha" to set the transparency of the patches.
>
> Bruno

Subject: Line between two points on a sphere

From: Vinita

Date: 24 Jul, 2012 15:02:11

Message: 12 of 19

Super !!!! That worked Bruno.
Bruno I am drawing a quadrilateral on this sphere. In this quadrilateral out of the 6 possible connections between these 4 points (2 diagonal and 4 sides) only 4 need to be considered as good edges(2 diagonal + 2 opposite edges) and the rest two as bad edges(2 left out opposite edges). This is also known as a concave quadrilateral (http://4.bp.blogspot.com/_nD_R3gXtq2Y/S8d43kzcg1I/AAAAAAAAAUc/GiH52XS5gR0/s400/cq5.JPG.jpeg)
The biggest problem is that what representation should I choose so as to discriminate between the good and the bad edges. Its a subset of an optimization problem.

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <jumap6$ote$1@newscl01ah.mathworks.com>...
> "Vinita " <rsingh2083@gmail.com> wrote in message
> > But its not showing on the 3d sphere plot. Although its working if i plot it in a new figure.
>
> The line is inside the sphere, you might know it, no ?
>
> Use "alpha" to set the transparency of the patches.
>
> Bruno

Subject: Line between two points on a sphere

From: Vinita

Date: 25 Jul, 2012 07:34:35

Message: 13 of 19

Bruno I have a little problem :-
I want to select 4 random points on a unit sphere and then draw a line connecting them. Can you please help m,e on this. Heres what I could do till now.

theta=linspace(0,2*pi,40);
phi=linspace(0,pi,40);
[theta,phi]=meshgrid(theta,phi);
rho=1;
x=rho*sin(phi).*cos(theta);
y=rho*sin(phi).*sin(theta);
z=rho*cos(phi);
mesh(x,y,z)
axis equal
grid on
ALPHA('clear'); % renders the sphere transparent


"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <jumap6$ote$1@newscl01ah.mathworks.com>...
> "Vinita " <rsingh2083@gmail.com> wrote in message
> > But its not showing on the 3d sphere plot. Although its working if i plot it in a new figure.
>
> The line is inside the sphere, you might know it, no ?
>
> Use "alpha" to set the transparency of the patches.
>
> Bruno

Subject: Line between two points on a sphere

From: TideMan

Date: 25 Jul, 2012 08:33:04

Message: 14 of 19

On Wednesday, July 25, 2012 7:34:35 PM UTC+12, Vinita wrote:
> Bruno I have a little problem :-
> I want to select 4 random points on a unit sphere and then draw a line connecting them. Can you please help m,e on this. Heres what I could do till now.
>
> theta=linspace(0,2*pi,40);
> phi=linspace(0,pi,40);
> [theta,phi]=meshgrid(theta,phi);
> rho=1;
> x=rho*sin(phi).*cos(theta);
> y=rho*sin(phi).*sin(theta);
> z=rho*cos(phi);
> mesh(x,y,z)
> axis equal
> grid on
> ALPHA('clear'); % renders the sphere transparent
>
>
> "Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <jumap6$ote$1@newscl01ah.mathworks.com>...
> > "Vinita " <rsingh2083@gmail.com> wrote in message
> > > But its not showing on the 3d sphere plot. Although its working if i plot it in a new figure.
> >
> > The line is inside the sphere, you might know it, no ?
> >
> > Use "alpha" to set the transparency of the patches.
> >
> > Bruno

What you need is the gnomonic map transformation.
This enables you to draw straight lines on the surface of a sphere.
You can get this from the (free) Matlab toolbox m_map (Google it).
 

Subject: Line between two points on a sphere

From: Christopher Creutzig

Date: 25 Jul, 2012 11:27:15

Message: 15 of 19

On 25.07.12 09:34, Vinita wrote:

> I want to select 4 random points on a unit sphere and then draw a line connecting them. Can you please help m,e on this. Heres what I could do till now.

I'm not sure what exactly your question is. This is what I'd do:

sphere
alpha(0.1)
hold on
% select points uniformly distributed over the sphere:
pts = randn(4,3);
for k=1:size(pts,1), pts(k,:) = pts(k,:)/norm(pts(k,:)); end
% for plotting, add the first point again as the last
pts(end+1,:)=pts(1,:);
% plot them
line(pts(:,1), pts(:,2), pts(:,3), 'LineWidth', 3)



HTH,
Christopher

Subject: Line between two points on a sphere

From: Vinita

Date: 25 Jul, 2012 11:59:08

Message: 16 of 19

Christopher Creutzig <Christopher.Creutzig@mathworks.com> wrote in message <500FD813.3040203@mathworks.com>...
> On 25.07.12 09:34, Vinita wrote:
>
> > I want to select 4 random points on a unit sphere and then draw a line connecting them. Can you please help m,e on this. Heres what I could do till now.
>
> I'm not sure what exactly your question is. This is what I'd do:
>
> sphere
> alpha(0.1)
> hold on
> % select points uniformly distributed over the sphere:
> pts = randn(4,3);
> for k=1:size(pts,1), pts(k,:) = pts(k,:)/norm(pts(k,:)); end
> % for plotting, add the first point again as the last
> pts(end+1,:)=pts(1,:);
> % plot them
> line(pts(:,1), pts(:,2), pts(:,3), 'LineWidth', 3)
>
>
>
> HTH,
> Christopher

Hi Chris,

That is great. i wanted to ask one thing though. If I dont do this part
   sphere
   alpha(0.1)
but proceed with the rest of the code, would the points thus generated still lie on an invisible sphere.

Subject: Line between two points on a sphere

From: Christopher Creutzig

Date: 26 Jul, 2012 06:41:00

Message: 17 of 19

On 25.07.12 13:59, Vinita wrote:

> That is great. i wanted to ask one thing though. If I dont do this part
> sphere
> alpha(0.1)
> but proceed with the rest of the code, would the points thus generated still lie on an invisible sphere.

Sure. They would also do that if you didn't plot anything at all, and
you can change the 4 in the randn(4,3) call to whatever number of points
you need (and the 3 to whatever number of dimensions you want).


Christopher

Subject: Line between two points on a sphere

From: Vinita

Date: 26 Jul, 2012 11:08:13

Message: 18 of 19

Hey Chris,

I have another problem. I have this graph :-
http://static.inky.ws/image/2485/image.jpg
I want to map these points on a unit sphere now. Do you know how to do it.

Christopher Creutzig <Christopher.Creutzig@mathworks.com> wrote in message <5010E67C.4070308@mathworks.com>...
> On 25.07.12 13:59, Vinita wrote:
>
> > That is great. i wanted to ask one thing though. If I dont do this part
> > sphere
> > alpha(0.1)
> > but proceed with the rest of the code, would the points thus generated still lie on an invisible sphere.
>
> Sure. They would also do that if you didn't plot anything at all, and
> you can change the 4 in the randn(4,3) call to whatever number of points
> you need (and the 3 to whatever number of dimensions you want).
>
>
> Christopher

Subject: Line between two points on a sphere

From: Christopher Creutzig

Date: 26 Jul, 2012 11:29:17

Message: 19 of 19

On 26.07.12 13:08, Vinita wrote:

> I have another problem. I have this graph :-
> http://static.inky.ws/image/2485/image.jpg
> I want to map these points on a unit sphere now. Do you know how to do it.

Select any mapping from a finite plane onto (a subset of a) sphere and
use it to get the image of the points. Which one you select will make a
huge difference for the result and should depend on what your goal is in
the end.

Note that the surface of a sphere is not flat, which means that a lot of
things are completely different from what you are used to in the plane.


Christopher

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us