User Tools

Site Tools


boissier:solution_homework2
  • Note: 3/5
  • Votre algorithme ne gère que le cas des arbres binaires.
  • La gestion des tailles est 'simpliste' (je vous cite) et ne produit pas l'effet d'emboîtement attendu.

Guy Melançon 2012/04/17 16:05


Cet algorithme ne fonctionne que sur des arbres binaires ! Le positionnement est un peu simpliste.

Voici le zip contenant l'arbre de test et le code : onion.zip

Voici le code seul :

rendu.py
from tulip import *
from collections import deque
import random
import time
 
def main(graph) : 
	simple_onion_binary_tree(graph)
 
def simple_onion_binary_tree(graph):
	node_list = deque([])
	child_list = deque([])
	division_list = deque([])
	pos_list = deque([])
	deep_list = deque([])
	root_node = my_getroot(graph)
	bb = create_bounding_box(graph)
	deep_total = get_deep(graph, root_node)
	node_list.append(root_node)
	child_list.append(0)
	division_list.append(1)
	pos_list.append(0)
	deep_list.append(0)
	deep = 0
	R = 0
	V = 255
	B = 0
	print "Graph initial BoundingBox Height"
	print bb.height()
	print "Graph initial BoundingBox Width"
	print bb.width()
 
	while (len(node_list) != 0):
		node = node_list.popleft()
		child = child_list.popleft()
		division = division_list.popleft()
		pos = pos_list.popleft()
		new_deep = deep_list.popleft()
		if (new_deep != deep):
			deep = new_deep
			R = random.randint(0, 255)
			V = random.randint(0, 255)
			B = random.randint(0, 255)
		width = (bb.width()) / (division)
		height = (bb.height()) / (division)
		work_on_a_node(graph, node, width, height, R, V, B)
		time.sleep(0.7)
		move_one_node(graph, node, 0, pos)
		nb_childs = nb_out_nodes(graph, node)
		child = 1
		while (child != (nb_childs + 1)):
			node_list.append(graph.getOutNode(node, child))
			child_list.append(child)
			division_list.append(division * nb_childs)
			deep_list.append(deep + 1)
			if ((child % 2) == 1):
				pos_list.append(pos + (height / nb_childs))
			else:
				pos_list.append(pos - (height / nb_childs))
			child += 1
 
#We modify a node from a little point to a big rectangle
def work_on_a_node(graph, node, W, H, R, V, B):
	color_one_node(graph, node, R, V, B)
	bigger_one_node(graph, node, W, H)
	updateVisualization()
 
#Get the deepness that is under the node
def get_deep(graph, node):
	if (nb_out_nodes(graph, node) == 0):
		return (0)
	deep = rec_get_deep(graph, node)
	return (deep - 1)
 
#Recursive function that give the depth
def rec_get_deep(graph, node):
	deep = 0
	deep_res = 0
	i = 1
	nb_childs = nb_out_nodes(graph, node)
	while (i != (nb_childs + 1)):
		deep = rec_get_deep(graph, graph.getOutNode(node, i))
		if (deep > deep_res):
			deep_res = deep
		i += 1
	return (deep_res + 1)
 
#Get the tree root
def my_getroot(graph) : 
	node = graph.getOneNode()
	while (nb_in_nodes(graph, node) != 0):
		get_first_in_node(graph, node)
	return (node)
 
#Number of childs of the node
def nb_out_nodes(graph, node):
	return length(graph.getOutNodes(node))
 
#Obtaining first node of the iterator (the only parent)
def get_first_in_node(graph, node):
	for iter in graph.getInNodes(node):
		return (iter)
 
#Obtaining number of parents (normally 1)
def nb_in_nodes(graph, node):
	return length(graph.getInNodes(node))
 
#Obtaining the number of instances in the iterator
def length(iterable):
	try:
		return (len(iterable))
	except:
		i = 0
		for x in iterable: 
			i += 1
		return (i)
 
 
####################
####################
def color_one_edge(graph, edge, R, V, B):
	color = graph.getColorProperty("viewColor")
	color.setEdgeValue(edge, tlp.Color(R, V, B, 255))
 
def color_one_node(graph, node, R, V, B):
	color = graph.getColorProperty("viewColor")
	color.setNodeValue(node, tlp.Color(R, V, B, 255))
 
#Obtaining the bounding box of the graph
def create_bounding_box(graph):
	layout =  graph.getLayoutProperty("viewLayout")
	size = graph.getSizeProperty("viewSize")
	rotation = graph.getDoubleProperty("viewRotation")
	bounding_box = tlp.computeBoundingBox(graph, layout, size, rotation)
	return (bounding_box)
 
#Move one node in the canva
def move_one_node(graph, node, X, Y):
	layout =  graph.getLayoutProperty("viewLayout")
	currentNodeLayout = layout.getNodeValue(node)
	currentNodeLayout.setX(X)
	currentNodeLayout.setY(Y)
	layout.setNodeValue(node, currentNodeLayout)
 
#Changing the size of a node
def bigger_one_node(graph, node, W, H):
	size =  graph.getSizeProperty("viewSize")
	currentNodeSize= size.getNodeValue(node)
	currentNodeSize.setW(W)
	currentNodeSize.setH(H)
	size.setNodeValue(node, currentNodeSize)
/net/html/perso/melancon/Visual_Analytics_Course/data/pages/boissier/solution_homework2.txt · Last modified: 2012/04/17 16:07 by melancon