SOLUTION AU FOND DE CETTE QUESTIONTracer une ligne sur une présentation de la grille, puis plus veritcal axe
J'ai ce code:
public void lineImproved(int x0, int y0, int x1, int y1, Color color)
{
int pix = color.getRGB();
int dx = x1 - x0;
int dy = y1 - y0;
raster.setPixel(pix, x0, y0);
if (Math.abs(dx) > Math.abs(dy)) { // slope < 1
float m = (float) dy/(float) dx; // compute slope
float b = y0 - m*x0;
dx = (dx < 0) ? -1 : 1;
while (x0 != x1) {
x0 += dx;
raster.setPixel(pix, x0, Math.round(m*x0 + b));
}
} else
if (dy != 0) { // slope >= 1
float m = (float) dx/(float) dy; // compute slope
float b = x0 - m*y0;
dy = (dy < 0) ? -1 : 1;
while (y0 != y1) {
y0 += dy;
raster.setPixel(pix, Math.round(m*y0 + b), y0);
}
}
}
Il trace actuellement une ligne et remplit les pixels spécifiques qui composent la ligne entre les 2 points spécifiés (ie [x0, y0] et [x1, y1]). J'en ai besoin pour inclure un h0 et h1 pour la hauteur des 2 points. Ce faisant, j'espère pouvoir obtenir une valeur de hauteur sur l'axe vertical avec chaque fonction raster.setPixel.
MISE À JOUR J'ai maintenant le code comme il se doit pour cette tâche, mais toujours seulement en 2D. Je dois mettre en œuvre la solution proposée au code suivant pour vérifier:
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;
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))
{ // slope < 1
float m = (float)dy/(float)dx; // compute slope
float b = y0 - m * x0;
dx = (dx < 0) ? -1 : 1;
while (x0 != x1)
{
x0 += dx;
DrawOneBlock(theplayer, drawBlock, x0, Convert.ToInt32(Math.Round(m * x0 + b)), z0, ref blocks, ref cannotUndo);
}
}
else
{
if (dy != 0)
{ // slope >= 1
float m = (float)dx/(float)dy; // compute slope
float b = x0 - m * y0;
dy = (dy < 0) ? -1 : 1;
while (y0 != y1)
{
y0 += dy;
DrawOneBlock(theplayer, drawBlock, Convert.ToInt32(Math.Round(m * y0 + b)), y0, z0, ref blocks, ref cannotUndo);
}
}
}
return blocks;
}
SOLUTION:
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);
//a>x,b>y,c>z
if (Math.Abs(dx) > Math.Abs(dy) && Math.Abs(dx) > Math.Abs(dz) && detected == false)
{ // x distance is largest
detected = true;
float my = (float)dy/(float)dx; // compute y slope
float mz = (float)dz/(float)dx; // compute z slope
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);
}
}
//a>y,b>z,c>x
if (Math.Abs(dy) > Math.Abs(dz) && Math.Abs(dy) > Math.Abs(dx) && detected == false && detected == false)
{ // y distance is largest
detected = true;
float mz = (float)dz/(float)dy; // compute z slope
float mx = (float)dx/(float)dy; // compute x slope
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);
}
}
//a>z,b>x,c>y
if (Math.Abs(dz) > Math.Abs(dx) && Math.Abs(dz) > Math.Abs(dy) && detected == false && detected == false)
{ // z distance is largest
detected = true;
float mx = (float)dx/(float)dz; // compute x slope
float my = (float)dy/(float)dz; // compute y slope
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);
}
}
Voulez-vous dire en ajoutant une dimension z de sorte que vous remplissez dans une trame 3D (champ voxel) à la place? Sinon, que signifie la hauteur dans votre cas, y'a pas y0 et y1 la hauteur? – Hannesh
Je ne l'ai pas expliqué correctement. Je peux actuellement dessiner une ligne sur une surface plane de x0, y0 à x1, y1 et les pixels entre eux sont remplis correctement pour compléter la ligne. – SystemX17
Je dois ensuite ajouter un composant h0 pour obtenir la hauteur au-dessus de cette surface plane pour la profondeur afin de déterminer quelle couche elle apparaît sur des grilles de profondeur à l'échelle similaires. – SystemX17