KrYn0MoRe

cool math functions v2

Nov 10th, 2020 (edited)
840
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.12 KB | None | 0 0
  1. local folder = Instance.new("WorldModel",script)
  2. local sc = 0
  3.  
  4. function swait(t)
  5.     sc = sc + 1
  6.     if sc >= t then
  7.         sc = 0
  8.         game:GetService("RunService").Heartbeat:Wait()
  9.     end
  10. end
  11.  
  12. function donut(s)
  13.     local r = s/2
  14.     local l = 100 + s*10
  15.     local center = CFrame.new(0,s*2,-50)
  16.     for i = 1,l do
  17.         local x2 = r*math.cos(i*l/r)
  18.         local y2 = r*math.sin(i*l/r)
  19.         local scale = s/((s-s/4)-math.cos(2*i))
  20.         local z2 = math.sin(x2-y2)+math.cos(x2+y2)+math.cos(x2/y2)
  21.         local xy = CFrame.new(x2,y2,0)
  22.         local part = Instance.new("Part")
  23.         part.Color = Color3.fromHSV(math.abs(x2),255,255)
  24.         part.Size = Vector3.new(1,1,1)
  25.         part.CFrame = xy*center
  26.         part.Anchored = true
  27.         part.Parent = folder
  28.     end
  29. end
  30.  
  31. function infinity(s)
  32.     game:GetService("RunService").Stepped:Connect(function(t)
  33.         local center = CFrame.new(0,s*2,-50)
  34.         local scale = s/((s-s/4)-math.cos(2*t))
  35.         local x = scale*math.cos(t)
  36.         local y = scale*math.sin(2*t)/2
  37.         local z = scale*math.sin(2/t)*2
  38.         local x2 = ((x+1)*s)
  39.         local y2 = ((y-1)*s)
  40.         local z2 = (x2+y2)
  41.         local xy = CFrame.new(x2,y2,0)
  42.         local part = Instance.new("Part")
  43.         part.Material = Enum.Material.Neon
  44.         part.Color = Color3.fromHSV(math.abs(x),255,255)
  45.         part.Size = Vector3.new(1,1,1)
  46.         part.CFrame = xy*center
  47.         part.Anchored = true
  48.         part.Parent = folder
  49.     end)
  50. end
  51.  
  52. function infinity2(s)
  53.     local part = Instance.new("Part")
  54.     part.Material = Enum.Material.Neon
  55.     part.Size = Vector3.new(1,1,1)
  56.     part.Anchored = true
  57.     part.Parent = folder
  58.     local att1,att2 = Instance.new("Attachment"),Instance.new("Attachment")
  59.     att1.Position = Vector3.new(0,-0.5,0)
  60.     att2.Position = Vector3.new(0,0.5,0)
  61.     att1.Parent = part
  62.     att2.Parent = part
  63.     local trail = Instance.new("Trail")
  64.     trail.Parent = part
  65.     trail.Attachment0 = att1
  66.     trail.Attachment1 = att2
  67.     trail.FaceCamera = true
  68.     trail.LightInfluence = 1
  69.     trail.Transparency = NumberSequence.new(0,1)
  70.     trail.MinLength = 0
  71.     trail.Lifetime = 10
  72.     game:GetService("RunService").Heartbeat:Connect(function(t)
  73.         local center = CFrame.new(0,s*2,-50)
  74.         local scale = s/((s-s/4)-math.cos(2*t))
  75.         local x = scale*math.cos(t)
  76.         local y = scale*math.sin(2*t)/2
  77.         local z = scale*math.sin(2/t)*2
  78.         local x2 = ((x+1)*s)
  79.         local y2 = ((y-1)*s)
  80.         local z2 = (x2+y2)
  81.         local xy = CFrame.new(x2,y2,0)
  82.         game:GetService("TweenService"):Create(part,TweenInfo.new(1),{CFrame = xy*center}):Play()
  83.         trail.Color = ColorSequence.new(Color3.fromHSV(math.abs(x),255,255))
  84.     end)
  85. end
  86.  
  87. local sc = 0
  88.  
  89. function swait(t)
  90.     sc = sc + 1
  91.     if sc >= t then
  92.         sc = 0
  93.         game:GetService("RunService").Heartbeat:Wait()
  94.     end
  95. end
  96.  
  97. function mandelbrot(s)
  98.     local width = s
  99.     local height, wscale = width, 2/width
  100.     local m, limit2 = 100, 16
  101.     local iter = 0
  102.     local center = CFrame.new(0,0,-50)
  103.     local div = 10
  104.     local size = width/height/div
  105.     local i = 0
  106.  
  107.     local pf = Instance.new("Folder",script)
  108.     local pt = {}
  109.  
  110.     local colors = {}
  111.  
  112.     for n = 1,m do
  113.         local hu = math.sqrt(n/m)
  114.         colors[n] = Color3.fromHSV(hu, 255, 150)
  115.     end
  116.     local function render()
  117.         i = i + 1
  118.         for y = 1,height do
  119.             for x = 1,width do
  120.                 local a = x * wscale - 1.5
  121.                 local b = 2*y / height - 1
  122.  
  123.                 local ca = a
  124.                 local cb = b
  125.  
  126.                 local n = 0
  127.                 local sin = 1 -- math.sin(i/10)
  128.  
  129.                 while (n < m) do
  130.                     local aa = a * a - b * b
  131.                     local bb = 2 * a * b
  132.                     a = aa + ca * sin
  133.                     b = bb + cb
  134.                     if (a * a + b * b > limit2) then
  135.                         break
  136.                     end
  137.                     n = n + 1
  138.                 end
  139.  
  140.                 local bright = math.sqrt((n*limit2)%255)
  141.                 local color = colors[n]
  142.                 --color = Color3.new(bright,bright,bright)
  143.                 if (n == m) then
  144.                     bright = 0
  145.                     color = Color3.new()
  146.                 end
  147.                 local tp = ((y-1)*(width*1))+x
  148.                 local vs = Vector3.new(size,size,size*(bright*2))
  149.                 vs = Vector3.new(size,size,size)
  150.                 if 1 >= i then
  151.                     local f = Instance.new('Part')
  152.                     f.Material = Enum.Material.SmoothPlastic
  153.                     f.Massless = true
  154.                     f.Anchored = true
  155.                     f.CanCollide = false
  156.                     f.CastShadow = false
  157.                     f.Color = color
  158.                     f.Size = Vector3.new(1, 1, 1)
  159.                     local mesh = Instance.new("BlockMesh")
  160.                     mesh.Scale = vs
  161.                     mesh.Parent = f
  162.                     f.CFrame = center*CFrame.new(x*size,y*size,0)
  163.                     f.Parent = pf
  164.                     pt[tp] = f
  165.                 else
  166.                     pt[tp].Color = color
  167.                     if pt[tp]:FindFirstChildOfClass("BlockMesh") then
  168.                         pt[tp]:FindFirstChildOfClass("BlockMesh").Scale = vs
  169.                     end
  170.                 end
  171.                 swait(500)
  172.             end
  173.         end
  174.     end
  175.     spawn(function()
  176.         while wait(0) do
  177.             render()
  178.         end
  179.     end)
  180. end
  181.  
  182. function tree(bl)
  183.     local center = CFrame.new(0,bl/2,-50)
  184.     local base = Instance.new("Part")
  185.     base.Material = Enum.Material.SmoothPlastic
  186.     base.Color = Color3.new(1,0,0)
  187.     base.Size = Vector3.new()
  188.     base.CFrame = center
  189.     base.CastShadow = false
  190.     base.Massless = true
  191.     base.Anchored = true
  192.     base.CanCollide = false
  193.     base.Parent = folder
  194.     local branches = {base}
  195.     local function make_part(r)
  196.         local p = base:Clone()
  197.         p.Color = Color3.new(1-(r/bl),0,0)
  198.         p.Parent = folder
  199.         return p
  200.     end
  201.     local function connect(p1,p2,r)
  202.         local a1 = Instance.new("Attachment",p1)
  203.         local a2 = Instance.new("Attachment",p2)
  204.         local bar = Instance.new("RodConstraint")
  205.         bar.Attachment0 = a1
  206.         bar.Attachment1 = a2
  207.         bar.Color = BrickColor.new(Color3.new(1-(r/bl),0,0))
  208.         bar.Parent = p1
  209.         bar.Visible = true
  210.     end
  211.     local function extend(t, branch_length, shorten_by, angle, r)
  212.         local new_length = 0
  213.         local min_length = 5
  214.         local div = 10
  215.         if branch_length > min_length then
  216.             local new = {}
  217.             for i,v in pairs(t) do
  218.                 local up = make_part(r)
  219.                 up.CFrame = v.CFrame*CFrame.new(0,branch_length/div,0) -- up
  220.                 local left = make_part(r)
  221.                 left.CFrame = up.CFrame*CFrame.Angles(0,0,math.rad(angle))*CFrame.new(0,branch_length/div,0) -- left
  222.                 local right = make_part(r)
  223.                 right.CFrame = up.CFrame*CFrame.Angles(0,0,-math.rad(angle))*CFrame.new(0,branch_length/div,0) -- right
  224.                 connect(up,v,r)
  225.                 connect(left,up,r)
  226.                 connect(right,up,r)
  227.                 table.insert(new,1,left)
  228.                 table.insert(new,1,right)
  229.                 swait(100)
  230.             end
  231.             new_length = branch_length - shorten_by
  232.             r = r + shorten_by
  233.             extend(new, new_length, shorten_by, angle, r)
  234.         end
  235.     end
  236.     extend(branches, bl, 5, 30, 1)
  237. end
  238.  
  239. function pi(s)
  240.     local Part0 = Instance.new("Part")
  241.     local SurfaceGui1 = Instance.new("SurfaceGui")
  242.     local TextLabel2 = Instance.new("TextBox")
  243.     Part0.Parent = folder
  244.     Part0.CFrame = CFrame.new(2.6149931, 8.83000565, -12.8999939, -1, 0, 0, 0, 1, 0, 0, 0, -1)
  245.     Part0.Orientation = Vector3.new(0, 180, 0)
  246.     Part0.Position = Vector3.new(2.6149931, 8.83000565, -20)
  247.     Part0.Rotation = Vector3.new(-180, 0, -180)
  248.     Part0.Color = Color3.new(0.0666667, 0.0666667, 0.0666667)
  249.     Part0.Size = Vector3.new(19.2099972, 5.33999777, 2)
  250.     Part0.Anchored = true
  251.     Part0.BottomSurface = Enum.SurfaceType.Smooth
  252.     Part0.BrickColor = BrickColor.new("Really black")
  253.     Part0.CanCollide = false
  254.     Part0.Material = Enum.Material.SmoothPlastic
  255.     Part0.TopSurface = Enum.SurfaceType.Smooth
  256.     Part0.brickColor = BrickColor.new("Really black")
  257.     SurfaceGui1.Parent = Part0
  258.     SurfaceGui1.CanvasSize = Vector2.new(400, 60)
  259.     TextLabel2.Parent = SurfaceGui1
  260.     TextLabel2.Size = UDim2.new(1, 0, 1, 0)
  261.     TextLabel2.BackgroundColor = BrickColor.new("Institutional white")
  262.     TextLabel2.BackgroundColor3 = Color3.new(1, 1, 1)
  263.     TextLabel2.BackgroundTransparency = 1
  264.     TextLabel2.BorderSizePixel = 0
  265.     TextLabel2.Font = Enum.Font.SourceSansBold
  266.     TextLabel2.FontSize = Enum.FontSize.Size36
  267.     TextLabel2.Text = "Retreiving data.."
  268.     TextLabel2.TextColor = BrickColor.new("Institutional white")
  269.     TextLabel2.TextColor3 = Color3.new(1, 1, 1)
  270.     TextLabel2.TextScaled = true
  271.     TextLabel2.TextSize = 36
  272.     TextLabel2.TextStrokeTransparency = 0
  273.     TextLabel2.TextWrap = true
  274.     TextLabel2.TextWrapped = true
  275.  
  276.     local text = TextLabel2
  277.  
  278.     wait()
  279.  
  280.     local n = 0
  281.     local bs = s
  282.     local start = 1
  283.     local add = true
  284.     game:GetService("RunService").Heartbeat:Connect(function()
  285.         for i = start,s do
  286.             i = (i*2)-1
  287.             if add then
  288.                 n = n + 1/i
  289.             else
  290.                 n = n - 1/i
  291.             end
  292.             add = not add
  293.         end
  294.         start = s
  295.         s = s + bs
  296.         text.Text = tostring(n*4) -- pi is 1/4 using this method
  297.     end)
  298. end
  299.  
  300. function triangle(s)
  301.     local width = s
  302.     local height = s
  303.     local center = CFrame.new(0,0,-50)
  304.     local div = 10
  305.     local size = width/height/div
  306.     local i = 0
  307.     local pf = Instance.new("Folder",script)
  308.     local pt = {}
  309.  
  310.     local function render()
  311.         i = i + 1
  312.         local m = {}
  313.         m[math.floor(width/2)] = true
  314.         for y = height, 1, -1 do
  315.             local n = {}
  316.             for x = 1, width do
  317.                 local color = Color3.new()
  318.                 if m[x] then
  319.                     color = Color3.new(1,1,1)
  320.                     n[x+1] = not n[x+1]
  321.                     n[x-1] = not n[x-1]
  322.                 end
  323.                 local tp = ((y-1)*(width*1))+x
  324.                 if 1 >= i then
  325.                     local f = Instance.new('Part')
  326.                     f.Material = Enum.Material.SmoothPlastic
  327.                     f.Massless = true
  328.                     f.Anchored = true
  329.                     f.CanCollide = false
  330.                     f.CastShadow = false
  331.                     f.Color = color
  332.                     f.Size = Vector3.new(1, 1, 1)
  333.                     local mesh = Instance.new("BlockMesh")
  334.                     mesh.Scale = Vector3.new(size,size,size)
  335.                     mesh.Parent = f
  336.                     f.CFrame = center*CFrame.new(x*size,y*size,0)
  337.                     f.Parent = pf
  338.                     pt[tp] = f
  339.                 else
  340.                     pt[tp].Color = color
  341.                 end
  342.                 swait(50)
  343.             end
  344.             m = n
  345.         end
  346.     end
  347.     while true do
  348.         render()
  349.     end
  350. end
  351.  
  352. local plr = owner -- sandbox value
  353.  
  354. plr.Chatted:Connect(function(msg)
  355.     if msg:lower():sub(1,5) == 'inf2/' then
  356.         local s = msg:sub(6)
  357.         if tonumber(s) then
  358.             infinity2(tonumber(s))
  359.         end
  360.     elseif msg:lower():sub(1,4) == 'inf/' then
  361.         local s = msg:sub(5)
  362.         if tonumber(s) then
  363.             infinity(tonumber(s))
  364.         end
  365.     elseif msg:lower():sub(1,6) == 'donut/' then
  366.         local s = msg:sub(7)
  367.         if tonumber(s) then
  368.             donut(tonumber(s))
  369.         end
  370.     elseif msg:lower():sub(1,5) == 'tree/' then
  371.         local s = msg:sub(6)
  372.         if tonumber(s) then
  373.             tree(tonumber(s))
  374.         end
  375.     elseif msg:lower():sub(1,8) == 'fractal/' then
  376.         local s = msg:sub(9)
  377.         if tonumber(s) then
  378.             mandelbrot(tonumber(s))
  379.         end
  380.     elseif msg:lower():sub(1,3) == 'pi/' then
  381.         local s = msg:sub(4)
  382.         if tonumber(s) then
  383.             pi(tonumber(s))
  384.         end
  385.     elseif msg:lower():sub(1,9) == 'triangle/' then
  386.         local s = msg:sub(10)
  387.         if tonumber(s) then
  388.             triangle(tonumber(s))
  389.         end
  390.     end
  391. end)
Add Comment
Please, Sign In to add comment