update selection and shell sort
This commit is contained in:
@@ -1,6 +1,8 @@
|
|||||||
import bpy
|
import bpy
|
||||||
import random
|
import random
|
||||||
|
import math
|
||||||
from array import *
|
from array import *
|
||||||
|
from math import pi
|
||||||
|
|
||||||
############################################################
|
############################################################
|
||||||
# Selection Sort Algorithm
|
# Selection Sort Algorithm
|
||||||
@@ -25,8 +27,8 @@ def selection_sort(arr, count):
|
|||||||
if mat1[0] > mat2[0]:
|
if mat1[0] > mat2[0]:
|
||||||
min_idx = j
|
min_idx = j
|
||||||
|
|
||||||
arr[i].location.x = min_idx / 2
|
arr[i].location.x = min_idx * 2
|
||||||
arr[min_idx].location.x = i / 2
|
arr[min_idx].location.x = i * 2
|
||||||
|
|
||||||
iframe +=1
|
iframe +=1
|
||||||
arr[i].keyframe_insert(data_path="location", frame= iframe)
|
arr[i].keyframe_insert(data_path="location", frame= iframe)
|
||||||
@@ -35,65 +37,58 @@ def selection_sort(arr, count):
|
|||||||
arr[i], arr[min_idx] = arr[min_idx], arr[i]
|
arr[i], arr[min_idx] = arr[min_idx], arr[i]
|
||||||
|
|
||||||
############################################################
|
############################################################
|
||||||
# Setup Random Cubes + Array to be sorted
|
# Setup Random Colors + Array to be sorted
|
||||||
############################################################
|
############################################################
|
||||||
|
|
||||||
def setup_array(count):
|
def setup_array(count):
|
||||||
|
|
||||||
|
#fill array with numbers between 0 & count - 1
|
||||||
|
index = list(range(count))
|
||||||
|
|
||||||
|
#initialize 2d array
|
||||||
|
Matrix = [[0 for x in range(count)] for y in range(count)]
|
||||||
|
|
||||||
#delete all materials
|
#initialize object and material array
|
||||||
for material in bpy.data.materials:
|
planes = [0 for i in range(count*count)]
|
||||||
material.user_clear()
|
materials = [0 for i in range(count)]
|
||||||
bpy.data.materials.remove(material)
|
|
||||||
|
|
||||||
#delete every existing object
|
#delete every existing object
|
||||||
for ob in bpy.data.objects:
|
for ob in bpy.data.objects:
|
||||||
bpy.data.objects.remove(ob)
|
bpy.data.objects.remove(ob)
|
||||||
|
|
||||||
#fill arrays with numbers between 0 & count-1
|
#delete all existing materials
|
||||||
index = list(range(count))
|
for material in bpy.data.materials:
|
||||||
|
bpy.data.materials.remove(material, do_unlink=True)
|
||||||
#randomize array order
|
|
||||||
random.shuffle(index)
|
|
||||||
|
|
||||||
#initialize 2d array
|
|
||||||
Matrix = [[0 for x in range(count)] for y in range(count)]
|
|
||||||
|
|
||||||
#add count * count cubes
|
|
||||||
for i in range(count):
|
for i in range(count):
|
||||||
for j in range(count):
|
for j in range(count):
|
||||||
bpy.ops.mesh.primitive_cube_add(location=(j/2, 0, i/2), scale=(0.25, 0.25, 0.25))
|
bpy.ops.mesh.primitive_plane_add(location=(j*2, 0, i*2), rotation=(pi / 2, 0, 0), scale=(0.1, 0.1, 0.1))
|
||||||
|
|
||||||
#assign random scale to all cubes and add them to array
|
|
||||||
i = 0
|
|
||||||
j = 0
|
|
||||||
for ob in bpy.data.objects:
|
|
||||||
if ob.type == 'MESH':
|
|
||||||
if i == count:
|
|
||||||
j += 1
|
|
||||||
random.shuffle(index)
|
|
||||||
i = 0
|
|
||||||
mat = bpy.data.materials.new(name="MaterialName") #set new material to variable
|
|
||||||
mat.diffuse_color = (index[i], 255, 255, 255)
|
|
||||||
ob.data.materials.append(mat) #add the material to the object
|
|
||||||
Matrix[j].append(ob)
|
|
||||||
i += 1
|
|
||||||
|
|
||||||
#remove values that are 0 (needs to be solved beforehand)
|
|
||||||
for x in range(count):
|
|
||||||
for i in range(count):
|
|
||||||
Matrix[x].remove(0)
|
|
||||||
|
|
||||||
#presort arrayorder based on location
|
i=0
|
||||||
for i in range(count):
|
for ob in bpy.data.objects:
|
||||||
Matrix[i].sort(key = lambda obj: obj.location.x)
|
planes[i]= ob
|
||||||
|
i+=1
|
||||||
return (Matrix, count)
|
|
||||||
|
#sorts list of all objects based primary on their location.z and secondary on their location.x
|
||||||
|
planes.sort(key = lambda obj: obj.location.z + obj.location.x/(count*count))
|
||||||
|
|
||||||
|
for i in range(count):
|
||||||
|
material = bpy.data.materials.new(name="")
|
||||||
|
material.diffuse_color = (index[i]/3, 255, 255, 255)
|
||||||
|
materials[i] = material
|
||||||
|
|
||||||
|
for i in range(count):
|
||||||
|
random.shuffle(materials)
|
||||||
|
for j in range(count):
|
||||||
|
planes[j+i*count].data.materials.append(materials[j]) #add the material to the object
|
||||||
|
Matrix[i][j] = planes[j+i*count]
|
||||||
|
|
||||||
|
return(Matrix, count)
|
||||||
|
|
||||||
############################################################
|
############################################################
|
||||||
# Call Functions
|
# Call Functions
|
||||||
############################################################
|
############################################################
|
||||||
|
|
||||||
#max count = 31 (needs to be fixed)
|
|
||||||
Matrix, count = setup_array(31)
|
Matrix, count = setup_array(31)
|
||||||
|
|
||||||
#selection_sort every array
|
#selection_sort every array
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
import bpy
|
import bpy
|
||||||
import random
|
import random
|
||||||
|
import math
|
||||||
from array import *
|
from array import *
|
||||||
|
from math import pi
|
||||||
|
|
||||||
############################################################
|
############################################################
|
||||||
# Shell Sort Algorithm
|
# Shell Sort Algorithm
|
||||||
@@ -8,10 +10,12 @@ from array import *
|
|||||||
|
|
||||||
def shell_sort(arr, count):
|
def shell_sort(arr, count):
|
||||||
|
|
||||||
#start at frame 0
|
|
||||||
iframe = 0
|
iframe = 0
|
||||||
|
|
||||||
gap=count//2
|
gap=count//2
|
||||||
|
|
||||||
while gap>0:
|
while gap>0:
|
||||||
|
|
||||||
j=gap
|
j=gap
|
||||||
|
|
||||||
#check the array in from left to right
|
#check the array in from left to right
|
||||||
@@ -34,8 +38,8 @@ def shell_sort(arr, count):
|
|||||||
break
|
break
|
||||||
|
|
||||||
else:
|
else:
|
||||||
arr[i+gap].location.x = i / 2
|
arr[i+gap].location.x = i * 2
|
||||||
arr[i].location.x = (i + gap) / 2
|
arr[i].location.x = (i + gap) * 2
|
||||||
|
|
||||||
arr[i+gap].keyframe_insert(data_path="location", frame= iframe)
|
arr[i+gap].keyframe_insert(data_path="location", frame= iframe)
|
||||||
arr[i].keyframe_insert(data_path="location", frame= iframe)
|
arr[i].keyframe_insert(data_path="location", frame= iframe)
|
||||||
@@ -49,66 +53,59 @@ def shell_sort(arr, count):
|
|||||||
gap=gap//2
|
gap=gap//2
|
||||||
|
|
||||||
############################################################
|
############################################################
|
||||||
# Setup Random Cubes + Array to be sorted
|
# Setup Random Colors + Array to be sorted
|
||||||
############################################################
|
############################################################
|
||||||
|
|
||||||
def setup_array(count):
|
def setup_array(count):
|
||||||
|
|
||||||
|
#fill array with numbers between 0 & count - 1
|
||||||
|
index = list(range(count))
|
||||||
|
|
||||||
|
#initialize 2d array
|
||||||
|
Matrix = [[0 for x in range(count)] for y in range(count)]
|
||||||
|
|
||||||
#delete all materials
|
#initialize object and material array
|
||||||
for material in bpy.data.materials:
|
planes = [0 for i in range(count*count)]
|
||||||
material.user_clear()
|
materials = [0 for i in range(count)]
|
||||||
bpy.data.materials.remove(material)
|
|
||||||
|
|
||||||
#delete every existing object
|
#delete every existing object
|
||||||
for ob in bpy.data.objects:
|
for ob in bpy.data.objects:
|
||||||
bpy.data.objects.remove(ob)
|
bpy.data.objects.remove(ob)
|
||||||
|
|
||||||
#fill arrays with numbers between 0 & count-1
|
#delete all existing materials
|
||||||
index = list(range(count))
|
for material in bpy.data.materials:
|
||||||
|
bpy.data.materials.remove(material, do_unlink=True)
|
||||||
#randomize array order
|
|
||||||
random.shuffle(index)
|
|
||||||
|
|
||||||
#initialize 2d array
|
|
||||||
Matrix = [[0 for x in range(count)] for y in range(count)]
|
|
||||||
|
|
||||||
#add count * count cubes
|
|
||||||
for i in range(count):
|
for i in range(count):
|
||||||
for j in range(count):
|
for j in range(count):
|
||||||
bpy.ops.mesh.primitive_cube_add(location=(j/2, 0, i/2), scale=(0.25, 0.25, 0.25))
|
bpy.ops.mesh.primitive_plane_add(location=(j*2, 0, i*2), rotation=(pi / 2, 0, 0), scale=(0.1, 0.1, 0.1))
|
||||||
|
|
||||||
#assign random scale to all cubes and add them to array
|
|
||||||
i = 0
|
|
||||||
j = 0
|
|
||||||
for ob in bpy.data.objects:
|
|
||||||
if ob.type == 'MESH':
|
|
||||||
if i == count:
|
|
||||||
j += 1
|
|
||||||
random.shuffle(index)
|
|
||||||
i = 0
|
|
||||||
mat = bpy.data.materials.new(name="MaterialName") #set new material to variable
|
|
||||||
mat.diffuse_color = (index[i], 255, 255, 255)
|
|
||||||
ob.data.materials.append(mat) #add the material to the object
|
|
||||||
Matrix[j].append(ob)
|
|
||||||
i += 1
|
|
||||||
|
|
||||||
#remove values that are 0 (needs to be solved beforehand)
|
|
||||||
for x in range(count):
|
|
||||||
for i in range(count):
|
|
||||||
Matrix[x].remove(0)
|
|
||||||
|
|
||||||
#presort arrayorder based on location
|
i=0
|
||||||
for i in range(count):
|
for ob in bpy.data.objects:
|
||||||
Matrix[i].sort(key = lambda obj: obj.location.x)
|
planes[i]= ob
|
||||||
|
i+=1
|
||||||
return (Matrix, count)
|
|
||||||
|
#sorts list of all objects based primary on their location.z and secondary on their location.x
|
||||||
|
planes.sort(key = lambda obj: obj.location.z + obj.location.x/(count*count))
|
||||||
|
|
||||||
|
for i in range(count):
|
||||||
|
material = bpy.data.materials.new(name="")
|
||||||
|
material.diffuse_color = (index[i]/3, 255, 255, 255)
|
||||||
|
materials[i] = material
|
||||||
|
|
||||||
|
for i in range(count):
|
||||||
|
random.shuffle(materials)
|
||||||
|
for j in range(count):
|
||||||
|
planes[j+i*count].data.materials.append(materials[j]) #add the material to the object
|
||||||
|
Matrix[i][j] = planes[j+i*count]
|
||||||
|
|
||||||
|
return(Matrix, count)
|
||||||
|
|
||||||
############################################################
|
############################################################
|
||||||
# Call Functions
|
# Call Functions
|
||||||
############################################################
|
############################################################
|
||||||
|
|
||||||
#max count = 31 (needs to be fixed)
|
Matrix, count = setup_array(32)
|
||||||
Matrix, count = setup_array(31)
|
|
||||||
|
|
||||||
#shell_sort every array
|
#shell_sort every array
|
||||||
for i in range(count):
|
for i in range(count):
|
||||||
|
|||||||
Reference in New Issue
Block a user