Skip to content

Possible bug in fill_triangle() method #33

Closed
@peter-l5

Description

@peter-l5

The fill_triangle method seems to be drawing triangles incorrectly around the middle vertex (in the vertical direction). This appears to be related to the method drawing a horizontal line with the same y co-ordinate twice at the level of the middle vertex. The behaviour happens because the final value of the loop variable y in the loop used to fill the top part of the triangle is repeated for the first line used to fill the bottom part of the triangle. The code from the two loops is below for reference.

The images below illustrate the issue around the middle vertex.

I have developed and tested a fix for this employed in the repository framebuf2 and will post it here as a pull request separately in the next day or two. While I realise that this library is due to be deprecated soon, I figure it may still be worthwhile to fix it.

        for y in range(y0, last + 1):
            a = x0 + sa // dy01
            b = x0 + sb // dy02
            sa += dx01
            sb += dx02
            if a > b:
                a, b = b, a
            self.hline(a, y, b - a + 1, *args, **kwargs)
        sa = dx12 * (y - y1)
        sb = dx02 * (y - y0)
        while y <= y2:
            a = x1 + sa // dy12
            b = x0 + sb // dy02
            sa += dx12
            sb += dx02
            if a > b:
                a, b = b, a
            self.hline(a, y, b - a + 1, *args, **kwargs)
            y += 1

fill_triangle_issue1
fill_triangle_issue2

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions