2012-06-03 140 views
2

如何在三維空間中笛卡爾座標和極座標(和後坐)座標系之間進行轉換?最好用一個c#的例子,但任何真的不勝感激。謝謝!笛卡兒到極座標(三維座標)

編輯 當變化的20%是考慮到(未形成球體)

enter image description here

編輯2

private void Spherise() { 
     for (int i = 0; i < vertices.Count; i++) { 
      float radius = this.radius; 
      float longitude = 0; 
      float latitude = 0; 

      float sphereRadius = 32; 

      Color color = vertices[i].Color; 

      ToPolar(vertices[i].Position - centre, out radius, out longitude, out latitude); 
      Vector3 position = ToCartesian(sphereRadius, longitude, latitude) + centre; 

      Vector3 normal = vertices[i].Position - centre; 
      normal.Normalize(); 

      const float lerpAmount = 0.6f; 
      Vector3 lerp = (position - vertices[i].Position) * lerpAmount + vertices[i].Position; 
      vertices[i] = new VertexPositionColorNormal(lerp, color, normal); 
     } 
    } 

    private void ToPolar(Vector3 cart, out float radius, out float longitude, out float latitude) { 
     radius = (float)Math.Sqrt((double)(cart.X * cart.X + cart.Y * cart.Y + cart.Z * cart.Z)); 
     longitude = (float)Math.Acos(cart.X/Math.Sqrt(cart.X * cart.X + cart.Y * cart.Y)) * (cart.Y < 0 ? -1 : 1); 
     latitude = (float)Math.Acos(cart.Z/radius) * (cart.Z < 0 ? -1 : 1); 
    } 

    private Vector3 ToCartesian(float radius, float longitude, float latitude) { 
     float x = radius * (float)(Math.Sin(latitude) * Math.Cos(longitude)); 
     float y = radius * (float)(Math.Sin(latitude) * Math.Sin(longitude)); 
     float z = radius * (float)Math.Cos(latitude); 

     return new Vector3(x, y, z); 
    } 

enter image description here

+0

最好告訴我們到目前爲止你已經嘗試了什麼? – Shai

+1

爲什麼倒票(誰做的)?我在問,因爲我不知道如何做到這一點,並且在這個網站上似乎沒有包含第三維的問題...... – Darestium

+0

你在說什麼極座標系統?圓柱?球形? –

回答

5

FRO米笛卡爾座標到極座標:

r = sqrt(x * x + y * y + z * z) 
long = acos(x/sqrt(x * x + y * y)) * (y < 0 ? -1 : 1) 
lat = acos(z/r) 

從極地到笛卡爾:

x = r * sin(lat) * cos(long) 
y = r * sin(lat) * sin(long) 
z = r * cos(lat) 

我沒有測試它。

您可以重寫以減少浮點操作的數量。

+0

謝謝!在第一行第二塊我假設你的意思是'x'? – Darestium

+0

@Darestium最後一行是x – nhahtdh

+0

@Darestium:我編輯帖子,以免混淆。 – nhahtdh

0

這取決於如何測量方位角 - 從水平面或垂直軸。我已經閱讀了維基百科的文章,但是如果您將它作爲地理緯度(Equator = 0,Poles = + 90和-90)進行衡量,那麼您應該使用asinsin

我在三維建模軟件中使用c#,並在那裏測量方位關於xy平面而不是z軸。在我的情況下,公式是:

LAT = ASIN(Z/R)

X = R * COS(LAT)* COS(長)

Y = R * COS(LAT)*罪(長)

Z = R * (LAT)