Advertisement
EmeraldIT

Untitled

Sep 12th, 2024
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.14 KB | None | 0 0
  1. local points = {
  2. Vector2.new(0, 0),
  3. Vector2.new(2, 3),
  4. Vector2.new(5, 8),
  5. Vector2.new(9, 10)
  6. }
  7.  
  8. -- Function to calculate the distance between two Vector2 points
  9. local function calculateDistance(p1, p2)
  10. return (p2 - p1).Magnitude
  11. end
  12.  
  13. -- Function to equally space out the points
  14. local function spaceOutPoints(points)
  15. local totalDistance = 0
  16. local segmentDistances = {}
  17.  
  18. -- Step 1: Calculate the total distance between consecutive points
  19. for i = 1, #points - 1 do
  20. local distance = calculateDistance(points[i], points[i + 1])
  21. totalDistance = totalDistance + distance
  22. table.insert(segmentDistances, distance)
  23. end
  24.  
  25. -- Step 2: Determine the equal distance between each point
  26. local equalDistance = totalDistance / (#points - 1)
  27.  
  28. -- Step 3: Create the new spaced-out points table
  29. local newPoints = {}
  30. table.insert(newPoints, points[1]) -- Start with the first point
  31.  
  32. local currentPosition = points[1]
  33. local remainingDistance = equalDistance
  34.  
  35. for i = 1, #segmentDistances do
  36. local segmentDistance = segmentDistances[i]
  37. local startPoint = points[i]
  38. local endPoint = points[i + 1]
  39.  
  40. -- Calculate the direction of the segment
  41. local direction = (endPoint - startPoint).Unit
  42.  
  43. -- Distribute points along the segment at equal distance
  44. while remainingDistance <= segmentDistance do
  45. local newPoint = startPoint + direction * remainingDistance
  46. table.insert(newPoints, newPoint)
  47.  
  48. -- Move to the new position
  49. currentPosition = newPoint
  50. segmentDistance = segmentDistance - remainingDistance
  51. remainingDistance = equalDistance
  52. end
  53.  
  54. -- If the current segment isn't enough to fit another full equal distance point, move to the next segment
  55. remainingDistance = remainingDistance - segmentDistance
  56. end
  57.  
  58. -- Ensure the last point is included
  59. table.insert(newPoints, points[#points])
  60.  
  61. return newPoints
  62. end
  63.  
  64. -- Call the function to space out the points
  65. local spacedPoints = spaceOutPoints(points)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement