Advertisement
emiellr

bezier.lua

Jul 7th, 2022
659
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function lerp(a, b, t)
  2.     return a + (b-a)*t
  3. end
  4.  
  5. width, height = term.getSize()
  6. midw = math.floor(width/2)
  7. midh = math.floor(height/2)
  8.  
  9. p0 = vector.new(1, 1, 0)
  10. p2 = vector.new(width, height, 0)
  11.  
  12. function quad_bezier(p0, p1, p2, step)
  13.  
  14.     local oldx = p0.x
  15.     local oldy = p0.y
  16.  
  17.     for t=0, 1, step do
  18.         local x1 = lerp(p0.x, p1.x, t)
  19.         local y1 = lerp(p0.y, p1.y, t)
  20.         local x2 = lerp(p1.x, p2.x, t)
  21.         local y2 = lerp(p1.y, p2.y, t)
  22.         local x = lerp(x1, x2, t)
  23.         local y = lerp(y1, y2, t)
  24.        
  25.         paintutils.drawLine(oldx, oldy, x, y, colors.white)
  26.  
  27.         oldx = x
  28.         oldy = y
  29.     end
  30. end
  31.  
  32. while true do
  33.     local p1 = vector.new(1, height, 0)
  34.     local event, button, x, y = os.pullEvent("mouse_drag")
  35.     term.setBackgroundColor(colors.black)
  36.     term.clear()
  37.     p1.x, p1.y = x, y
  38.     quad_bezier(p0, p1, p2, 0.2)
  39. end
  40.  
  41. -- paintutils.drawLine(p0.x, p0.y, p1.x, p1.y, colors.white)
Advertisement
RAW Paste Data Copied
Advertisement