我试图生成一个完整的球面坐标,从 phi 开始,范围从 0 - pi 度,theta 范围从 0 到 2pi 度。我尝试使用嵌套 for 循环来生成 x、y、z 坐标,但出现错误“无法将 double[] 转换为 double”。不知道如何解决这个问题,非常感谢您的帮助。
static void Main(string[] args) {
var phi = Enumerable.Range(0, 181).ToArray();
var theta = Enumerable.Range(0, 361).ToArray();
double[] phiconverted = new double[181];
double[] thetaconverted = new double[361];
for (int i = 0; i < phi.Length; i++)
{
phiconverted[i] = phi[i] * Math.PI / 180;
}
for (int i = 0; i < theta.Length; i++)
{
thetaconverted[i] = theta[i] * Math.PI / 180;
}
double x, y, z;
for (int i = 0; i < phiconverted.Length; i++)
{
for (int j = 0; j<thetaconverted.Length; j++)
{
x = 10 * Math.Sin(thetaconverted) * Math.Cos(phiconverted);
y = 10 * Math.Sin(thetaconverted) * Math.Sin(phiconverted);
z = 10 * Math.Cos(thetaconverted);
}
}
}
指示异常发生在哪一行通常很有帮助。在本例中,我假设是
x=
、y=
和 z=
线。
您收到的错误表明您向需要
double[]
的方法提供了 double
(数组)。原因是您尚未指定要访问的数组中的哪个索引。大概应该是这样的:
x = 10 * Math.Sin(thetaconverted[j]) * Math.Cos(phiconverted[i]);
y = 10 * Math.Sin(thetaconverted[j]) * Math.Sin(phiconverted[i]);
z = 10 * Math.Cos(thetaconverted[j]);