Sys*_*X17 0 c# 3d drawing line
我(在帮助下)创建了一个在3D空间中绘制和绘制一系列块的函数.通常,这是在64x64x64网格立方体中执行的.
这是我的代码:
internal static int DrawLine(Player theplayer, Byte drawBlock,
int x0, int y0, int z0, int x1, int y1, int z1)
{
int blocks = 0;
bool cannotUndo = false;
bool detected = false;
int dx = x1 - x0;
int dy = y1 - y0;
int dz = z1 - z0;
DrawOneBlock(theplayer, drawBlock, x0, y0, z0, ref blocks, ref cannotUndo);
if (Math.Abs(dx) > Math.Abs(dy) &&
Math.Abs(dx) > Math.Abs(dz) &&
detected == false)
{
detected = true;
float my = (float)dy / (float)dx;
float mz = (float)dz / (float)dx;
float by = y0 - my * x0;
float bz = z0 - mz * x0;
dx = (dx < 0) ? -1 : 1;
while (x0 != x1)
{
x0 += dx;
DrawOneBlock(theplayer, drawBlock,
Convert.ToInt32(x0),
Convert.ToInt32(Math.Round(my * x0 + by)),
Convert.ToInt32(Math.Round(mz * x0 + bz)),
ref blocks, ref cannotUndo);
}
}
if (Math.Abs(dy) > Math.Abs(dz) &&
Math.Abs(dy) > Math.Abs(dx) &&
detected == false)
{
detected = true;
float mz = (float)dz / (float)dy;
float mx = (float)dx / (float)dy;
float bz = z0 - mz * y0;
float bx = x0 - mx * y0;
dy = (dy < 0) ? -1 : 1;
while (y0 != y1)
{
y0 += dy;
DrawOneBlock(theplayer, drawBlock,
Convert.ToInt32(Math.Round(mx * y0 + bx)),
Convert.ToInt32(y0),
Convert.ToInt32(Math.Round(mz * y0 + bz)),
ref blocks, ref cannotUndo);
}
}
if (detected == false)
{
detected = true;
float mx = (float)dx / (float)dz;
float my = (float)dy / (float)dz;
float bx = x0 - mx * z0;
float by = y0 - my * z0;
dz = (dz < 0) ? -1 : 1;
while (z0 != z1)
{
z0 += dz;
DrawOneBlock(theplayer, drawBlock,
Convert.ToInt32(Math.Round(mx * z0 + bx)),
Convert.ToInt32(Math.Round(my * z0 + by)),
Convert.ToInt32(z0),
ref blocks, ref cannotUndo);
}
}
return blocks;
}
Run Code Online (Sandbox Code Playgroud)
它应该排队块图并返回它绘制的块数.问题是它没有绘制一条不折不扣的线.在某些情况下,当至少所有块都应通过它们的顶点连接时,它会在块之间留下间隙.
我努力解决的代码中唯一的部分是我计算轴的最大差异并创建斜率常量.我试图做一个完美的对角线时遇到了一个问题.所有值都相等所以我只是默认为z轴 - 这是我认为问题存在的地方.
也许修改为(希望)在3D中工作的Bresenham线算法可以替代你?
public static void Swap<T>(ref T x, ref T y)
{
T tmp = y;
y = x;
x = tmp;
}
private void Draw3DLine(int x0, int y0, int z0, int x1, int y1, int z1)
{
bool steepXY = Math.Abs(y1 - y0) > Math.Abs(x1 - x0);
if (steepXY) { Swap(ref x0, ref y0); Swap(ref x1, ref y1); }
bool steepXZ = Math.Abs(z1 - z0) > Math.Abs(x1 - x0);
if (steepXZ) { Swap(ref x0, ref z0); Swap(ref x1, ref z1); }
int deltaX = Math.Abs(x1 - x0);
int deltaY = Math.Abs(y1 - y0);
int deltaZ = Math.Abs(z1 - z0);
int errorXY = deltaX / 2, errorXZ = deltaX / 2;
int stepX = (x0 > x1) ? -1 : 1;
int stepY = (y0 > y1) ? -1 : 1;
int stepZ = (z0 > z1) ? -1 : 1;
int y=y0, z=z0;
// Check if the end of the line hasn't been reached.
for(int x = x0; x!=x1; x+=stepX)
{
int xCopy=x, yCopy=y, zCopy=z;
if (steepXZ) Swap(ref xCopy, ref zCopy);
if (steepXY) Swap(ref xCopy, ref yCopy);
// Replace the WriteLine with your call to DrawOneBlock
Console.WriteLine("[" + xCopy + ", " + yCopy + ", " + zCopy + "], ");
errorXY -= deltaY;
errorXZ -= deltaZ;
if (errorXY < 0)
{
y += stepY;
errorXY += deltaX;
}
if (errorXZ < 0)
{
z += stepZ;
errorXZ += deltaX;
}
}
}
Run Code Online (Sandbox Code Playgroud)