Created
January 20, 2018 14:07
-
-
Save morgan3d/53d430167aa94da00146bcae839fae3b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// @CasualEffects & @pervognsen | |
var shift = ((border !== TRANSPARENT) && (border !== fill)) ? 0.5 : 0; | |
// For each non-horizontal edge, store: | |
// | |
// [startX, startY, dx/dy slope, vertical height]. | |
// | |
// These are the values needed for the edge-intersection test. Add edges so that the | |
// start Y coordinate is less than the end one. | |
var edgeArray = []; | |
var y0 = height, y1 = -1; | |
function addEdge(Sx, Sy, Ex, Ey) { | |
if (Sy < Ey) { | |
// Update bounding box | |
if (Sy < y0) { y0 = Sy; } | |
if (Ey > y1) { y1 = Ey; } | |
edgeArray.push([Sx, Sy, (Ex - Sx) / (Ey - Sy), Ey - Sy]); | |
} else if (Sy > Ey) { | |
addEdge(Ex, Ey, Sx, Sy); | |
} | |
} | |
addEdge(Ax, Ay, Bx, By); | |
addEdge(Bx, By, Cx, Cy); | |
addEdge(Cx, Cy, Ax, Ay); | |
// Intentionally left as a float to avoid float to int conversion for the inner loop | |
y0 = Math.max(0, Math.floor(y0)); | |
y1 = Math.min(height - 1, Math.floor(y1)); | |
for (var y = y0; y <= y1; ++y) { | |
// For this scanline, intersect the edge lines of the triangle. | |
// As a convex polygon, we can simply intersect ALL edges and then | |
// take the min and max intersections. | |
var x0 = Infinity, x1 = -Infinity; | |
for (var i = edgeArray.length - 1; i >= 0; --i) { | |
var seg = edgeArray[i]; | |
var ry = y - seg[1]; | |
if ((ry >= 0) && (ry < seg[3])) { | |
x = seg[0] + ry * seg[2]; | |
if (x < x0) { x0 = x; } | |
if (x > x1) { x1 = x; } | |
} | |
} | |
if ((x0 <= 63.5) && (x1 >= 0)) { | |
_screen.fill(fill, | |
(Math.max(Math.round(x0 + shift) << 0, 0) + (y << logWidth)) | 0, | |
(Math.min(Math.round(x1 - shift) << 0, width - 1) + (y << logWidth) + 1) | 0); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment