[r47]: wireframe_core.py  Maximize  Restore  History

Download this file

58 lines (46 with data), 1.8 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
"""
Wireframe 3D cube simulation.
Developed by Leonel Machava <leonelmachava@gmail.com>
http://codeNtronix.com
"""
class Node:
def __init__(self, coordinates):
self.x = coordinates[0]
self.y = coordinates[1]
self.z = coordinates[2]
class Edge:
def __init__(self, start, stop):
self.start = start
self.stop = stop
class Wireframe:
def __init__(self):
self.nodes = []
self.edges = []
def addNodes(self, nodeList):
for node in nodeList:
self.nodes.append(Node(node))
def addEdges(self, edgeList):
for (start, stop) in edgeList:
self.edges.append(Edge(self.nodes[start], self.nodes[stop]))
def outputNodes(self):
for i, node in enumerate(self.nodes):
print "Node %d: (%.3f, %.3f, %.3f)" % (i, node.x, node.y, node.z)
def outputEdges(self):
for i, edge in enumerate(self.edges):
print "Edge %d: (%.3f, %.3f, %.3f)" % (i, edge.start.x, edge.start.y, edge.start.z),
print "to (%.3f, %.3f, %.3f)" % (edge.stop.x, edge.stop.y, edge.stop.z)
def findCentre(self):
num_nodes = len(self.nodes)
meanX = sum([node.x for node in self.nodes]) / num_nodes
meanY = sum([node.y for node in self.nodes]) / num_nodes
meanZ = sum([node.z for node in self.nodes]) / num_nodes
return (meanX, meanY, meanZ)
if __name__ == "__main__":
cubeNodes = [(x,y,z) for x in (0,1) for y in (0,1) for z in (0,1)]
cube = Wireframe()
cube.addNodes(cubeNodes)
cube.addEdges([(n,n+4) for n in range(0,4)])
cube.addEdges([(n,n+1) for n in range(0,8,2)])
cube.addEdges([(n,n+2) for n in (0,1,4,5)])
cube.outputNodes()
cube.outputEdges()