【问题标题】:Adding pattern to SeedFill algorithm向 SeedFill 算法添加模式
【发布时间】:2016-12-07 20:49:52
【问题描述】:

我正在实施种子填充算法。对于颜色,我使用通用变量。但是我不知道如何实现整数模式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 变量。如何将其更改为模式?

【问题讨论】:

    标签: java algorithm graphical-programming


    【解决方案1】:

    您需要询问图案,而不是要求 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)
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2011-01-24
      • 1970-01-01
      • 1970-01-01
      • 2012-01-09
      • 2020-12-21
      • 1970-01-01
      • 2016-08-31
      • 2017-12-27
      • 2012-04-18
      相关资源
      最近更新 更多