空间直角坐标系与球面坐标互转

空间直角坐标系与球面坐标互转

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace AppSurveryTools.SphericalAndCartesian
 7 {
 8     class CartesianCoord
 9     {
10         public double x;
11         public double y;
12         public double z;
13 
14         private static CartesianCoord temp = CartesianCoord.ZERO;
15 
16         public CartesianCoord(double x = 0, double y = 0, double z = 0)
17         {
18             this.x = x;
19             this.y = y;
20             this.z = z;
21         }
22 
23         public CartesianCoord clone()
24         {
25             return new CartesianCoord(this.x, this.y, this.z);
26         }
27 
28         public void copyTo(CartesianCoord n)
29         {
30             n.x = this.x;
31             n.y = this.y;
32             n.z = this.z;
33         }
34 
35         public void copyFrom(CartesianCoord n)
36         {
37             this.x = n.x;
38             this.y = n.y;
39             this.z = n.z;
40         }
41 
42         public void reset(double newx = 0, double newy = 0, double newz = 0)
43         {
44             this.x = newx;
45             this.y = newy;
46             this.z = newz;
47         }
48 
49         public static CartesianCoord ZERO
50         {
51             get
52             {
53                 return new CartesianCoord(0, 0, 0);
54             }
55         }
56     }
57 }
CartesianCoord
 1 namespace AppSurveryTools.SphericalAndCartesian
 2 {
 3     public class SphericalCoord
 4     {
 5         public double radius;
 6         public double theta;
 7         public double phi;
 8 
 9         private static SphericalCoord temp = SphericalCoord.ZERO;
10 
11         public SphericalCoord(double radius, double theta = 0, double phi = 0)
12         {
13             this.radius = radius;
14             this.theta = theta;
15             this.phi = phi;
16         }
17 
18         public SphericalCoord clone()
19         {
20             return new SphericalCoord(this.radius, this.theta, this.phi);
21         }
22 
23         public void copyTo(SphericalCoord n)
24         {
25             n.radius = this.radius;
26             n.theta = this.theta;
27             n.phi = this.phi;
28         }
29 
30         public void copyFrom(SphericalCoord n)
31         {
32             this.radius = n.radius;
33             this.theta = n.theta;
34             this.phi = n.phi;
35         }
36 
37         public void reset(double newradius = 0, double newtheta = 0, double newphi = 0)
38         {
39             this.radius = newradius;
40             this.theta = newtheta;
41             this.phi = newphi;
42         }
43 
44         public static SphericalCoord ZERO
45         {
46             get
47             {
48                 return new SphericalCoord(0, 0, 0);
49             }
50         }
51     }
52 }
SphericalCoord
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace AppSurveryTools.SphericalAndCartesian
 7 {
 8     class CoordsTransform
 9     {
10         public CoordsTransform()
11         {
12         }
13 
14         public SphericalCoord CartesianToSpherical(CartesianCoord coord)
15         {
16             var radius = this.GetModuloFromCartesianCoord(coord);
17             var theta = this.GetThetaFromCartesianCoord(coord);
18             var phi = this.GetPhiFromCartesianCoord(coord);
19             return new SphericalCoord(radius, theta, phi);
20         }
21         /// <summary>
22         /// 获取空间直角坐标空间中模,径向距离
23         /// </summary>
24         /// <param name="coord"></param>
25         /// <returns></returns>
26         protected double GetModuloFromCartesianCoord(CartesianCoord coord)
27         {
28             return Math.Sqrt(coord.x * coord.x + coord.y * coord.y + coord.z * coord.z);
29         }
30         /// <summary>
31         /// Theta角,天顶角
32         /// </summary>
33         /// <param name="coord"></param>
34         /// <returns></returns>
35         protected double GetThetaFromCartesianCoord(CartesianCoord coord)
36         {
37             //return Math.atan(Math.sqrt(coord.x*coord.x + coord.y*coord.y)/coord.z);
38             return Math.Acos(coord.z / this.GetModuloFromCartesianCoord(coord));
39         }
40         /// <summary>
41         /// 获取phi角,方位角
42         /// </summary>
43         /// <param name="coord"></param>
44         /// <returns></returns>
45         protected double GetPhiFromCartesianCoord(CartesianCoord coord)
46         {
47             return Math.Atan(coord.y / coord.x);
48         }
49 
50         public CartesianCoord SphericalToCartesian(SphericalCoord coord)
51         {
52             var x = this.GetXFromSphericalCoord(coord);
53             var y = this.GetYFromSphericalCoord(coord);
54             var z = this.GetZFromSphericalCoord(coord);
55             return new CartesianCoord(x, y, z);
56         }
57         //球面坐标计算X坐标
58         protected double GetXFromSphericalCoord(SphericalCoord coord)
59         {
60             return coord.radius * Math.Sin(coord.theta) * Math.Cos(coord.phi);
61         }
62         //球面坐标计算Y坐标
63         protected double GetYFromSphericalCoord(SphericalCoord coord)
64         {
65             return coord.radius * Math.Sin(coord.theta) * Math.Sin(coord.phi);
66         }
67         //球面坐标计算Z坐标
68         protected double GetZFromSphericalCoord(SphericalCoord coord)
69         {
70             return coord.radius * Math.Cos(coord.theta);
71         }
72     }
73 }
CoordsTransform

调用代码

 1   private void btnCartesianToSpherical_Click(object sender, EventArgs e)
 2         {
 3             CoordsTransform CoordTrans = new CoordsTransform();
 4             double x = (double)numX.Value;
 5             double y = (double)numY.Value;
 6             double z = (double)numZ.Value;
 7             CartesianCoord vCartesianCoord = new CartesianCoord(x, y, z);
 8             SphericalCoord ResSphericalCoord = CoordTrans.CartesianToSpherical(vCartesianCoord);
 9             numRadius.Value = (decimal)ResSphericalCoord.radius;
10             numPhi.Value = (decimal)ResSphericalCoord.phi;
11             numAmith.Value = (decimal)ResSphericalCoord.theta;
12         }
13 
14         private void btnSphericalToCartesian_Click(object sender, EventArgs e)
15         {
16             CoordsTransform CoordTrans = new CoordsTransform();
17             double radius = (double)numRadius.Value;
18             double phi = (double)numPhi.Value;
19             double theta = (double)numAmith.Value;
20             SphericalCoord vSphericalCoord = new SphericalCoord(radius, theta, phi);
21             CartesianCoord ResCartesianCoord = CoordTrans.SphericalToCartesian(vSphericalCoord);
22             numX.Value = (decimal)ResCartesianCoord.x;
23             numY.Value = (decimal)ResCartesianCoord.y;
24             numZ.Value = (decimal)ResCartesianCoord.z;
25         }
View Code

参考文献:

http://www.cnblogs.com/hans_gis/archive/2012/11/21/2755126.html

posted @ 2013-12-25 22:06  太一吾鱼水  阅读(1804)  评论(0编辑  收藏  举报