-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathwireframe3.py
More file actions
101 lines (81 loc) · 3.26 KB
/
wireframe3.py
File metadata and controls
101 lines (81 loc) · 3.26 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/bin/env python
import math
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):
print "\n --- Nodes --- "
for i, node in enumerate(self.nodes):
print " %d: (%.2f, %.2f, %.2f)" % (i, node.x, node.y, node.z)
def outputEdges(self):
print "\n --- Edges --- "
for i, edge in enumerate(self.edges):
print " %d: (%.2f, %.2f, %.2f)" % (i, edge.start.x, edge.start.y, edge.start.z),
print "to (%.2f, %.2f, %.2f)" % (edge.stop.x, edge.stop.y, edge.stop.z)
def translate(self, axis, d):
""" Add constant 'd' to the coordinate 'axis' of each node of a wireframe """
if axis in ['x', 'y', 'z']:
for node in self.nodes:
setattr(node, axis, getattr(node, axis) + d)
def scale(self, (centre_x, centre_y), scale):
""" Scale the wireframe from the centre of the screen """
for node in self.nodes:
node.x = centre_x + scale * (node.x - centre_x)
node.y = centre_y + scale * (node.y - centre_y)
node.z *= scale
def findCentre(self):
""" Find the centre of the wireframe. """
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)
def rotateX(self, (cx,cy,cz), radians):
for node in self.nodes:
y = node.y - cy
z = node.z - cz
d = math.hypot(y, z)
theta = math.atan2(y, z) + radians
node.z = cz + d * math.cos(theta)
node.y = cy + d * math.sin(theta)
def rotateY(self, (cx,cy,cz), radians):
for node in self.nodes:
x = node.x - cx
z = node.z - cz
d = math.hypot(x, z)
theta = math.atan2(x, z) + radians
node.z = cz + d * math.cos(theta)
node.x = cx + d * math.sin(theta)
def rotateZ(self, (cx,cy,cz), radians):
for node in self.nodes:
x = node.x - cx
y = node.y - cy
d = math.hypot(y, x)
theta = math.atan2(y, x) + radians
node.x = cx + d * math.cos(theta)
node.y = cy + d * math.sin(theta)
if __name__ == "__main__":
cube_nodes = [(x,y,z) for x in (0,1) for y in (0,1) for z in (0,1)]
cube = Wireframe()
cube.addNodes(cube_nodes)
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()