正二十面体的骰子,数学描述见http://zh.wikipedia.org/wiki/%E6%AD%A3%E4%BA%8C%E5%8D%81%E9%9D%A2%E9%AB%94
代码如下:
import rhinoscriptsyntax as rs
import math
def GetMidPoint(p1, p2, p3):
r = [(p1[0] + p2[0] + p3[0])/3, (p1[1] + p2[1] + p3[1])/3, (p1[2] + p2[2] + p3[2])/3]
return r
def RegularIcosahedronTest3():
EDGE_LEN = 2
FAN = (math.sqrt(5) + 1) / 2
SCALE_FACTOR = 12
INNER_R = FAN * FAN * EDGE_LEN / 2 / math.sqrt(3) * SCALE_FACTOR
OUTER_R = math.sqrt(FAN * math.sqrt(5)) / 2 * EDGE_LEN * SCALE_FACTOR
#outer_s = rs.AddSphere([0, 0, 0], OUTER_R)
#inner_s = rs.AddSphere([0, 0, 0], INNER_R)
#12 vertex points
#(0, +-1, +-FAN) (+-1, +-FAN, 0) (+-FAN, 0, +-1)
vertexPoints = [[0, SCALE_FACTOR, SCALE_FACTOR * FAN],
[0, -SCALE_FACTOR, SCALE_FACTOR * FAN],
[SCALE_FACTOR * FAN, 0, SCALE_FACTOR],
[SCALE_FACTOR, SCALE_FACTOR * FAN, 0],
[-SCALE_FACTOR, SCALE_FACTOR * FAN, 0],
[-SCALE_FACTOR * FAN, 0, SCALE_FACTOR],
[SCALE_FACTOR, -SCALE_FACTOR * FAN, 0],
[SCALE_FACTOR * FAN, 0, -SCALE_FACTOR],
[0, SCALE_FACTOR, -SCALE_FACTOR * FAN],
[-SCALE_FACTOR * FAN, 0, -SCALE_FACTOR],
[-SCALE_FACTOR, -SCALE_FACTOR * FAN, 0],
[0, -SCALE_FACTOR, -SCALE_FACTOR * FAN],
]
count = 0
for p in vertexPoints:
rs.AddTextDot (count, p)
#rs.AddText (str(count), p, height=1.0 , font="Arial" ,font_style=0, justification=131074 )
count += 1
#According to expand chart
LAYER1 = [0]
LAYER2 = [1, 2, 3, 4, 5, 1]
LAYER3 = [6, 7, 8, 9, 10, 6]
LAYER4 = [11]
#30 edge lines
edgeLines = []
for i in range(len(LAYER2)-1):
edgeLines.append(rs.AddLine(vertexPoints[LAYER2], vertexPoints[LAYER1[0]])) #5
edgeLines.append(rs.AddLine(vertexPoints[LAYER2], vertexPoints[LAYER2[i+1]])) #5
edgeLines.append(rs.AddLine(vertexPoints[LAYER2], vertexPoints[LAYER3]))#5
for i in range(len(LAYER3)-1):
edgeLines.append(rs.AddLine(vertexPoints[LAYER3], vertexPoints[LAYER2[i+1]]))#5
edgeLines.append(rs.AddLine(vertexPoints[LAYER3], vertexPoints[LAYER3[i+1]]))#5
edgeLines.append(rs.AddLine(vertexPoints[LAYER3], vertexPoints[LAYER4[0]]))#5
print str(len(edgeLines)) + ' edge lines'
#20 faces
mid_points = []
faces = []
vectors = []
for i in range(len(LAYER2)-1):
mp = GetMidPoint(vertexPoints[LAYER1[0]], vertexPoints[LAYER2], vertexPoints[LAYER2[i+1]])
mid_points.append(mp)
faces.append(rs.AddSrfPt ([vertexPoints[LAYER1[0]], vertexPoints[LAYER2[i+1]], vertexPoints[LAYER2]]))
vectors.append(rs.VectorCreate (mp, vertexPoints[LAYER1[0]]))
for i in range(len(LAYER2)-1):
mp = GetMidPoint(vertexPoints[LAYER2[i+1]], vertexPoints[LAYER3[i+1]], vertexPoints[LAYER3])
mid_points.append(mp)
faces.append(rs.AddSrfPt ([vertexPoints[LAYER2[i+1]], vertexPoints[LAYER3[i+1]], vertexPoints[LAYER3]]))
vectors.append(rs.VectorCreate (mp, vertexPoints[LAYER2[i+1]]))
for i in range(len(LAYER2)-1):
mp = GetMidPoint(vertexPoints[LAYER4[0]], vertexPoints[LAYER3], vertexPoints[LAYER3[i+1]])
mid_points.append(mp)
faces.append(rs.AddSrfPt ([vertexPoints[LAYER4[0]], vertexPoints[LAYER3], vertexPoints[LAYER3[i+1]]]))
vectors.append(rs.VectorCreate (mp, vertexPoints[LAYER4[0]]))
for i in range(len(LAYER2)-1):
mp = GetMidPoint(vertexPoints[LAYER3], vertexPoints[LAYER2], vertexPoints[LAYER2[i+1]])
mid_points.append(mp)
faces.append(rs.AddSrfPt ([vertexPoints[LAYER3], vertexPoints[LAYER2], vertexPoints[LAYER2[i+1]]]))
vectors.append(rs.VectorCreate (mp, vertexPoints[LAYER3]))
print str(len(faces)) + ' faces'
print mid_points
entities = []
for i in range(len(faces)):
#Add number on faces
xaxis = rs.VectorRotate(vectors, 90.0, mid_points)
plane = rs.PlaneFromNormal(mid_points, mid_points, xaxis)
txtObj = rs.AddText(str(i%10), plane, 10.0 , "Arial" ,font_style=1, justification=131074)
curveObj = rs.ExplodeText(txtObj, True)
tempSurface = rs.AddPlanarSrf(curveObj)
rs.SelectObjects(tempSurface)
rs.Command("_ExtrudeSrf -1")
#rs.Command("_ExtrudeSrf _BothSides 1 _Enter")
tempEntity = rs.BooleanDifference(rs.OffsetSurface(faces, 3, None, False, True), rs.LastCreatedObjects()[0])
if(tempEntity != None):
entities.append(tempEntity[0])
print str(len(entities)) + ' entities'
RegularIcosahedron = rs.BooleanUnion(entities)
rs.MoveObject(RegularIcosahedron, [0, 0, 50])
if __name__=="__main__":
rs.EnableRedraw(False)
RegularIcosahedronTest3()
rs.EnableRedraw(True)
|
例如
#Add number on faces
xaxis = rs.VectorRotate(vectors[i], 90.0, mid_points[i])
plane = rs.PlaneFromNormal(mid_points[i], mid_points[i], xaxis)
改为了
#Add number on faces
xaxis = rs.VectorRotate(vectors, 90.0, mid_points)
plane = rs.PlaneFromNormal(mid_points, mid_points, xaxis)
举报