Advertisement
Guest User

Untitled

a guest
Nov 21st, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.97 KB | None | 0 0
  1. local function smooth( points, steps )
  2.  
  3. if #points < 3 then
  4. return points
  5. end
  6.  
  7. local steps = steps or 5
  8.  
  9. local spline = {}
  10. local count = #points - 1
  11. local p0, p1, p2, p3, x, y
  12.  
  13. for i = 1, count do
  14.  
  15. if i == 1 then
  16. p0, p1, p2, p3 = points[i], points[i], points[i + 1], points[i + 2]
  17. elseif i == count then
  18. p0, p1, p2, p3 = points[#points - 2], points[#points - 1], points[#points], points[#points]
  19. else
  20. p0, p1, p2, p3 = points[i - 1], points[i], points[i + 1], points[i + 2]
  21. end
  22.  
  23. for t = 0, 1, 1 / steps do
  24.  
  25. x = 0.5 * ( ( 2 * p1.x ) + ( p2.x - p0.x ) * t + ( 2 * p0.x - 5 * p1.x + 4 * p2.x - p3.x ) * t * t + ( 3 * p1.x - p0.x - 3 * p2.x + p3.x ) * t * t * t )
  26. y = 0.5 * ( ( 2 * p1.y ) + ( p2.y - p0.y ) * t + ( 2 * p0.y - 5 * p1.y + 4 * p2.y - p3.y ) * t * t + ( 3 * p1.y - p0.y - 3 * p2.y + p3.y ) * t * t * t )
  27.  
  28. --prevent duplicate entries
  29. if not(#spline > 0 and spline[#spline].x == x and spline[#spline].y == y) then
  30. table.insert( spline , { x = x , y = y } )
  31. end
  32.  
  33. end
  34.  
  35. end
  36.  
  37. return spline
  38.  
  39. end
  40.  
  41. local function drawgraph( points )
  42. surface.SetDrawColor( 255, 0, 0, 255 )
  43. draw.NoTexture()
  44. --surface.DrawPoly( points )
  45. for i=1, #points do
  46. local point = points[i]
  47. local pointn = points[i+1]
  48. if pointn then
  49. surface.DrawLine( point.x, point.y, pointn.x, pointn.y )
  50. end
  51. end
  52. end
  53.  
  54. local points = {
  55. {x = 100, y = 200},
  56. {x = 150, y = 100},
  57. {x = 200, y = 150},
  58. {x = 250, y = 100},
  59. {x = 300, y = 125},
  60. {x = 350, y = 150},
  61. {x = 400, y = 135},
  62. {x = 450, y = 170},
  63. {x = 500, y = 200}
  64.  
  65. -- { x = 100, y = 200 },
  66. -- { x = 150, y = 100 },
  67. -- { x = 200, y = 200 }
  68. }
  69. local sline = smooth(points)
  70. --draw(points)
  71.  
  72.  
  73. local spline = smooth( points )
  74.  
  75.  
  76. local frame = vgui.Create("DFrame")
  77. frame:SetSize(ScrW()*0.8, ScrH()*0.8)
  78. frame:Center()
  79. frame:MakePopup()
  80. frame.Paint = function(self, w, h)
  81. draw.RoundedBox(6, 0, 0, w, h, Color(40,40,40,255))
  82. drawgraph(spline)
  83. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement