[英]Cartesian to Polar (3d coordinates)
您如何在 3D 空間中的笛卡爾坐標系和極坐標系(和反坐標系)之間進行轉換? 最好使用 ac# 示例,但任何東西都將不勝感激。 謝謝!
編輯當考慮 20% 的變化時(不形成球體)
編輯 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);
}
從笛卡爾到極地:
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)
我還沒有測試。
您可以重寫以減少浮點運算的數量。
這取決於如何測量方位角-從水平面還是從垂直軸。 我已經閱讀了Wikipedia文章,但如果將其度量為地理緯度(赤道= 0,波蘭人= + 90和-90),則應該使用asin和sin 。
我在3D建模軟件中使用c#,那里的方位角是相對於xy平面而不是z軸測量的。 就我而言,公式為:
lat = asin (z / r)
x = r * cos (經度)* cos(經度)
y = r * cos (lat)* sin(long)
z = r * 罪 (lat)
為了考慮到 4 個象限:
r = sqrt(x * x + y * y + z * z)
long = atan2(y,x);
lat = acos(z / r);
這是在以下函數中實現的,我在 4 個象限中檢查過:
double modulo(vector <double> xyz) { return sqrt(xyz[0] * xyz[0] + xyz[1] * xyz[1] + xyz[2] * xyz[2] + 1e-130); }
void cartesian_to_polar(vector <double> a, double& r, double& lat, double& lon) { r = modulo(a); lon = atan2(a[1], a[0]); lat = acos(a[2] / r); }
void polar_to_cartesian(double r, double lat, double lon, vector <double>& a) { a[2] = r * cos(lat); a[0] = r * sin(lat) * cos(lon); a[1] = r * sin(lat) * sin(lon); }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.