Added counting + fixed gap
This commit is contained in:
138
InsertionSort.py
138
InsertionSort.py
@@ -2,6 +2,79 @@ import bpy
|
|||||||
import random
|
import random
|
||||||
from mathutils import Vector, Matrix
|
from mathutils import Vector, Matrix
|
||||||
|
|
||||||
|
############################################################
|
||||||
|
# Insertion Sort Algorithm
|
||||||
|
############################################################
|
||||||
|
|
||||||
|
def insertionSort(cubes, arrayCounter, comparisonCounter):
|
||||||
|
#start at frame 0
|
||||||
|
iframe=0
|
||||||
|
originFrame = 0
|
||||||
|
|
||||||
|
for i in range(0, len(cubes)):
|
||||||
|
#defines key_item that is compared until correct location
|
||||||
|
key_item = cubes[i]
|
||||||
|
key_item.location.x = i
|
||||||
|
|
||||||
|
j = i - 1
|
||||||
|
|
||||||
|
#add 2 to array counter
|
||||||
|
arrayCounter.inputs[0].default_value += 2
|
||||||
|
arrayCounter.inputs[0].keyframe_insert(data_path='default_value', frame=iframe)
|
||||||
|
|
||||||
|
while j >= 0 and cubes[j].scale.z > key_item.scale.z:
|
||||||
|
#sets position of item in array
|
||||||
|
cubes[j + 1] = cubes[j]
|
||||||
|
|
||||||
|
#sets location
|
||||||
|
cubes[j + 1].location.x = j
|
||||||
|
cubes[j].location.x = j + 1
|
||||||
|
|
||||||
|
j -= 1
|
||||||
|
|
||||||
|
#adding keyframes to all cubes whenever one position/location is shifted
|
||||||
|
for cube in cubes:
|
||||||
|
cube.keyframe_insert(data_path="location", frame=iframe)
|
||||||
|
|
||||||
|
#add 2 to array counter
|
||||||
|
arrayCounter.inputs[0].default_value += 2
|
||||||
|
arrayCounter.inputs[0].keyframe_insert(data_path='default_value', frame=iframe)
|
||||||
|
|
||||||
|
#add 1 to comparison counter
|
||||||
|
comparisonCounter.inputs[0].default_value += 1
|
||||||
|
comparisonCounter.inputs[0].keyframe_insert(data_path='default_value', frame=iframe)
|
||||||
|
|
||||||
|
#next frame
|
||||||
|
iframe+=1
|
||||||
|
|
||||||
|
#place key_item into correct position/location
|
||||||
|
cubes[j + 1] = key_item
|
||||||
|
cubes[j + 1].location.x = i
|
||||||
|
|
||||||
|
#add 1 to array counter
|
||||||
|
arrayCounter.inputs[0].default_value += 1
|
||||||
|
arrayCounter.inputs[0].keyframe_insert(data_path='default_value', frame=iframe)
|
||||||
|
|
||||||
|
#origin and target index of key_item in array
|
||||||
|
origin = i
|
||||||
|
target = j + 1
|
||||||
|
|
||||||
|
#set location/position for key_item + add keyframes
|
||||||
|
x = 0
|
||||||
|
while x <= (origin-target):
|
||||||
|
key_item.location.x = origin - x
|
||||||
|
key_item.keyframe_insert(data_path="location", frame= originFrame + x - 1)
|
||||||
|
|
||||||
|
x += 1
|
||||||
|
|
||||||
|
originFrame = iframe
|
||||||
|
|
||||||
|
############################################################
|
||||||
|
# Setup Random Cubes + Array to be sorted
|
||||||
|
############################################################
|
||||||
|
|
||||||
|
def setUpCubeArray():
|
||||||
|
|
||||||
#variables
|
#variables
|
||||||
count = 50
|
count = 50
|
||||||
cubes=[]
|
cubes=[]
|
||||||
@@ -36,8 +109,9 @@ arrayString = node_grp.nodes.new("FunctionNodeInputString")
|
|||||||
arrayCounter = node_grp.nodes.new("FunctionNodeValueToString")
|
arrayCounter = node_grp.nodes.new("FunctionNodeValueToString")
|
||||||
groupOutput = node_grp.nodes.new('NodeGroupOutput')
|
groupOutput = node_grp.nodes.new('NodeGroupOutput')
|
||||||
|
|
||||||
#90 degree rotation of the counter object
|
#90 degree rotation of the transform node of counter object
|
||||||
transform.inputs[2].default_value[0] = 1.5708
|
transform.inputs[2].default_value[0] = 1.5708
|
||||||
|
|
||||||
#set default values of some nodes
|
#set default values of some nodes
|
||||||
comparisonString.string = "Comparisons:"
|
comparisonString.string = "Comparisons:"
|
||||||
arrayString.string = "Array Accesses:"
|
arrayString.string = "Array Accesses:"
|
||||||
@@ -55,7 +129,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-1))
|
||||||
|
|
||||||
#randomize array order
|
#randomize array order
|
||||||
random.shuffle(ran)
|
random.shuffle(ran)
|
||||||
@@ -72,73 +146,33 @@ def origin_to_bottom(ob, matrix=Matrix()):
|
|||||||
mw.translation = mw @ o
|
mw.translation = mw @ o
|
||||||
|
|
||||||
#create cubes with random location
|
#create cubes with random location
|
||||||
for i in range(count):
|
for i in range(count-1):
|
||||||
bpy.ops.mesh.primitive_cube_add(location=(ran[i], 0, 0), scale=(0.25, 0.25, 0.25))
|
bpy.ops.mesh.primitive_cube_add(location=(ran[i], 0, 0), scale=(0.25, 0.25, 0.25))
|
||||||
|
|
||||||
#shuffle array
|
#shuffle array
|
||||||
random.shuffle(ran)
|
random.shuffle(ran)
|
||||||
|
|
||||||
#assign random scale to all cubes and add them to array
|
#assign random scale to all cubes and add them to array
|
||||||
i = 0
|
s = 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[s]+1
|
||||||
cubes.append(ob)
|
cubes.append(ob)
|
||||||
i += 1
|
s += 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)
|
||||||
|
|
||||||
#start at frame 0
|
return (cubes, arrayCounter, comparisonCounter)
|
||||||
iframe=0
|
|
||||||
originFrame = 0
|
|
||||||
|
|
||||||
#insertion sort alogrithm
|
############################################################
|
||||||
for i in range(0, count):
|
# Call Functions
|
||||||
|
############################################################
|
||||||
|
|
||||||
#defines key_item that is compared until correct location
|
cubes, arrayCounter, comparisonCounter = setUpCubeArray()
|
||||||
key_item = cubes[i]
|
|
||||||
key_item.location.x = i
|
|
||||||
j = i - 1
|
|
||||||
print(i)
|
|
||||||
while j >= 0 and cubes[j].scale.z > key_item.scale.z:
|
|
||||||
|
|
||||||
#sets position of item in array
|
insertionSort(cubes, arrayCounter, comparisonCounter)
|
||||||
cubes[j + 1] = cubes[j]
|
|
||||||
cubes[j + 1].location.x = j
|
|
||||||
|
|
||||||
#sets location
|
|
||||||
cubes[j].location.x = j + 1
|
|
||||||
j -= 1
|
|
||||||
|
|
||||||
#adding keyframes to all cubes whenever one position/location is shifted
|
|
||||||
for cube in cubes:
|
|
||||||
cube.keyframe_insert(data_path="location", frame=iframe)
|
|
||||||
|
|
||||||
#next frame
|
|
||||||
iframe+=1
|
|
||||||
|
|
||||||
|
|
||||||
#place key_item into correct position/location
|
|
||||||
cubes[j + 1] = key_item
|
|
||||||
cubes[j + 1].location.x = i
|
|
||||||
key_item.location.x = j + 1
|
|
||||||
|
|
||||||
#origin and target index of key_item in array
|
|
||||||
origin = i
|
|
||||||
target = j + 1
|
|
||||||
|
|
||||||
x = 0
|
|
||||||
while x <= (origin-target):
|
|
||||||
|
|
||||||
#set location/position for key_item + add keyframes
|
|
||||||
key_item.location.x = origin - x
|
|
||||||
key_item.keyframe_insert(data_path="location", frame= originFrame + x - 1)
|
|
||||||
|
|
||||||
x += 1
|
|
||||||
|
|
||||||
originFrame = iframe
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user