En général: Comment les décalages transversaux peuvent-ils être implémentés et utilisés plus efficacement?Comment la traversée d'une carte (bit-) peut-elle être mise en œuvre de manière plus élégante?
Disons que nous avons un bitmap défini ci-dessous. Comment pourrions-nous traverser (dans ce cas collecter) tous les pixels proches à partir d'un pixel fixe - et finalement éviter ces 8 if-déclarations?
// The bitmap 1920x1080px
RGBColor[][] imageMatrix = new RGBColor[1920][1080];
// Collect all nearby pixels that are not white
ArrayList<RGBColor> neighboringPixels = new ArrayList<RGBColor>();
// Width-index of center pixel
int w = 50;
// Height-index of center pixel
int h = 50;
// Initializing offsets for a more elegant check-up...
int[][] offsets = { { -1, -1 }, { 0, -1 }, { 1, -1 },
{ 1, 0 }, { 1, 1 }, { 0, 1 }, { -1, 1 },
{ -1, 0 } };
// But this is what I came up with
// Get top-left pixel
if (!(w - 1 < 0 || w - 1 > 255 || h - 1 < 0 || h - 1 > 255)) {
neighboringPixels.add(imageMatrix[w - 1][h - 1]);
}
// Get top pixel
if (!(w < 0 || w > 255 || h - 1 < 0 || h - 1 > 255)) {
neighboringPixels.add(imageMatrix[w][h - 1]);
}
// Get top-right pixel
if (!(w + 1 < 0 || w + 1 > 255 || h - 1 < 0 || h - 1 > 255)) {
neighboringPixels.add(imageMatrix[w + 1][h - 1]);
}
// Get right pixel
if (!(w + 1 < 0 || w + 1 > 255 || h < 0 || h > 255)) {
neighboringPixels.add(imageMatrix[w + 1][h]);
}
// Get bottom-right pixel
if (!(w + 1 < 0 || w + 1 > 255 || h + 1 < 0 || h + 1 > 255)) {
neighboringPixels.add(imageMatrix[w + 1][h + 1]);
}
// Get bottom pixel
if (!(w < 0 || w > 255 || h + 1 < 0 || h + 1 > 255)) {
neighboringPixels.add(imageMatrix[w][h + 1]);
}
// Get bottom-left pixel
if (!(w - 1 < 0 || w - 1 > 255 || h + 1 < 0 || h + 1 > 255)) {
neighboringPixels.add(imageMatrix[w - 1][h + 1]);
}
// Get left pixel
if (!(w - 1 < 0 || w - 1 > 255 || h < 0 || h > 255)) {
neighboringPixels.add(imageMatrix[w - 1][h]);
}
définir "efficace"? voulez-vous dire la vitesse ou les lignes de code? –
@Lashane I signifie principalement seulement des lignes de code. Il doit y avoir un moyen d'éviter ces 8 affirmations ou même si nous devrions travailler dans une salle en trois dimensions. – JAR