0

我正在实施种子填充算法。对于颜色,我使用通用变量。但是我不知道如何实现patt整数模式。这是源代码:

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变量。如何将其更改为模式?

4

1 回答 1

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)
  }
}
于 2016-12-07T23:32:57.017 回答