如何在 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);
}