joshb / triangleraster

A graphics demo written in C++ that demonstrates triangle rasterization.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Failing on triangles with both short sides on the left

bamert opened this issue · comments

Hey Josh,

thanks a lot for the code, I'm using it to rasterize triangles in a Delaunay triangulation. I discovered an issue in the case of triangles with both short sides on the left; the spans in this case reach from x1 to x2, where x1 > x2 and so no pixels are drawn in:

for(int x = span.X1; x < span.X2; x++) {

The failure cases are exemplified in the image below (black triangles: failure case, gray: successfully rasterized)
example

I've fixed it by simply swapping the range limits if x2>x1.

Hey there,

That's cool! Glad to hear you found it useful. It's been a while since I worked on this, but it looks like the Span constructor ensures that X1 is less than X2, so I think that case should already be handled in the original code (it probably does make more sense to do the swapping when drawing the span, though)