|
From: Stephen H. <sh...@ua...> - 2000-10-23 16:51:55
|
I was testing the ico.py program and found several things that caused a
bunch of syntax errors--
many '3D's' and extra = signs VPython didn't like. Cleaning those out gave
a working program (below).
Did anybody else try this out? Did it run without being doctored?
Steve Highland [Arrrgh... I don't know what I'm doin' ...]
________________
from visual import *
class ico:
def __init__(self):
f =frame()
root5 = 5.0 ** 0.5
phi = (1.0 + root5)/2.0
tau = 1.0/phi
points=[(1.0,phi,0.0),(-1.0,phi,0.0),(-1.0,-phi,0.0),(1.0,-phi,0.0),
(0.0,1.0,phi),(0.0,-1.0,phi),(0.0,-1.0,-phi),(0.0,1.0,-phi),
(phi,0.0,1.0),(phi,0.0,-1.0),(-phi,0.0,-1.0),(-phi,0.0,1.0)]
self.vertices = []
for point in points:
self.vertices.append(sphere(frame=f, pos=point,
radius=0.4, color=color.blue))
ico_edge_indices = [
(0,1),(0,7),(0,9),(0,8),(0,4),
(1,7),(7,9),(9,8),(8,4),(4,1),
(7,10),(10,1),(1,11),(11,4),(4,5),
(5,8),(8,3),(3,9),(9,6),(6,7),
(10,11),(11,5),(5,3),(3,6),(6,10),
(2,10),(2,11),(2,5),(2,3),(2,6)]
self.edges = []
for edge in ico_edge_indices:
v1 = vector(points[edge[0]])
v2 = vector(points[edge[1]])
v3 = v2 - v1
self.edges.append(cylinder(frame=f, pos=v1, axis=v3,
color=color.red,radius=0.1))
face_list
=[0,9,7,6,10,2,11,5,4,8,0,9,0,1,7,10,0,4,1,11,10,6,2,3,5,8,11,2,10,6]
solid = []
for index in face_list:
solid.append(points[index])
self.faces = convex(frame=f, pos = solid, color=color.green)
self.frame = f
def rotate(self, angle=None):
self.frame.rotate(angle=angle, axis=(0.0,0.0,-1.0))
def cycleColors(self, object=None):
if object == None:
for obj in self.vertices: self.cycleColors(obj)
for obj in self.edges: self.cycleColors(obj)
self.cycleColors(self.faces)
return
(red, green, blue) = object.color
if red <= 0.0:
if green <= 0.0:
blue = blue - 0.01
red = red + 0.01
else:
green = green - 0.01
blue = blue + 0.01
elif green <= 0.0:
if (blue <= 0.0):
red = red - 0.01
green = green + 0.01
else:
blue = blue - 0.01
red = red + 0.01
else:
red = red - 0.01
green = green + 0.01
object.color = (red, green, blue)
if __name__ == '__main__':
i = ico()
# rate(30)
while(1):
i.rotate(0.005)
i.cycleColors()
|
|
From: Dethe E. <de...@al...> - 2000-10-24 15:51:46
|
Hi Stephen,
I see that in the archived message too. Since the extra "3D"s and "=" were
not in the original code (it ran fine before I sent it) I assume that they
were introduced as an artifact of my mailer or sending it as an attachment
(gotta switch from Outlook).
Below is the current version, no attachment this time. I've improved the
color cycling to improve performance a bit.
--Dethe
------------------------
from visual import *
class ico:
def __init__(self):
f = frame()
root5 = 5.0 ** 0.5
phi = (1.0 + root5)/2.0
tau = 1.0/phi
points =
[(1.0,phi,0.0),(-1.0,phi,0.0),(-1.0,-phi,0.0),(1.0,-phi,0.0),
(0.0,1.0,phi),(0.0,-1.0,phi),(0.0,-1.0,-phi),(0.0,1.0,-phi),
(phi,0.0,1.0),(phi,0.0,-1.0),(-phi,0.0,-1.0),(-phi,0.0,1.0)]
self.buildColorTable()
self.vertexColorIndex = 400 # blue
self.edgeColorIndex = 0 # red
self.faceColorIndex = 200 # green
self.vertexColor = self.colorTable[self.vertexColorIndex]
self.edgeColor = self.colorTable[self.edgeColorIndex]
self.faceColor = self.colorTable[self.faceColorIndex]
self.vertices = []
for point in points:
self.vertices.append(sphere(frame=f, pos=point, radius=0.4,
color=self.vertexColor))
ico_edge_indices = [
(0,1),(0,7),(0,9),(0,8),(0,4),
(1,7),(7,9),(9,8),(8,4),(4,1),
(7,10),(10,1),(1,11),(11,4),(4,5),
(5,8),(8,3),(3,9),(9,6),(6,7),
(10,11),(11,5),(5,3),(3,6),(6,10),
(2,10),(2,11),(2,5),(2,3),(2,6)]
self.edges = []
for edge in ico_edge_indices:
v1 = vector(points[edge[0]])
v2 = vector(points[edge[1]])
v3 = v2 - v1
self.edges.append(cylinder(frame=f, pos=v1, axis=v3, radius=0.1,
color=self.edgeColor))
face_list =
[0,9,7,6,10,2,11,5,4,8,0,9,0,1,7,10,0,4,1,11,10,6,2,3,5,8,11,2,10,6]
solid = []
for index in face_list:
solid.append(points[index])
self.faces = convex(frame=f, pos = solid, color=self.faceColor)
self.frame = f
def rotate(self, angle=None):
self.frame.rotate(angle=angle, axis=(0.0,0.0,-1.0))
def buildColorTable(self):
self.colorTable = []
red = 1.0
green = 0.0
blue = 0.0
for i in range(100):
self.colorTable.append((red,green,blue))
green = green + 0.01
for i in range(100):
self.colorTable.append((red,green,blue))
red = red - 0.01
for i in range (100):
self.colorTable.append((red,green,blue))
blue = blue + 0.01
for i in range(100):
self.colorTable.append((red,green,blue))
green = green - 0.01
for i in range (100):
self.colorTable.append((red,green,blue))
red = red + 0.01
for i in range(99):
self.colorTable.append((red,green,blue))
blue = blue - 0.01
self.maxcolorindex = len(self.colorTable)
def cycleColors(self):
self.cycleVertexColors()
self.cycleEdgeColors()
self.cycleFaceColors()
def cycleIndex(self, index):
index = index + 1
if index >= self.maxcolorindex:
index = 0
return index
def cycleVertexColors(self):
self.vertexColorIndex = self.cycleIndex(self.vertexColorIndex)
self.vertexColor = self.colorTable[self.vertexColorIndex]
for vertex in self.vertices:
vertex.color = self.vertexColor
def cycleEdgeColors(self):
self.edgeColorIndex = self.cycleIndex(self.edgeColorIndex)
self.edgeColor = self.colorTable[self.edgeColorIndex]
for edge in self.edges:
edge.color = self.edgeColor
def cycleFaceColors(self):
self.faceColorIndex = self.cycleIndex(self.faceColorIndex)
self.faceColor = self.colorTable[self.faceColorIndex]
self.faces.color = self.faceColor
def main():
i = ico()
while 1:
rate(30)
i.rotate(0.05)
i.cycleColors()
if __name__ == '__main__':
main()
|