Update merge_sort_scale.py
This commit is contained in:
@@ -1,9 +1,10 @@
|
|||||||
|
from random import randint
|
||||||
import bpy
|
import bpy
|
||||||
import random
|
import random
|
||||||
from mathutils import Vector, Matrix
|
from mathutils import Vector, Matrix
|
||||||
|
|
||||||
#variables
|
#variables
|
||||||
count = 50
|
count = 150
|
||||||
cubes=[]
|
cubes=[]
|
||||||
|
|
||||||
#delete every existing node_group
|
#delete every existing node_group
|
||||||
@@ -55,7 +56,7 @@ node_grp.links.new(arrayCounter.outputs[0], joinStrings.inputs[1])
|
|||||||
node_grp.links.new(arrayString.outputs[0], joinStrings.inputs[1])
|
node_grp.links.new(arrayString.outputs[0], joinStrings.inputs[1])
|
||||||
|
|
||||||
#fill arrays with numbers between 1 & count
|
#fill arrays with numbers between 1 & count
|
||||||
ran = list(range(1,count+1))
|
ran = list(range(0,count))
|
||||||
|
|
||||||
#randomize array order
|
#randomize array order
|
||||||
random.shuffle(ran)
|
random.shuffle(ran)
|
||||||
@@ -83,66 +84,110 @@ i = 0
|
|||||||
for ob in bpy.data.objects:
|
for ob in bpy.data.objects:
|
||||||
if ob.type == 'MESH' and ob.name != "Counter":
|
if ob.type == 'MESH' and ob.name != "Counter":
|
||||||
origin_to_bottom(ob)
|
origin_to_bottom(ob)
|
||||||
ob.scale.z = ran[i]
|
ob.scale.z = ran[i]+1
|
||||||
cubes.append(ob)
|
cubes.append(ob)
|
||||||
i += 1
|
i += 1
|
||||||
|
|
||||||
#sort array based on location.x
|
#sort array based on location.x
|
||||||
cubes.sort(key = lambda obj: obj.location.x)
|
cubes.sort(key = lambda obj: obj.location.x)
|
||||||
|
|
||||||
def mergeSort(cubes):
|
def merge(arr, l, m, r):
|
||||||
if len(cubes) > 1:
|
|
||||||
mid = len(cubes) // 2
|
|
||||||
left = cubes[:mid]
|
|
||||||
right = cubes[mid:]
|
|
||||||
|
|
||||||
# Recursive call on each half
|
global array
|
||||||
mergeSort(left)
|
global iframe
|
||||||
mergeSort(right)
|
|
||||||
|
|
||||||
# Two iterators for traversing the two halves
|
|
||||||
i = 0
|
|
||||||
j = 0
|
|
||||||
|
|
||||||
# Iterator for the main list
|
n1 = m - l + 1
|
||||||
k = 0
|
n2 = r - m
|
||||||
|
|
||||||
while i < len(left) and j < len(right):
|
# create temp arrays
|
||||||
if left[i].scale.z <= right[j].scale.z:
|
L = [0] * (n1)
|
||||||
|
R = [0] * (n2)
|
||||||
|
|
||||||
left[i].location.x = k
|
# Copy data to temp arrays L[] and R[]
|
||||||
|
for i in range(0, n1):
|
||||||
|
L[i] = arr[l + i]
|
||||||
|
|
||||||
# The value from the left half has been used
|
for j in range(0, n2):
|
||||||
cubes[k] = left[i]
|
R[j] = arr[m + 1 + j]
|
||||||
|
|
||||||
# Move the iterator forward
|
# Merge the temp arrays back into arr[l..r]
|
||||||
i += 1
|
i = 0 # Initial index of first subarray
|
||||||
else:
|
j = 0 # Initial index of second subarray
|
||||||
|
k = l # Initial index of merged subarray
|
||||||
|
|
||||||
right[j].location.x = k
|
while i < n1 and j < n2:
|
||||||
|
if L[i].scale.z <= R[j].scale.z:
|
||||||
|
arr[k] = L[i]
|
||||||
|
|
||||||
cubes[k] = right[j]
|
L[i].location.x = k
|
||||||
|
|
||||||
j += 1
|
|
||||||
# Move to the next slot
|
|
||||||
k += 1
|
|
||||||
# For all the remaining values
|
|
||||||
while i < len(left):
|
|
||||||
|
|
||||||
left[i].location.x = k
|
|
||||||
|
|
||||||
cubes[k] = left[i]
|
|
||||||
|
|
||||||
i += 1
|
i += 1
|
||||||
k += 1
|
else:
|
||||||
while j < len(right):
|
arr[k] = R[j]
|
||||||
|
|
||||||
right[j].location.x = k
|
R[j].location.x = k
|
||||||
|
|
||||||
cubes[k]=right[j]
|
|
||||||
|
|
||||||
j += 1
|
j += 1
|
||||||
k += 1
|
k += 1
|
||||||
|
|
||||||
#start recursion
|
for cube in array:
|
||||||
mergeSort(cubes)
|
cube.keyframe_insert(data_path="location", frame=iframe)
|
||||||
|
for cube in L:
|
||||||
|
cube.keyframe_insert(data_path="location", frame=iframe)
|
||||||
|
for cube in R:
|
||||||
|
cube.keyframe_insert(data_path="location", frame=iframe)
|
||||||
|
|
||||||
|
iframe += 1
|
||||||
|
|
||||||
|
# Copy the remaining elements of L[], if there
|
||||||
|
# are any
|
||||||
|
while i < n1:
|
||||||
|
arr[k] = L[i]
|
||||||
|
L[i].location.x = k
|
||||||
|
|
||||||
|
x=0
|
||||||
|
for cube in array:
|
||||||
|
cube.keyframe_insert(data_path="location", frame=iframe)
|
||||||
|
for cube in L:
|
||||||
|
cube.keyframe_insert(data_path="location", frame=iframe)
|
||||||
|
for cube in R:
|
||||||
|
cube.keyframe_insert(data_path="location", frame=iframe)
|
||||||
|
iframe += 1
|
||||||
|
|
||||||
|
i += 1
|
||||||
|
k += 1
|
||||||
|
|
||||||
|
# Copy the remaining elements of R[], if there
|
||||||
|
# are any
|
||||||
|
while j < n2:
|
||||||
|
arr[k] = R[j]
|
||||||
|
|
||||||
|
R[j].location.x = k
|
||||||
|
for cube in array:
|
||||||
|
cube.keyframe_insert(data_path="location", frame=iframe)
|
||||||
|
for cube in L:
|
||||||
|
cube.keyframe_insert(data_path="location", frame=iframe)
|
||||||
|
for cube in R:
|
||||||
|
cube.keyframe_insert(data_path="location", frame=iframe)
|
||||||
|
iframe+=1
|
||||||
|
|
||||||
|
j += 1
|
||||||
|
k += 1
|
||||||
|
# l is for left index and r is right index of the
|
||||||
|
# sub-array of arr to be sorted
|
||||||
|
|
||||||
|
def mergeSort(arr, l, r):
|
||||||
|
if l < r:
|
||||||
|
|
||||||
|
# Same as (l+r)//2, but avoids overflow for
|
||||||
|
# large l and h
|
||||||
|
m = l+(r-l)//2
|
||||||
|
# Sort first and second halves
|
||||||
|
mergeSort(arr, l, m)
|
||||||
|
mergeSort(arr, m+1, r)
|
||||||
|
merge(arr, l, m, r)
|
||||||
|
|
||||||
|
iframe = 0
|
||||||
|
array = cubes
|
||||||
|
mergeSort(cubes, 0, count-1)
|
||||||
Reference in New Issue
Block a user