首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >java小林·吴氏算法

java小林·吴氏算法
EN

Stack Overflow用户
提问于 2017-12-05 05:06:28
回答 1查看 301关注 0票数 0

我试图编写吴小林的算法,但我被卡住了。

似乎它应该可以工作,但不是,线条是完全光栅化的巫师完全不透明,有人知道为什么吗?非常感谢。我将其与pseoudo代码进行了比较,它看起来非常相似。我认为在选择颜色时会出现错误。

代码:

代码语言:javascript
复制
public class LineRasterizerXiaolinWu {

private RasterImage img;
private int color;
double gradient;

public LineRasterizerXiaolinWu(RasterImage img) {
    this.img = img;
}

public RasterImage rasterizeLine(int x1, int y1,
                                 int x2, int y2) {

    RasterImage result = img;

    boolean steep = Math.abs(y2-y1)> Math.abs(x2-x1);

    if(steep){
        int c = y1;
        y1 = x1;
        x1 = c;
        c = x2;
        x2 = y2;
        y2 = c;
    }
    if(x2 < x1) {
        int d = x2;
        x2 = x1;
        x1 = d;
        d = y2;
        y2 = y1;
        y1 = d;
    }

    double dx = x2 - x1;
    double dy = y2 - y1;

    if (dx == 0.0){
        gradient =1;
    }else {
        gradient = dy / dx;
    }

    //first end point
    int xEnd = round(x1);
    int yEnd = (int)(y1+gradient*(xEnd-x1));
    int xGap = (int) rfpart(x1+0.5);

    int xPxl1 = xEnd;
    int yPxl1 = (int)ipart(yEnd);

    if (steep) {
        plot(yPxl1, xPxl1, rfpart(yEnd)*xGap);
        plot((yPxl1 + 1), xPxl1, fpart(yEnd)*xGap);
    } else {
        plot(xPxl1, yPxl1, rfpart(yEnd) * xGap);
        plot(xPxl1, yPxl1 + 1, fpart(yEnd) * xGap);
    }

    double intery = yEnd + gradient;

    //second end point
    xEnd = round(x2);
    yEnd = (int)(y2+gradient*(xEnd-x2));
    xGap = (int) rfpart(x2+0.5);

    int xPxl2 = xEnd;
    int yPxl2 = (int)ipart(yEnd);

    if (steep) {
        plot(yPxl2, xPxl2, rfpart(yEnd)*xGap);
        plot((yPxl2 + 1), xPxl2, fpart(yEnd)*xGap);
    } else {
        plot(xPxl2, yPxl2, rfpart(yEnd) * xGap);
        plot(xPxl2, yPxl2 + 1, fpart(yEnd) * xGap);
    }

    if(steep){
        for(int i = xPxl1+1; i<=xPxl2-1; i++){
            plot((int)ipart(intery),i,rfpart(intery));
            plot((int)ipart(intery)+1,i,fpart(intery));
            intery +=gradient;
        }
    }else {
        for(int i = xPxl1+1; i<=xPxl2-1; i++){
            plot(i,(int)ipart(intery),rfpart(intery));
            plot(i,(int)ipart(intery)+1,fpart(intery));
            intery +=gradient;
        }
    }
    return result;
}

private void plot(int x, int y, double intensity){
    double alfaChanel = Math.round(intensity*100.0)/100.0;

    if (alfaChanel >= 0 && alfaChanel <= 0.2) {
        color =  (new Color(1.0f, 0.0f, 0.0f, 1.0f)).getRGB();
    } else if (alfaChanel > 0.2 && alfaChanel <= 0.4) {
        color =  (new Color(1.0f, 0.0f, 0.0f, 0.8f)).getRGB();
    } else if (alfaChanel > 0.4 && alfaChanel <= 0.6) {
        color =  (new Color(1.0f, 0.0f, 0.0f, 0.6f)).getRGB();
    } else if (alfaChanel > 0.6 && alfaChanel <= 0.8) {
        color =  (new Color(1.0f, 0.0f, 0.0f, 0.4f)).getRGB();
    } else if (alfaChanel > 0.8 && alfaChanel <= 1.0) {
        color =  (new Color(1.0f, 0.0f, 0.0f, 0.2f)).getRGB();
    }



    img.withPixel(x,y,color);
}

//integer part of x
private double ipart (double x){
    return Math.floor(x);
}

private int round (double x){
    return (int) Math.round(x+0.5);
}

//fractional part of x
private double fpart (double x){
    return x-Math.floor(x);
}

private double rfpart (double x){
    return 1-fpart(x);
}

结果:

EN

回答 1

Stack Overflow用户

发布于 2017-12-06 02:26:16

通过添加以下方法解决了问题:

代码语言:javascript
复制
 private int blend (int a, int b, float ratio) {
    if (ratio > 1f) {
        ratio = 1f;
    } else if (ratio < 0f) {
        ratio = 0f;
    }

    float iRatio = 1.0f - ratio;

    int aA = (a >> 24 & 0xff);
    int aR = ((a & 0xff0000) >> 16);
    int aG = ((a & 0xff00) >> 8);
    int aB = (a & 0xff);

    int bA = (b >> 24 & 0xff);
    int bR = ((b & 0xff0000) >> 16);
    int bG = ((b & 0xff00) >> 8);
    int bB = (b & 0xff);

    int A = (int)((aA * iRatio) + (bA * ratio));
    int R = (int)((aR * iRatio) + (bR * ratio));
    int G = (int)((aG * iRatio) + (bG * ratio));
    int B = (int)((aB * iRatio) + (bB * ratio));

    return A << 24 | R << 16 | G << 8 | B;
}

并像这样选择颜色:

代码语言:javascript
复制
private void plot(int x, int y, double intensity){
    double alfaChanel = Math.round(intensity*100.0)/100.0;
    backGroundColor = 0xff2f2f2f;
    drawingColor = 0xffff0000;

    if (alfaChanel >= 0 && alfaChanel <= 0.2) {
        color = blend(backGroundColor,drawingColor,(float)alfaChanel);
    } else if (alfaChanel > 0.2 && alfaChanel <= 0.4) {
        color = blend(backGroundColor,drawingColor,(float)alfaChanel);
    } else if (alfaChanel > 0.4 && alfaChanel <= 0.6) {
        color = blend(backGroundColor,drawingColor,(float)alfaChanel);
    } else if (alfaChanel > 0.6 && alfaChanel <= 0.8) {
        color = blend(backGroundColor,drawingColor,(float)alfaChanel);
    } else if (alfaChanel > 0.8 && alfaChanel <= 1.0) {
        color = blend(backGroundColor,drawingColor,(float)alfaChanel);
    }

    img.withPixel(x,y,color);
}

谢谢

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47641955

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档