Advertisement
jig487

depth calculation functions

Jan 3rd, 2023
1,003
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.18 KB | None | 0 0
  1. --Returns interpolated X value
  2. local function intY(a,b,y)
  3.     return a.x + ((y - a.y) * (b.x - a.x)) / (b.y - a.y)
  4. end
  5.  
  6. --Returns interpolated Y value
  7. local function intX(a,b,x)
  8.     return a.y + ((b.y - a.y) * (x - a.x)) / (b.x - a.x)
  9. end
  10.  
  11. --get the t value for a point (p) somewhere between the line formed from two give points (a,b)
  12. local function getT(a,b,p)
  13.     local v1 = vector.new( a.x-b.x, a.y-b.y )
  14.     local v2 = vector.new( a.x-p.x, a.y-p.y )
  15.     return (v1:dot(v2)) / (v1:dot(v1))
  16. end
  17.  
  18. --use the t value of a point to interpolate between two given values (v1,v2)
  19. --for interpolating between z's, v1 = starting z value, v2 = ending z value
  20. local function lerp(v1,v2,t)
  21.     return (1 - t) * v1 + t * v2
  22. end
  23.  
  24. local function pPix(x,y,c)
  25.     term.setCursorPos(x,y)
  26.     term.setBackgroundColor(colors.white)
  27.     term.setTextColor(colors.black)
  28.     term.write(c)
  29.     term.setBackgroundColor(colors.black)
  30.     term.setTextColor(colors.white)
  31. end
  32.  
  33.  
  34. local function fillLerp(a,b,c)
  35.     if a.y < b.y then a,b = b,a end
  36.     if a.y < c.y then a,c = c,a end
  37.     if b.x > c.x then b,c = c,b end
  38.  
  39.     for y = b.y, a.y-1 do
  40.         local xStart = intY(a,b,y)
  41.         local xEnd = intY(a,c,y)
  42.  
  43.         local p1 = vector.new(xStart,y)
  44.         local p2 = vector.new(xEnd,y)
  45.  
  46.         local t1 = getT(a,b,p1)
  47.         local t2 = getT(a,c,p2)
  48.  
  49.         local z1 = lerp(a.z,b.z,t1)
  50.         local z2 = lerp(a.z,c.z,t2)
  51.  
  52.         for x = xStart, xEnd do
  53.             local t3 = (x - xStart) / (xEnd - xStart)
  54.             local z = math.floor(lerp(z1,z2,t3))
  55.             pPix(x,y,z)
  56.         end
  57.     end
  58. end
  59.  
  60. local function fillInt(a,b,c)
  61.     if a.y < b.y then a,b = b,a end
  62.     if a.y < c.y then a,c = c,a end
  63.     if b.x > c.x then b,c = c,b end
  64.  
  65.     for y = b.y, a.y-1 do
  66.         local xStart = intY(a,b,y)
  67.         local xEnd = intY(a,c,y)
  68.  
  69.         local p1 = vector.new(xStart,y)
  70.         local p2 = vector.new(xEnd,y)
  71.  
  72.         local t1 = getT(a,b,p1)
  73.         local t2 = getT(a,c,p2)
  74.  
  75.         local z1 = lerp(a.z,b.z,t1)
  76.         local z2 = lerp(a.z,c.z,t2)
  77.  
  78.         local startScanDepth = { x = xStart, y = z1}
  79.         local endScanDepth = { x = xEnd, y = z2}
  80.  
  81.         for x = xStart, xEnd do
  82.             local z = math.floor( intX( startScanDepth , endScanDepth, x ) )
  83.             pPix(x,y,z)
  84.         end
  85.     end
  86. end
  87.  
  88. local function fillYInt(a,b,c)
  89.     if a.y < b.y then a,b = b,a end
  90.     if a.y < c.y then a,c = c,a end
  91.     if b.x > c.x then b,c = c,b end
  92.  
  93.     for y = b.y, a.y-1 do
  94.         local xStart = intY(a,b,y)
  95.         local xEnd = intY(a,c,y)
  96.  
  97.         local azp = vector.new(a.z,a.y)
  98.         local bzp = vector.new(b.z,b.y)
  99.         local czp = vector.new(c.z,c.y)
  100.  
  101.         local z1 = intY(azp,bzp,y)
  102.         local z2 = intY(azp,czp,y)
  103.  
  104.         local startScanDepth = { x = z1, y = xStart}
  105.         local endScanDepth = { x = z2, y = xEnd}
  106.  
  107.         for x = xStart, xEnd do
  108.             local z = math.floor( intY( startScanDepth , endScanDepth, x ) )
  109.             pPix(x,y,z)
  110.         end
  111.     end
  112. end
  113.  
  114. local fill = fillYInt
  115.  
  116. local a = vector.new(20,20,9)
  117. local b = vector.new(2,2,5)
  118. local c = vector.new(30,2,8)
  119.  
  120. term.clear()
  121. fill(a,b,c)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement