Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local points = {
- Vector2.new(0, 0),
- Vector2.new(2, 3),
- Vector2.new(5, 8),
- Vector2.new(9, 10)
- }
- -- Function to calculate the distance between two Vector2 points
- local function calculateDistance(p1, p2)
- return (p2 - p1).Magnitude
- end
- -- Function to equally space out the points
- local function spaceOutPoints(points)
- local totalDistance = 0
- local segmentDistances = {}
- -- Step 1: Calculate the total distance between consecutive points
- for i = 1, #points - 1 do
- local distance = calculateDistance(points[i], points[i + 1])
- totalDistance = totalDistance + distance
- table.insert(segmentDistances, distance)
- end
- -- Step 2: Determine the equal distance between each point
- local equalDistance = totalDistance / (#points - 1)
- -- Step 3: Create the new spaced-out points table
- local newPoints = {}
- table.insert(newPoints, points[1]) -- Start with the first point
- local currentPosition = points[1]
- local remainingDistance = equalDistance
- for i = 1, #segmentDistances do
- local segmentDistance = segmentDistances[i]
- local startPoint = points[i]
- local endPoint = points[i + 1]
- -- Calculate the direction of the segment
- local direction = (endPoint - startPoint).Unit
- -- Distribute points along the segment at equal distance
- while remainingDistance <= segmentDistance do
- local newPoint = startPoint + direction * remainingDistance
- table.insert(newPoints, newPoint)
- -- Move to the new position
- currentPosition = newPoint
- segmentDistance = segmentDistance - remainingDistance
- remainingDistance = equalDistance
- end
- -- If the current segment isn't enough to fit another full equal distance point, move to the next segment
- remainingDistance = remainingDistance - segmentDistance
- end
- -- Ensure the last point is included
- table.insert(newPoints, points[#points])
- return newPoints
- end
- -- Call the function to space out the points
- local spacedPoints = spaceOutPoints(points)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement