Skip to content

Instantly share code, notes, and snippets.

@Screwtapello
Last active June 27, 2020 09:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Screwtapello/afe2519800aeb854915ade2499e0a91a to your computer and use it in GitHub Desktop.
Save Screwtapello/afe2519800aeb854915ade2499e0a91a to your computer and use it in GitHub Desktop.
MAX_X = 320
MAX_Y = 240
POINT_A = {MAX_X/2, 0 } // top middle
POINT_B = {0, MAX_Y} // bottom left
POINT_C = {MAX_X, MAX_Y} // bottom right
// we greyscale, baby!
COLOR_A = 0
COLOR_B = 192
COLOR_C = 128
// for each scanline
for y in 0 .. MAX_Y:
// the left edge of the scanline is y/MAX_Y of the distance
// from the tip's x
// to the left base point's x
left_x = y/MAX_Y * (B.x - A.x) + A.x
// the left edge's color is y/MAX_Y of the distance
// from the tip's color
// to the left base point's color
left_color = y/MAX_Y * (COLOR_B - COLOR_A) + COLOR_A
// the right edge of the scanline is y/MAX_Y of the distance
// from the tip's x
// to the right base point's x
right_x = y/MAX_Y * (C.x - A.x) + A.x
// the right edge's color is y/MAX_Y of the distance
// from the tip's color
// to the right base point's color
right_color = y/MAX_Y * (COLOR_C - COLOR_A) + COLOR_A
// for each pixel in the scanline
for x in left_x to right_x:
// the pixel's color is interpolated
// from the left color to the right color
color = (x - left_x)/(right_x - left_x) * (right_color - left_color) + left_color
// now we can draw!
drawPixel(x, y, color)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment