Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

graphics - Algorithm to fill triangle

i'm thinking about rasterization triangle algorithm. ( triangle_rasterization_lesson )

I wtote the following code:

void triangle(int xa, int ya, int xb, int yb, int xc, int yc, TGAImage &image, TGAColor color)
{
    line(xa, ya, xb, yb, image, color);
    line(xa, ya, xc, yc, image, color);
    line(xb, yb, xc, yc, image, color);
    for (int x = xa; x<=xb; x++)
    {
        for (int y = ya; y<=yb; y++)
        {
            line(xc, yc, x, y, image, white);
        }
    }
}

With triangle(100, 100, 100, 400, 400, 100, image, red); it works properly. But if i swap X(xa, ya) and Z(xc, yc) coordinates to doesn't fill my square.

With triangle(70, 50, 200, 100, 20, 150, image, red); it draws triangle, but filling comes out of bounds.

Where is the problem?

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your are increasing x and y one every loop. But you can't do this for every triangle. Depend on triangle angle when x increasing , maybe y decreasing. For example if triangle has 90 degree, you will not increase x or y. I can't explain myself very well but there is code and imaged explanation:

http://www.sunshine2k.de/coding/java/TriangleRasterization/TriangleRasterization.html


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.8k users

...