Advertisement
SinisterMemories

Generate Shape

Oct 2nd, 2019
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.06 KB | None | 0 0
  1. -- [[ Service Declarations ]] --
  2.  
  3. local workspace = game:GetService("Workspace")
  4.  
  5. -- [[ Prebuilt Library Declarations ]] --
  6.  
  7. --// Variables
  8.  
  9. local pi = math.pi
  10.  
  11. --// Functions
  12.  
  13. local cos = math.cos
  14. local sin = math.sin
  15.  
  16. -- [[ Line Class Declarations ]] --
  17.  
  18. local line = {}
  19.  
  20. do
  21.     --// Line Methods
  22.    
  23.     function line.new(positionA, positionB, lineWidth)
  24.         local newLine = Instance.new("Part")
  25.  
  26.         local distance = (positionA - positionB).Magnitude
  27.    
  28.         newLine.Anchored = true
  29.        
  30.         newLine.Size = Vector3.new(lineWidth, lineWidth, distance)
  31.         newLine.CFrame = CFrame.new(positionA, positionB) * CFrame.new(lineWidth / 2, 0, -distance / 2)
  32.        
  33.         newLine.Parent = workspace
  34.  
  35.         return newLine 
  36.     end
  37. end
  38.  
  39. -- [[ Shape Class Declarations ]] --
  40.  
  41. local shape = {}
  42.  
  43. do
  44.     --// Shape Methods
  45.    
  46.     function shape.new(pointCount, radius, lineWidth)
  47.         local newShape = {}
  48.        
  49.         do
  50.             --// Calculate Points
  51.            
  52.             local points = {}
  53.            
  54.             local angle = (pi * 2) / pointCount
  55.            
  56.             for i = 1, pointCount do
  57.                 local currentAngle = angle * i
  58.                
  59.                 local xPosition = cos(currentAngle) * radius
  60.                 local zPosition = sin(currentAngle) * radius
  61.                
  62.                 local position = Vector3.new(xPosition, 0, zPosition)
  63.                
  64.                 do
  65.                     --// Render Point
  66.                    
  67.                     local newPoint = Instance.new("Part")
  68.                    
  69.                     newPoint.Anchored = true
  70.                    
  71.                     newPoint.Transparency = .5
  72.                    
  73.                     newPoint.Size = Vector3.new(1, 1, 1)
  74.                     newPoint.CFrame = CFrame.new(position)
  75.                    
  76.                     newPoint.Parent = workspace
  77.                 end
  78.                
  79.                 points[#points + 1] = position
  80.             end
  81.            
  82.             newShape.points = points
  83.         end
  84.        
  85.         do
  86.             --// Generate Lines
  87.            
  88.             local points = newShape.points
  89.            
  90.             for i = 1, #points do
  91.                 local index = (i % #points) + 1
  92.                 local nextIndex = ((i + 1) % #points) + 1
  93.                
  94.                 local currentPoint = points[index]
  95.                 local nextPoint = points[nextIndex]
  96.                
  97.                 line.new(currentPoint, nextPoint, lineWidth)
  98.             end
  99.         end
  100.        
  101.         return newShape
  102.     end
  103. end
  104.  
  105. -- [[ Init ]] --
  106.  
  107. do
  108.     --// Generate Shape
  109.    
  110.     shape.new(24, 50, 1)
  111. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement