Advertisement
9551

lerp testing

Nov 3rd, 2021
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.56 KB | None | 0 0
  1. local function intY(a,b,y)
  2.     return a.x + ((y - a.y) * (b.x - a.x)) / (b.y - a.y)
  3. end
  4.  
  5. --get the t value for a point (p) somewhere between the line formed from two give points (a,b)
  6. local function getT(a,b,p)
  7.     local v1 = vector.new( a.x-b.x, a.y-b.y )
  8.     local v2 = vector.new( a.x-p.x, a.y-p.y )
  9.     return (v1:dot(v2)) / (v1:dot(v1))
  10. end
  11.  
  12. --use the t value of a point to interpolate between two given values (v1,v2)
  13. --for interpolating between z's, v1 = starting z value, v2 = ending z value
  14. local function lerp(v1,v2,t)
  15.     return (1 - t) * v1 + t * v2
  16. end
  17.  
  18. local function pPix(x,y,c)
  19.     term.setCursorPos(x,y)
  20.     term.setBackgroundColor(colors.white)
  21.     term.setTextColor(colors.black)
  22.     term.write(c)
  23.     term.setBackgroundColor(colors.black)
  24.     term.setTextColor(colors.white)
  25. end
  26.  
  27. local function fill(a,b,c)
  28.     if a.y < b.y then a,b = b,a end
  29.     if a.y < c.y then a,c = c,a end
  30.     if b.x > c.x then b,c = c,b end
  31.  
  32.     for y = b.y, a.y-1 do
  33.         local xStart = intY(a,b,y)
  34.         local xEnd = intY(a,c,y)
  35.  
  36.         local p1 = vector.new(xStart,y)
  37.         local p2 = vector.new(xEnd,y)
  38.  
  39.         local t1 = getT(a,b,p1)
  40.         local t2 = getT(a,c,p2)
  41.  
  42.         local z1 = lerp(a.z,b.z,t1)
  43.         local z2 = lerp(a.z,c.z,t2)
  44.  
  45.         for x = xStart, xEnd do
  46.             local t3 = (x - xStart) / (xEnd - xStart)
  47.             local z = math.floor(lerp(z1,z2,t3))
  48.             pPix(x,y,z)
  49.         end
  50.     end
  51. end
  52.  
  53. local a = vector.new(20,20,9)
  54. local b = vector.new(2,2,1)
  55. local c = vector.new(30,2,4)
  56.  
  57. term.clear()
  58. fill(a,b,c)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement