### https://www.student.tugraz.at/bulatova/dm2/hu_03_bulatova.py ### grabbed: 2024-11-08 16:07:49 ################################# ############################## ### DM2_w24 hu_03_setUp ### ### _diag / 2024 10 17 ### ############################## import rhinoscriptsyntax as rs import random, time, sys ### # sys.path.append("P:/") ### add path where "DM_lib.py" can be found !!! sys.path.append("C:/Users/frede/Desktop/dm2/lib") import DM_lib as dm ### reload(dm) ############################## rs.UnitSystem(3) rs.ShowGrid(view=None, show=1) rs.ShowGridAxes(view=None, show=1) rs.ViewDisplayMode(view=None, mode="Wireframe") rs.EnableRedraw(0) dm.PointRadius(displayModeX=0, rad=1, styl=3, verbose=1) dm.PointRadius(displayModeX=1, rad=3, styl=2, verbose=1) rs.DeleteObjects(rs.AllObjects()) ###################################### anz = random.choice( range(8, 256, 4) ) # anz = 19 # dm.allCoords = dm.setUp_hu_03( anz ) # coordsCir = dm.allCoords[0] ### calling def from DM_lib to get *new* set of coords # coordsCub = dm.allCoords[1] ### calling def from DM_lib to get *new* set of coords ###################################### #rs.AddPoints( coordsCir ) #rs.AddCurve( coordsCub, 1 ) #dm.textDots( coordsCub ) if 0: randomVec = [ random.uniform(-30,30) for i in range(2) ] coordsCub = [ rs.VectorRotate(cor, randomVec[0], randomVec) for cor in coordsCub ] coordsCir = [ rs.VectorRotate(cor, randomVec[0], randomVec) for cor in coordsCir ] siz = rs.Distance( coordsCub[0], coordsCub[1] ) ### edge length of cubus #dm.textDots( coordsCub ) rs.AddCurve( coordsCub, 1 ) rs.AddCurve( coordsCir, 1 ) #dm.textDots( coordsCir ) anz = len(coordsCub) print "*** anz =", anz print "*** siz =", round(siz, 2), "/ edge lenght =", round(siz, 2),"*",int(anz/4),"=", round(siz*anz/4, 2) print "*** **********\n" ### here YOU go: ################ rs.ZoomExtents() #print ( 3>1) *1 ##print range(10, 30, 2) #for i in range(10, 30, 6): # print i, "hdgjhf" from math import sqrt def drawVector(vector, pointO = [0,0,0]): rs.AddCurve([pointO, vector], 3) def drawCube(center, sideLength, rotationAngle = 0, rotationAxis = [0,0,0], color=[0,0,0]): ''' center: center of the cube sideLength: length of the edge rotationAngle: angle of rotation around rotationAxis rotationAxis: axis of rotation color: color of the cube returns: cubeCurves ''' # Cube is composed of vertices (vectors) and curves (array of points) # Therefore we need to create vertices and use them to create curves # Create vector (half diagonal of the cube) pointO = [0, 0, 0] cubeVectors = [None]*8 # first 4 - base, last 4 - top # Create vector for drawing vertices of the cube halfDiagonalLength = sideLength*sqrt(3)/2 vectorHalfDiagonal = rs.VectorCreate(pointO, [halfDiagonalLength, halfDiagonalLength, halfDiagonalLength]) # Drawing all vertices for i in range(4): # Get vector above the current one vectorInvertedByZ = [x for x in vectorHalfDiagonal] vectorInvertedByZ[2] *= -1 cubeVectors[i] = vectorInvertedByZ cubeVectors[4+i] = vectorHalfDiagonal # Rotate current vector by 90 degrees around Z axis vectorHalfDiagonal = rs.VectorRotate(vectorHalfDiagonal, 90, [0, 0, 1] ) # Rotate cube # Important: rotate it in the pointO [0,0,0] for i in range(len(cubeVectors)): cubeVectors[i] = rs.VectorRotate(cubeVectors[i], rotationAngle, rotationAxis) # Move Cube from [0,0,0] to center for i in range(len(cubeVectors)): cubeVectors[i] = rs.VectorAdd(cubeVectors[i], center) # Create Curves cubeCurves = [] cubeCurves.append(rs.AddCurve( [cubeVectors[0], cubeVectors[1], cubeVectors[2], cubeVectors[3], cubeVectors[0]], 1 )) cubeCurves.append(rs.AddCurve( [cubeVectors[4], cubeVectors[5], cubeVectors[6], cubeVectors[7], cubeVectors[4]], 1 )) cubeCurves.append(rs.AddCurve( [cubeVectors[0], cubeVectors[1], cubeVectors[5], cubeVectors[4], cubeVectors[0]], 1 )) cubeCurves.append(rs.AddCurve( [cubeVectors[3], cubeVectors[2], cubeVectors[6], cubeVectors[7], cubeVectors[3]], 1 )) # Set colors for curve in cubeCurves: rs.ObjectColor(curve, color) return cubeCurves def drawSphere(center, radius, circlesQuantity, pointsInCircleQuantity, innerCurvesQuantity = 500): # Array to store vectors sphereVectors = [] # Angles of rotation. The idea is to create sphere by drawing only the upper hemisphere. # We do it by angleAroundZ = 360 angleAroundY = 90 # Steps angleStepPerZRotation = angleAroundZ / pointsInCircleQuantity angleStepPerYRotation = angleAroundY / circlesQuantity # Vector for drawing Sphere drawingVector = rs.VectorCreate([0,0,0], [radius, 0, 0]) # Drawing circles for i in range(circlesQuantity): # Drawing points of the circle for j in range(pointsInCircleQuantity): sphereVectors.append(drawingVector) # Rotate drawing vector around clock (z axis) drawingVector = rs.VectorRotate(drawingVector, angleStepPerZRotation, [0,0,1]) # Rotate drawing vector up (y axis) drawingVector = rs.VectorRotate(drawingVector, angleStepPerYRotation, [0,1,0]) # Add second hemisphere for i in range(len(sphereVectors)): invertedVector = [coord*(-1) for coord in sphereVectors[i]] sphereVectors.append(invertedVector) # Move sphere from [0,0,0] to center for i in range(len(sphereVectors)): sphereVectors[i] = rs.VectorAdd(sphereVectors[i], center) # for vector in sphereVectors: # rs.AddPoint(vector) # drawVector(vector, center) # Fill in with innerCurvesQuantity curves for i in range(innerCurvesQuantity): randomColor1 = random.randint(0, 255) randomColor2 = random.randint(0, 255) randomColor3 = random.randint(0, 255) p0 = random.choice(sphereVectors) p1 = random.choice(sphereVectors) p2 = random.choice(sphereVectors) # rs.ObjectColor(,[randomColor1, randomColor1, randomColor1]) rs.AddCurve([p0, p1, p2], 1) def mapCurveOnTheSphere(curve, curveDivisionVectorsQuantity, sphereCenter, sphereRadius, color=[0,0,0]): # Divide curve to get more points curveDivisionVectors = rs.DivideCurve(curve, curveDivisionVectorsQuantity, 0) for i, curveDivisionVector in enumerate(curveDivisionVectors): # Calculate vector between sphere's center and curve's vector # YOU NEED TO SUBTRACT CURVE'S VECTOR FROM SPHERE'S CENTER, NOT THE OTHER WAY AROUND vectorBetweenSphereAndCurve = rs.VectorSubtract(curveDivisionVector, sphereCenter) # Unitize vector, so that it can be scaled easily vectorBetweenSphereAndCurveUnitized = rs.VectorUnitize(vectorBetweenSphereAndCurve) # Scale Vector to the sphere's radius, so that it is directly on the Sphere's plane vectorBetweenSphereAndCurveScaled = rs.VectorScale(vectorBetweenSphereAndCurveUnitized, sphereRadius) vectorBetweenSphereAndCurveScaledAndMoved = rs.VectorAdd(vectorBetweenSphereAndCurveScaled, sphereCenter) if i%1==0: rs.ObjectColor(rs.AddLine( curveDivisionVector, vectorBetweenSphereAndCurveScaledAndMoved), color) #rs.AddLine( cen, circPnt ) rs.ObjectColor( rs.AddPoint( vectorBetweenSphereAndCurveScaledAndMoved ), [200,200,00] ) def demo(cubesQuantity, sphereCenter, sphereRadius, cubeCenter, cubeSideLength, cubeRotationAxis, cubeSelfRotationAxis, color=[255,255,255]): # Sphere in the center # Cubes around it with mapping drawSphere(sphereCenter, sphereRadius, 100, 100, 10000) angleAroundZ = 360 angleStepPerZRotation = angleAroundZ / cubesQuantity colorMaxValue = 255 colorValueStep = colorMaxValue / cubesQuantity cubeAngleOfRotation = 0 colVal = 0 cubeRotationAxisChanging = cubeRotationAxis for i in range(cubesQuantity): # Draw Cube # YOU NEED TO ROTATE CUBE BY 90 DEGRESS IN ORDER TO MAP SIDE CURVE ON THE SPHERE CORRECTLY cubeCurves = drawCube(cubeCenter, cubeSideLength, cubeAngleOfRotation+90, cubeSelfRotationAxis, [colVal, 255-colVal, 255-colVal]) # Move center of the cube, so that we draw another one slightly to the side cubeCenter = rs.VectorRotate(cubeCenter, angleStepPerZRotation, cubeRotationAxis) # Change angle of cube's rotation, so that is always faces the sphere cubeAngleOfRotation += angleStepPerZRotation cubeRotationAxisChanging = rs.VectorAdd(cubeRotationAxisChanging, cubeRotationAxis) mapCurveOnTheSphere(cubeCurves[3], 30, sphereCenter, sphereRadius, [colVal, 255-colVal, 255-colVal]) colVal += colorValueStep if 1: # cubeCenter = [1, 1, 1] # # sphereCenter = [21, 40, 20] # sphereRadius = 20 # # cubeCurves1 = drawCube(cubeCenter, 10, 15, [3, 10, 1]) # drawSphere(sphereCenter, sphereRadius, 100, 100) # # mapCurveOnTheSphere(cubeCurves1[0], 100, sphereCenter, sphereRadius) # mapCurveOnTheSphere(cubeCurves1[3], 100, sphereCenter, sphereRadius) demo(cubesQuantity = 16, sphereCenter = [0, 0, 30], sphereRadius = 20, cubeCenter = [40,0,30], cubeSideLength = 5, cubeRotationAxis = [0, 0, 1], cubeSelfRotationAxis = [2,1,2], color=[100,175,149])