我正在編寫一個程序化的行星生成器,我將地形工作到二維數組。我需要做的最後一件事是能夠「包裝」或將數組轉換爲全球投影。C#將二維數組包裝到球體中
它只需要在2D中,因此它可以忽略一個半球。目前陣列是2:1的方面。
512x256 Generated Terrain Array
感謝。
我正在編寫一個程序化的行星生成器,我將地形工作到二維數組。我需要做的最後一件事是能夠「包裝」或將數組轉換爲全球投影。C#將二維數組包裝到球體中
它只需要在2D中,因此它可以忽略一個半球。目前陣列是2:1的方面。
512x256 Generated Terrain Array
感謝。
這裏的關鍵思路是將圖像的x軸映射到球體經度上,圖像的y軸將映射到球體緯度上。
下面是從CodeProject代碼,這要歸功於andalmeida:
這裏不用座標映射:
public static double MapCoordinate(double i1, double i2, double w1,
double w2, double p)
{
return ((p - i1)/(i2 - i1)) * (w2 - w1) + w1;
}
裝入圖像:
System.Drawing.Image image1 = new Bitmap(Server.MapPath(
"./images/worldmap4.gif"));
Bitmap imgBitmap = new Bitmap(image1);
旋轉功能:
public static void RotX(double angle, ref double y, ref double z)
{
double y1 = y * System.Math.Cos(angle) - z * System.Math.Sin(angle);
double z1 = y * System.Math.Sin(angle) + z * System.Math.Cos(angle);
y = y1;
z = z1;
}
public static void RotY(double angle, ref double x, ref double z)
{
double x1 = x * System.Math.Cos(angle) - z * System.Math.Sin(angle);
double z1 = x * System.Math.Sin(angle) + z * System.Math.Cos(angle);
x = x1;
z = z1;
}
public static void RotZ(double angle, ref double x, ref double y)
{
double x1 = x * System.Math.Cos(angle) - y * System.Math.Sin(angle);
double y1 = x * System.Math.Sin(angle) + y * System.Math.Cos(angle);
x = x1;
y = y1;
}
同時循環顯示圖像的兩個維度,從圖像座標中映射phi和theta角度,從phi和theta中獲取笛卡爾三維座標,爲獲得的三維點提供一些旋轉並用相應的圖像顏色繪製它們:
for (int i = 0; i < imgBitmap.Width; i++)
{
for (int j = 0; j < imgBitmap.Height; j++)
{
// map the angles from image coordinates
double theta = Algebra.MapCoordinate(0.0, imgBitmap.Width - 1,
theta1, theta0, i);
double phi = Algebra.MapCoordinate(0.0, imgBitmap.Height - 1,phi0,
phi1, j);
// find the cartesian coordinates
double x = radius * Math.Sin(phi) * Math.Cos(theta);
double y = radius * Math.Sin(phi) * Math.Sin(theta);
double z = radius * Math.Cos(phi);
// apply rotation around X and Y axis to reposition the sphere
RotX(1.5, ref y, ref z);
RotY(-2.5, ref x, ref z);
// plot only positive points
if (z > 0)
{
Color color = imgBitmap.GetPixel(i, j);
Brush brs = new SolidBrush(color);
int ix = (int)x + 100;
int iy = (int)y + 100;
graphics.FillRectangle(brs, ix, iy, 1, 1);
brs.Dispose();
}
}
}
請提供更多信息。鏈接只有答案是脆弱的 - 如果URL失效,帖子不再提供很多答案。 – Pikalek
@Pikalek我同意你的觀點,你走了。 –
也許你想看看這個問題? http://stackoverflow.com/questions/12732590/how-map-2d-grid-points-x-y-onto-sphere-as-3d-points-x-y-z。我認爲這取決於你想要什麼樣的方法。 – Nessuno