2016-12-07 8 views
0

Я реализую алгоритм seedfill. Для цвета используется общая переменная. Однако я не знаю, как реализовать шаблон patt, который является целым числом. Вот исходный код:Добавление шаблона в алгоритм SeedFill

public class SeedFill4In<PixelType> implements SeedFill<PixelType> { 

@Override 
public @NotNull RasterImage<PixelType> fill(
     final @NotNull RasterImage<PixelType> img, final int c, final int r, 
     final @NotNull PixelType areaValue, 
     final @NotNull PixelType newValue) { 

    int [ ][ ] patt =  { 
      {0xFF0000,0x0000FF,0x0000FF,0xFF0000,0xFF0000}, 
      {0xFF0000,0x0000FF,0x00FF00,0xFF0000,0xFF0000}, 
      {0xFF0000,0x0000FF,0x0000FF,0x00FF00,0xFF0000}, 
      {0xFF0000,0x0000FF,0x0000FF,0xFF0000,0xFF0000}, 
      {0xFF0000,0x0000FF,0x0000FF,0xFF0000,0xFF0000} 
     }; 

    int i = c % patt.length; 
    int j = r % patt.length; 






    return new Object() { 
     @NotNull RasterImage<PixelType> fill(
       final @NotNull RasterImage<PixelType> img, final int c, final int r) { 
      return img.getPixel(c, r) 
        .flatMap((final @NotNull PixelType actualValue) -> { 
         if (actualValue.equals(areaValue)) { 
          return Optional.of(
           fill(
            fill(
             fill(
              fill(
               img.withPixel(c, r, newValue), 
               c, r - 1), 
              c + 1, r), 
             c, r + 1), 
            c - 1, r) 
           ); 
         } 
         return Optional.empty(); 
        }) 
        .orElse(img); 
     } 
    }.fill(img, c, r); 
} 

Для заполнения без рисунка я использовал переменную newValue. Как изменить его на образец?

ответ

0

Вместо того, чтобы просить PixelType newValue в качестве параметра (т.е. однородного цвета для вашей «кисти»), вам нужно задать для шаблона - в конце концов, это шаблон, что вы будете использовать в качестве «кисти "

public class SeedFill4In<PixelType> implements SeedFill<PixelType> { 

    @Override 
    public @NotNull RasterImage<PixelType> fill(
     final @NotNull RasterImage<PixelType> img, final int c, final int r, 
     final @NotNull PixelType areaValue, 
     final PixelType[][] brush // assumed square 
) { 

    int i = c % brush.length; // this is where the assumption of 
    int j = r % brush.length; // a square brush is used 

    final PixelType newValue=brush[j][i]; 

    // etc 
} 

Более общо:

interface Brush<PixelType> { 
    PixelType valueFor(int c, int r); 
} 

public class SeedFill4In<PixelType, BrushType extends Brush<PixelType>> 
implements SeedFill<PixelType> { 
    @Override 
    public @NotNull RasterImage<PixelType> fill(
     final @NotNull RasterImage<PixelType> img, final int c, final int r, 
     final @NotNull PixelType areaValue, 
     final BrushType brush 
) { 
    // TODO code here the stop conditions for out of bounds c,r 

    if (img.getPixel(c, r).equals(areaValue)) { 
     img.setPixel(c,r,brush.valueFor(c,r)); 
    } 
    // suboptimal as hell to revisit the nodes that have been already painted 
    // but that's beyond the original question 
    fill(img,c-1,r,areaValue,brush) 
    fill(img,c+1,r,areaValue,brush) 
    fill(img,c,r-1,areaValue,brush) 
    fill(img,c,r+1,areaValue,brush) 
    } 
}