×

工业设计互动平台

手机短信,快捷登录

微信登录,快人一步

QQ登录

只需一步,快速开始

正二十面体代码分享

发布于 2014-11-24 0 点赞 2 评论 3207 浏览

正二十面体的骰子,数学描述见http://zh.wikipedia.org/wiki/%E6%AD%A3%E4%BA%8C%E5%8D%81%E9%9D%A2%E9%AB%94
RegularIcosahedron.png
代码如下:

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)
参与人数 2经验 +3 大洋 +16 收起 理由
筑梦NARUTO + 10 很给力!
wyx10022 + 3 + 6 很给力!

查看全部评分

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

精彩回复

文明上网理性发言、请文明用语

wwwtiger | 设计助理 | 发表于 2014-11-24 16:36:24
怎么贴上去的代码有改动啊,是不是审核的时候被修改了?
例如
#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)
0 回复

举报

wwwtiger | 设计助理 | 发表于 2014-11-25 08:41:04
重新贴一遍试试
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[i]], vertexPoints[LAYER1[0]])) #5
        edgeLines.append(rs.AddLine(vertexPoints[LAYER2[i]], vertexPoints[LAYER2[i+1]])) #5
        edgeLines.append(rs.AddLine(vertexPoints[LAYER2[i]], vertexPoints[LAYER3[i]]))#5
        
    for i in range(len(LAYER3)-1):
        edgeLines.append(rs.AddLine(vertexPoints[LAYER3[i]], vertexPoints[LAYER2[i+1]]))#5
        edgeLines.append(rs.AddLine(vertexPoints[LAYER3[i]], vertexPoints[LAYER3[i+1]]))#5
        edgeLines.append(rs.AddLine(vertexPoints[LAYER3[i]], 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[i]], vertexPoints[LAYER2[i+1]])
        mid_points.append(mp)
        faces.append(rs.AddSrfPt ([vertexPoints[LAYER1[0]], vertexPoints[LAYER2[i+1]], vertexPoints[LAYER2[i]]]))
        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[i]])
        mid_points.append(mp)
        faces.append(rs.AddSrfPt ([vertexPoints[LAYER2[i+1]], vertexPoints[LAYER3[i+1]], vertexPoints[LAYER3[i]]]))
        vectors.append(rs.VectorCreate (mp, vertexPoints[LAYER2[i+1]]))
        
    for i in range(len(LAYER2)-1):
        mp = GetMidPoint(vertexPoints[LAYER4[0]], vertexPoints[LAYER3[i]], vertexPoints[LAYER3[i+1]])
        mid_points.append(mp)
        faces.append(rs.AddSrfPt ([vertexPoints[LAYER4[0]], vertexPoints[LAYER3[i]], vertexPoints[LAYER3[i+1]]]))
        vectors.append(rs.VectorCreate (mp, vertexPoints[LAYER4[0]]))
        
    for i in range(len(LAYER2)-1):
        mp = GetMidPoint(vertexPoints[LAYER3[i]], vertexPoints[LAYER2[i]], vertexPoints[LAYER2[i+1]])
        mid_points.append(mp)
        faces.append(rs.AddSrfPt ([vertexPoints[LAYER3[i]], vertexPoints[LAYER2[i]], vertexPoints[LAYER2[i+1]]]))
        vectors.append(rs.VectorCreate (mp, vertexPoints[LAYER3[i]]))
        
    print str(len(faces))  + ' faces'  
    print mid_points
   
    entities = []
    for i in range(len(faces)):
        #Add number on faces
        xaxis = rs.VectorRotate(vectors[i], 90.0, mid_points[i])
        plane = rs.PlaneFromNormal(mid_points[i], mid_points[i], 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[i], 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)
0 回复

举报